Title: Find the centroid of a polygon in C#
The centroid of a polygon is its "center of mass." If you were to cut the polygon out of cardboard or wood, the centroid would be the point where you could balance the polygon on a pin. Note that the centroid does not necessarily lie on the polygon (on a donut shape the centroid would be in the central hole), so you might need to glue the polygon to a piece of transparent plastic and place the pin on the plastic.
The formula for the centroid (X, Y) is given by:
X = Σ[(Xi + Xi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A
Y = Σ[(Yi + Yi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A
Here A is the area of the polygon and the sum is taken over all of the adjacent pairs of points.
// Find the polygon's centroid.
public PointF FindCentroid()
{
// Add the first point at the end of the array.
int num_points = Points.Length;
PointF[] pts = new PointF[num_points + 1];
Points.CopyTo(pts, 0);
pts[num_points] = Points[0];
// Find the centroid.
float X = 0;
float Y = 0;
float second_factor;
for (int i = 0; i < num_points; i++)
{
second_factor =
pts[i].X * pts[i + 1].Y -
pts[i + 1].X * pts[i].Y;
X += (pts[i].X + pts[i + 1].X) * second_factor;
Y += (pts[i].Y + pts[i + 1].Y) * second_factor;
}
// Divide by 6 times the polygon's area.
float polygon_area = PolygonArea();
X /= (6 * polygon_area);
Y /= (6 * polygon_area);
// If the values are negative, the polygon is
// oriented counterclockwise so reverse the signs.
if (X < 0)
{
X = -X;
Y = -Y;
}
return new PointF(X, Y);
}
This method copies the polygon's points into an array and repeats the first point at the end of the array to make looping through the polygon's segments easier. It then loops through the segments and adds up the X and Y terms shown in the earlier equation. It divides the totals by 6 times the area returned by the PolygonArea method (described in the post Calculate the area of a polygon in C#). Finally the method returns a new PointF representing the calculated point.
The example program marks the centroid with a yellow circle.
Download the example to experiment with it and to see additional details.
|