1

Given the following code I would like to compute the distance in Miles or Kilometers:

Point2D point1 = new Point2D(47.6062, -122.3321); // Seattle
Point2D point2 = new Point2D(37.7749, -122.4194); // San Francisco

double distance = point1.DistanceTo(point2);

After reviewing the documentation, it is not clear what type of unit is returned. I see there is a units section in the documentation.

However, it is not clear what the unit of distance would be:

  • Angle
  • AngleUnit
  • Degrees
  • IAngleUnit
  • Radians

Also, given the returned unit is one of the items listed above. How, would you handle the conversion to miles / kilometers?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Kyle Barnes
  • 729
  • 3
  • 13
  • 22

1 Answers1

0

According to documentation double Point2D.DistanceTo(Point2D otherPoint) "Finds the straight line distance to another point". That means you are supposed to be on a plain. The result units are the same as ones used to create Point2D objects. But that has nothing to do with finding distances on the surface of the Earth. What you need is:

static void Main(string[] args)
{
    var earthRadius = 6371; // km
    var seattle = GetVector(47.6062, -122.3321);
    var sanFrancisco = GetVector(37.7749, -122.4194);
    var angle = seattle.AngleTo(sanFrancisco);
    var distance = angle.Radians * earthRadius;
    Console.WriteLine(distance);
}


private static UnitVector3D GetVector(double lat, double lon)
{
    var tmp = UnitVector3D.ZAxis;
    var latitude = Angle.FromDegrees(lat);
    var longitude = Angle.FromDegrees(lon);
    var vector = UnitVector3D.XAxis.Rotate(tmp, longitude);
    tmp = UnitVector3D.ZAxis.CrossProduct(vector);
    vector = vector.Rotate(tmp, latitude);
    return vector;
}

The result is:

1093,21376382715

Check it is correct.

To get miles change earthRadius value to one in miles.

Update:

GetVector can be implemented another way:

private static UnitVector3D north = UnitVector3D.ZAxis;
private static UnitVector3D east = UnitVector3D.XAxis;
private static Point3D northPole = UnitVector3D.ZAxis.ToPoint3D();
private static Point3D southPole = (-1 * UnitVector3D.ZAxis).ToPoint3D();

private static UnitVector3D GetVector(double lat, double lon)
{
    var longitude = Angle.FromDegrees(lon);
    var direction = east.Rotate(north, longitude);
    var meridian = Circle3D.FromPoints(northPole, southPole, direction.ToPoint3D());
    var normal = meridian.Axis;
    var latitude = Angle.FromDegrees(lat);
    return direction.Rotate(normal, latitude);
}
CSDev
  • 3,177
  • 6
  • 19
  • 37