/// <summary> /// Calculate the distance between two geographic points, taking into consideration the earth's curvature. /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <returns></returns> public static double Distance(GeoPoint first, GeoPoint second) { // Use the haversine formula to calculate distance. //var R = 6371e3; // metres //var φ1 = lat1.toRadians(); //var φ2 = lat2.toRadians(); //var Δφ = (lat2 - lat1).toRadians(); //var Δλ = (lon2 - lon1).toRadians(); //var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + // Math.cos(φ1) * Math.cos(φ2) * // Math.sin(Δλ / 2) * Math.sin(Δλ / 2); //var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); //var d = R * c; var lat1 = ArgumentUtils.ToRadians(first.Latitude); var lat2 = ArgumentUtils.ToRadians(second.Latitude); var deltaLat = ArgumentUtils.ToRadians(second.Latitude - first.Latitude); var deltaLon = ArgumentUtils.ToRadians(second.Longitude - first.Longitude); var a = Math.Sin(deltaLat / 2) * Math.Sin(deltaLat / 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Sin(deltaLon / 2) * Math.Sin(deltaLon / 2); var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); return(ArgumentUtils.MeanEarthRadius * c); }
public static double Bearing(GeoPoint first, GeoPoint second) { //var y = Math.sin(λ2 - λ1) * Math.cos(φ2); //var x = Math.cos(φ1) * Math.sin(φ2) - // Math.sin(φ1) * Math.cos(φ2) * Math.cos(λ2 - λ1); //var brng = Math.atan2(y, x).toDegrees(); var deltaLat = ArgumentUtils.ToRadians(second.Latitude - first.Latitude); var deltaLon = ArgumentUtils.ToRadians(second.Longitude - first.Longitude); var lat1 = ArgumentUtils.ToRadians(first.Latitude); var lat2 = ArgumentUtils.ToRadians(second.Latitude); var y = Math.Sin(deltaLon) * Math.Cos(lat2); var x = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(deltaLat); return(ArgumentUtils.ToDegrees(Math.Atan2(y, x))); }
private static double ToY(double latitude, int zoomLevel) { var zoomFactor = K * Math.Pow(2, zoomLevel); return(zoomFactor * (Math.PI - Math.Log(Math.Tan(Math.PI / 4 + ArgumentUtils.ToRadians(latitude) / 2)))); }
private static double ToX(double longitude, int zoomLevel) { var zoomFactor = K * Math.Pow(2, zoomLevel); return(zoomFactor * (ArgumentUtils.ToRadians(longitude) + Math.PI)); }