Created
December 7, 2016 18:38
-
-
Save arumoy/a77dd3e77d4dde1580ff22aa4b1997db to your computer and use it in GitHub Desktop.
Great Circle Haversine function on .NET framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace GreatCircleNavigation | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
MapPoint m1 = new MapPoint(12.851853, 77.664216); | |
MapPoint m2 = new MapPoint(12.851854, 77.664215); | |
Console.WriteLine(m1.isWithinRange(m2, 20.00)); | |
Console.ReadKey(); | |
} | |
} | |
class MapPoint | |
{ | |
private double latitude, longitude; | |
private double R = 6371e3; | |
private static double toRadian(double degree) | |
{ | |
return (Math.PI / 180) * degree; | |
} | |
public MapPoint(double latitude, double longitude) | |
{ | |
this.latitude = MapPoint.toRadian(latitude); | |
this.longitude = MapPoint.toRadian(longitude); | |
} | |
public bool isWithinRange(MapPoint point, double radius) | |
{ | |
double delPhi = this.getLatitude() - point.getLatitude(); | |
double delLambda = this.getLongitude() - point.getLongitude(); | |
double haversine = Math.Sin(delPhi / 2) * Math.Sin(delPhi / 2) + | |
Math.Cos(this.getLatitude()) * Math.Cos(point.getLatitude()) | |
Math.Sin(delLambda / 2) * Math.Sin(delLambda / 2); | |
double c = 2 * Math.Atan2(Math.Sqrt(haversine), Math.Sqrt(1 - haversine)); | |
double rad = R * c; | |
Console.WriteLine(rad); | |
return rad <= radius ? true : false; | |
} | |
double getLatitude() | |
{ | |
return this.latitude; | |
} | |
double getLongitude() | |
{ | |
return this.longitude; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment