Skip to content

Instantly share code, notes, and snippets.

@ahmadmahboubi
Last active April 29, 2024 13:20
Show Gist options
  • Select an option

  • Save ahmadmahboubi/499db76fdcd2b3ce6de6246fc126552f to your computer and use it in GitHub Desktop.

Select an option

Save ahmadmahboubi/499db76fdcd2b3ce6de6246fc126552f to your computer and use it in GitHub Desktop.

Revisions

  1. ahmadmahboubi renamed this gist Apr 29, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. ahmadmahboubi created this gist Apr 29, 2024.
    15 changes: 15 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    private double HaversineDistance(Coordinates point1, Coordinates point2)
    {
    const double EarthRadiusKm = 6371;
    var dLat = Math.PI * ((double)point2.Latitude - (double)point1.Latitude) / 180.0;
    var dLon = Math.PI * ((double)point2.Longitude - (double)point1.Longitude) / 180.0;

    var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
    Math.Cos(Math.PI * (double)point1.Latitude / 180.0) * Math.Cos(Math.PI * (double)point2.Latitude / 180.0) *
    Math.Sin(dLon / 2) * Math.Sin(dLon / 2);

    var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
    var distance = EarthRadiusKm * c;

    return distance;
    }