Skip to content

Instantly share code, notes, and snippets.

@carlosmoretti
Created May 20, 2020 21:30
Show Gist options
  • Select an option

  • Save carlosmoretti/72c49ac6eb21dd814057b7a0a6c1313e to your computer and use it in GitHub Desktop.

Select an option

Save carlosmoretti/72c49ac6eb21dd814057b7a0a6c1313e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
public static void Main(String[] args)
{
string[] coords = new string[] { "-22.902794,-43.719856", "-22.901263,-43.734494", "-22.906334,-43.719430", "-22.900935,-43.732292", "-22.902794,-43.719856" };
List<double> distances = new List<double>();
//var a = (6.372795477598 * Math.Acos(Math.Sin(lata) * Math.Sin(latb) + Math.Cos(lata) * Math.Cos(latb) * Math.Cos(longa - longb)));
var calc = Haversine.calculate(-22.902794, -43.719856, -22.901263, -43.734494);
Console.Read();
}
}
public static class Haversine
{
public static double calculate(double lat1, double lon1, double lat2, double lon2)
{
var R = 6372.8; // In kilometers
var dLat = toRadians(lat2 - lat1);
var dLon = toRadians(lon2 - lon1);
lat1 = toRadians(lat1);
lat2 = toRadians(lat2);
var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
var c = 2 * Math.Asin(Math.Sqrt(a));
return R * c;
}
public static double toRadians(double angle)
{
return Math.PI * angle / 180.0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment