public static double ToYard(this double value, UnitsOfLength unit) { var result = 0.0; switch (unit) { case UnitsOfLength.Foot: result = value * 0.33333333; break; case UnitsOfLength.Kilometer: result = value * 1093.6133; break; case UnitsOfLength.Meter: result = value * 1.0936133; break; case UnitsOfLength.Mile: result = value * 1760; break; case UnitsOfLength.Yard: result = value; break; case UnitsOfLength.NauticalMile: result = value * 2025.37183; break; } return(result); }
/// <summary> /// Calculates the distance between two points of latitude and longitude. /// Great Link - http://www.movable-type.co.uk/scripts/latlong.html /// </summary> /// <param name="coordinate1">First coordinate.</param> /// <param name="coordinate2">Second coordinate.</param> /// <param name="unitsOfLength">Sets the return value unit of length.</param> public Double Distance(Coordinate coordinate1, Coordinate coordinate2, UnitsOfLength unitsOfLength) { var theta = coordinate1.Longitude - coordinate2.Longitude; var distance = Math.Sin(coordinate1.Latitude.ToRadian()) * Math.Sin(coordinate2.Latitude.ToRadian()) + Math.Cos(coordinate1.Latitude.ToRadian()) * Math.Cos(coordinate2.Latitude.ToRadian()) * Math.Cos(theta.ToRadian()); distance = Math.Acos(distance); distance = distance.ToDegree(); distance = distance * 60 * 1.1515; if (unitsOfLength == UnitsOfLength.Kilometer) { distance = distance * Constants.MilesToKilometers; } else if (unitsOfLength == UnitsOfLength.NauticalMiles) { distance = distance * Constants.MilesToNautical; } else if (unitsOfLength == UnitsOfLength.Meter) { distance = distance * Constants.MilesToKilometers * 1000; } return(distance); }
public static double ToMeter(this double value, UnitsOfLength unit) { var result = 0.0; switch (unit) { case UnitsOfLength.Foot: result = value * 0.3048; break; case UnitsOfLength.Kilometer: result = value * 1000; break; case UnitsOfLength.Meter: result = value; break; case UnitsOfLength.Mile: result = value * 1609.344; break; case UnitsOfLength.Yard: result = value * 0.9144; break; case UnitsOfLength.NauticalMile: result = value * 1852; break; } return(result); }
public Area ConvertTo(UnitsOfLength units) { if (Units == units) { return(this); } //tood: fix Area.ConvertTo() // this method isn't working. I think we need to take the square root of the area, convert to the new units and then square the new value for the correct area. // other ideas: https://www.varsitytutors.com/hotmath/hotmath_help/topics/convert-units-of-area-and-volume switch (units) { case UnitsOfLength.Foot: return(new Area(Value.ToFoot(Units), units)); case UnitsOfLength.Yard: return(new Area(Value.ToYard(Units), units)); case UnitsOfLength.Mile: return(new Area(Value.ToMile(Units), units)); case UnitsOfLength.Meter: return(new Area(Value.ToMeter(Units), units)); case UnitsOfLength.Kilometer: return(new Area(Value.ToKilometer(Units), units)); case UnitsOfLength.NauticalMile: return(new Area(Value.ToNauticalMile(Units), units)); default: throw new NotSupportedException(); } }
public Distance ConvertTo(UnitsOfLength units) { if (Units == units) { return(this); } switch (units) { case UnitsOfLength.Foot: return(new Distance(Value.ToFoot(Units), units)); case UnitsOfLength.Yard: return(new Distance(Value.ToYard(Units), units)); case UnitsOfLength.Mile: return(new Distance(Value.ToMile(Units), units)); case UnitsOfLength.Meter: return(new Distance(Value.ToMeter(Units), units)); case UnitsOfLength.Kilometer: return(new Distance(Value.ToKilometer(Units), units)); case UnitsOfLength.NauticalMile: return(new Distance(Value.ToNauticalMile(Units), units)); default: throw new NotSupportedException(); } }
public Distance GetDistance(UnitsOfLength units) { // it's faster to keep the calculations in nautical miles until the sumation is complete because NM is the native units for distance calculations. var value = 0.0; for (var i = 0; i < Count - 2; ++i) { value += this[i].GetDistance(this[i + 1], UnitsOfLength.NauticalMile).Value; } return(new Distance(value, UnitsOfLength.NauticalMile).ConvertTo(units)); }
/// <summary> /// Calculates the distance between two points of latitude and longitude. /// Great Link - http://www.movable-type.co.uk/scripts/latlong.html /// </summary> /// <param name="coordinate1">First coordinate.</param> /// <param name="coordinate2">Second coordinate.</param> /// <param name="unitsOfLength">Sets the return value unit of length.</param> public static Double Distance(Coordinates coordinate1, Coordinates coordinate2, UnitsOfLength unitsOfLength) { var theta = coordinate1.Longitude - coordinate2.Longitude; var distance = Math.Sin(coordinate1.Latitude.ToRadian()) * Math.Sin(coordinate2.Latitude.ToRadian()) + Math.Cos(coordinate1.Latitude.ToRadian()) * Math.Cos(coordinate2.Latitude.ToRadian()) * Math.Cos(theta.ToRadian()); distance = Math.Acos(distance); distance = distance.ToDegree(); distance = distance * 60 * 1.1515; if (unitsOfLength == UnitsOfLength.Kilometer) distance = distance * MilesToKilometers; else if (unitsOfLength == UnitsOfLength.NauticalMiles) distance = distance * MilesToNautical; return (distance); }
public virtual Area GetArea(UnitsOfLength units) { //https://planetcalc.com/1466/ //https://www.mathsisfun.com/geometry/herons-formula.html double area = 0.0; for (var i = 1; i < Count - 2; ++i) { var a = this[0].GetDistance(this[i], units); var b = this[i].GetDistance(this[i + 1], units); var c = this[0].GetDistance(this[i + 1], units); var s = (a + b + c) / 2.0; var x = s.Value * (s - a).Value * (s - b).Value * (s - c).Value; area += Math.Sqrt(x); } return(new Area(area, units)); }
public Area(double value, UnitsOfLength units) { Value = value; Units = units; }
public Distance(double value, UnitsOfLength units) { Value = value; Units = units; }