HolesCS - sindizzy/DSW GitHub Wiki
Sample code showing how to generate a polygon from random points that contains a hole.
using DotSpatial.Geometries;
using DotSpatial.Topology.Geometries;
public void PolgygonHolesSC()
{
//Defines a new coordinate array
Coordinate[]() coords = new Coordinate[20](20);
//Defines a new random number generator
Random rnd = new Random();
//defines a randomly generated center for teh polygon
Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90);
for (int i = 0; i < 19; i++)
{
//generates random coordinates and adds those coordinates to the array
coords[i](i) = new Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 10), center.Y + (i * 10) * Math.PI / 10);
}
//sets the last coordinate equal to the first, this 'closes' the polygon
coords[19](19) = new Coordinate(coords[0](0)(0).X, coords[0](0)(0).Y);
//defines a new LingRing from the coordinates
LinearRing Ring = new LinearRing(coords);
//Repeates the process, but generates a LinearRing with a smaller area, this will be the hole in the polgyon
Coordinate[]() coordshole = new Coordinate[20](20);
for (int i = 0; i < 20; i++)
{
coordshole[i](i) = new Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 20), center.Y + (i * 10) * Math.PI / 20);
}
coordshole[19](19) = new Coordinate(coordshole[0](0)(0).X, coordshole[0](0)(0).Y);
LinearRing Hole = new LinearRing(coordshole);
//This steps addes the hole LinerRing to a ILinearRing Array
//A Polgyon can contain multiple holes, thus a Array of Hole is required
ILinearRing[]() Holes = new ILinearRing[1](1);
Holes[0](0) = Hole;
//This passes the Ring, the polygon shell, and the Holes Array, the holes
Polygon pg = new Polygon(Ring, Holes);
}