/** * Calculate the great circle distance from one lat/long point to another using implementation of Haversine */ public static double Haversine(LatLongPoint PointA, LatLongPoint PointB) { double dLat = PointB.latitude - PointA.latitude; // dLat is the difference in latitude double dLon = PointB.longitude - PointA.longitude; // dLon is the difference in longitude double lat1 = PointA.latitude; // saves space in formula for readability double lat2 = PointB.latitude; // " " " " " " /* This bit (commented) requires conversion to radians first, the next bit (in use) includes (degrees / (180/PI)) // http://www.movable-type.co.uk/scripts/latlong.html double a = (Math.Sin(dLat / 2.0) * Math.Sin(dLat / 2.0)) + Math.Sin(dLon / 2.0) * Math.Sin(dLon / 2.0) * Math.Cos(lat1) * Math.Cos(lat2); double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1.0 - a));*/ // http://www.meridianworlddata.com/Distance-Calculation.asp double a = Math.Sin(lat1/57.2958) * Math.Sin(lat2/57.2958) + Math.Cos(lat1/57.2958) * Math.Cos(lat2/57.2958) * Math.Cos(dLon/57.2958); double c = Math.Atan(Math.Sqrt(1 - (a * a)) / a); double distance = (double)RADIUS_OF_EARTH * c; return distance; }
/** * Calculate the straight line distance from one 3d Cartesian coordinate to another, this straight line goes * THROUGH the sphere of the earth, not over the surface, and is used in the SurfaceDistance() function */ public static double StraightLineDistance(LatLongPoint a, LatLongPoint b) { double distance = 0.0; double xd, yd, zd; xd = b.x - a.x; // find a vector from a -> b by subtracting the coordinates of a from the coordinates of b yd = b.y - a.y; zd = b.z - a.z; distance = Math.Sqrt((xd * xd) + (yd * yd) + (zd * zd)); // distance = magnitude of the vector return distance; }