geospatial - Shorten this code that determines distance between two lat/longs in C#? -
public static double distance(latlong from, latlong to) { double lat1 = from.latitude * (math.pi / 180.0); double lat2 = to.latitude * (math.pi / 180.0); return math.acos((math.sin(lat1) * math.sin(lat2)) + (math.cos(lat1) * math.cos(lat2) * math.cos((math.pi / 180.0) * (to.longitude - from.longitude)))) * 3958.760; }
can shorten code stretch? i'm wondering ...
that's standard spherical law of cosines formula. won't simpler that. @ best, clean code little:
public static double distance(latlong from, latlong to) { double deg = math.pi / 180.0; // 1 degree in radians double lat1 = from.latitude * deg; double lat2 = to.latitude * deg; double dlng = (to.longitude - from.longitude) * deg; double r = 3958.760; return math.acos(math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(dlng)) * r; }
Comments
Post a Comment