Skip to content

Instantly share code, notes, and snippets.

@kiichi
Created August 7, 2015 23:19
Show Gist options
  • Select an option

  • Save kiichi/de3c67cc93f6750bb671 to your computer and use it in GitHub Desktop.

Select an option

Save kiichi/de3c67cc93f6750bb671 to your computer and use it in GitHub Desktop.
Area Calculation in C#
using System;
namespace DistanceAreaCalc {
class GeoCoordinate{
public double Longitude;
public double Latitude;
public GeoCoordinate(double lat, double lng) {
this.Latitude = lat;
this.Longitude = lng;
}
}
class MainClass {
public static void Main (string[] args) {
double area = GetArea(new GeoCoordinate[]{
new GeoCoordinate(40.76822,-73.981567),
new GeoCoordinate(40.76439,-73.97308),
new GeoCoordinate(40.796787,-73.949398),
new GeoCoordinate(40.800654,-73.958248),
new GeoCoordinate(40.76822,-73.981567)
});
Console.WriteLine(area/1000000 + " km^2");
}
public static double Rad(double deg){
return deg * Math.PI / 180.0f;
}
public static double GetArea(GeoCoordinate[] coords){
var area = 0.0;
var len = coords.Length;
if (len > 2){
for (var i = 0; i < len - 1; i++){
GeoCoordinate p1 = coords[i];
GeoCoordinate p2 = coords[i + 1];
area += Rad(p2.Longitude - p1.Longitude) * (2 + Math.Sin(Rad(p1.Latitude)) + Math.Sin(Rad(p2.Latitude)));
}
area = area * 6378137.0 * 6378137.0 / 2.0;
}
return Math.Abs(area);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment