PolygonCS - sindizzy/DSW GitHub Wiki

Sample code that demonstrates the creation of a new polygon from random points and calculating the area

using DotSpatial.Topology;
using DotSpatial.Common;

private void btnPolygon_Click(object sender, EventArgs e)
  {
      //creates a new coordinate array
      Coordinate[]() coords = new Coordinate[10](10);
      //creates a random point variable
      Random rnd = new Random();
      //Creates the center coordiante for the new polygon
      Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90);
      //a for loop that generates a new random X and Y value and feeds those values into the coordinate array
      for (int i = 0; i < 10; i++)
      {
          coords[i](i) = new Coordinate(center.X + Math.Cos((i * 2) * Math.PI / 18), center.Y + (i * 2) * Math.PI / 18);
      }
      //creates a new polygon from the coordinate array
      coords[9](9) = new Coordinate(coords[0](0)(0).X, coords[0](0)(0).Y);
      Polygon pg = new Polygon(coords);
      //new variable for the area of the polgyon
      Double area;
      area = pg.Area;
      //displays the area of the polygon
      MessageBox.Show("The Area of the polygon is: " + area);
  }