Created
August 7, 2015 23:19
-
-
Save kiichi/de3c67cc93f6750bb671 to your computer and use it in GitHub Desktop.
Area Calculation in C#
This file contains hidden or 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; | |
| 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