Computing Centroid of a polygon

Jul 23, 2012 at 10:43 AM

Polygon geometry has been constructed and when i tried to compute the centroid of that polygon below mentioned point is returned

{POINT (NaN NaN)}

Here is the code
            Random rnd = new Random();
            Coordinate[] c = new Coordinate[3];
            for (int i = 0; i < 2; i++)
            {
                c[i] = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90);
            }
            c[2] = c[0];
            var polygon = new DotSpatial.Topology.Polygon(c);
            var centroid = polygon.Centroid;

How to compute the centroid of geometries?

Developer
Jul 23, 2012 at 7:17 PM

You'll want to add the first point again at the end of the array to close the polygon, like you are doing, but you need at least 4 points, not three.

Jul 24, 2012 at 5:02 AM

Thanks