PolygonAreaCS - sindizzy/DSW GitHub Wiki

Sample code that will generate a coordinate array, create a polygon from the coordinate array, and calculate the area of the polygon.

using DotSpatial.Geometries;
using DotSpatial.Topology.Geometries;

{
     //Creates a new array of coordinates
     Coordinate[]() coords = new Coordinate[20](20);
     //Creates a random number generator
     Random rnd = new Random();
     //Createa a center point for the polygon
     Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90);
     //A For Loop that will randomly create coordinates and feeds the coordinates into the array of coordiantes
     for (int i = 0; i < 19; i++)              
     {
         coords[i](i) = new Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 10), center.Y + (i * 10) * Math.PI / 10);
     }
     //Set the last coordinate equal to the first coordinate in the array, thus 'closing' the polygon
     coords[19](19) = new Coordinate(coords[0](0)(0).X, coords[0](0)(0).Y);
     //Creates a new polygon from the coordinate array
     Polygon pg = new Polygon(coords);
     //Determines that area of the polygon
     Double area = pg.Area;
}