I am working with geospatial shapes and looking at the centroid algorithm here,
http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon
I have implemented the
This answer is inspired by the answer by Jer2654 and this source: http://coding-experiments.blogspot.com/2009/09/xna-quest-for-centroid-of-polygon.html
///
/// Method to compute the centroid of a polygon. This does NOT work for a complex polygon.
///
/// points that define the polygon
/// centroid point, or PointF.Empty if something wrong
public static PointF GetCentroid(List poly)
{
float accumulatedArea = 0.0f;
float centerX = 0.0f;
float centerY = 0.0f;
for (int i = 0, j = poly.Count - 1; i < poly.Count; j = i++)
{
float temp = poly[i].X * poly[j].Y - poly[j].X * poly[i].Y;
accumulatedArea += temp;
centerX += (poly[i].X + poly[j].X) * temp;
centerY += (poly[i].Y + poly[j].Y) * temp;
}
if (Math.Abs(accumulatedArea) < 1E-7f)
return PointF.Empty; // Avoid division by zero
accumulatedArea *= 3f;
return new PointF(centerX / accumulatedArea, centerY / accumulatedArea);
}