namespace Graph.Utils; public static class NodeUtils { public const int EarthRadius = 6371000; /* * Close enough */ public static double DistanceBetween(float lat1, float lon1, float lat2, float lon2) { double radiansLat1 = DegreesToRadians(lat1); double radiansLat2 = DegreesToRadians(lat2); double deltaRadiansLon = DegreesToRadians(lon2 - lon1); double distance = Math.Acos(Math.Sin(radiansLat1) * Math.Sin(radiansLat2) + Math.Cos(radiansLat1) * Math.Cos(radiansLat2) * Math.Cos(deltaRadiansLon)) * EarthRadius; return distance; } public static double DistanceBetween(Node n1, Node n2) => DistanceBetween(n1.Lat, n1.Lon, n2.Lat, n2.Lon); public static double DistanceBetween(Node n1, float lat2, float lon2) => DistanceBetween(n1.Lat, n1.Lon, lat2, lon2); public static double DistanceBetween(float lat1, float lon1, Node n2) => DistanceBetween(n2, lat1, lon1); /* * From https://www.movable-type.co.uk/scripts/latlong.html */ public static double DistanceBetweenHaversine(float lat1, float lon1, float lat2, float lon2) { double radiansLat1 = DegreesToRadians(lat1); double radiansLat2 = DegreesToRadians(lat2); double deltaRadiansLat = DegreesToRadians(lat2 - lat1); double deltaRadiansLon = DegreesToRadians(lon2 - lon1); double a = Math.Sin(deltaRadiansLat / 2) * Math.Sin(deltaRadiansLat / 2) + Math.Cos(radiansLat1) * Math.Cos(radiansLat2) * Math.Sin(deltaRadiansLon / 2) * Math.Sin(deltaRadiansLon / 2); double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); double d = EarthRadius * c; return d; } public static double DegreesToRadians(float deg) { return deg * Math.PI /180; } public static double RadiansToDegrees(float rad) { return rad * 180 / Math.PI; } }