Why is a formula for centroid of polygon not working for triangle?

1.8k Views Asked by At

I have a situation where I have to calculate the centroid of arbitrary polygons, the coordinates of whose vertices are given. The polygons can be triangles, quadrilaterals or pentagons. The only thing I know in advance is that they are all convex and non-self-intersecting.

After a lot of searching (on the internet), I decided to settle on the formula that Wikipedia provides.

enter image description here

When I tried to apply this formula for a triangle, the centroid it found sometimes tended to lie outside the triangle, and it never equaled the coordinates of the centroid I found by averaging the vertices of the triangle.

It is true that I don't know enough about the formula itself and could not understand it better after I looked up the reference Wikipedia supplied. Are there some assumptions that would make the formula inapplicable for the case of a triangle? Or, is there something I'm doing wrong entirely and are the centroids generated for the pentagons wrong as well? As far as I have read, I only need to ensure that the vertices are ordered counter-clockwise or clockwise.

1

There are 1 best solutions below

1
On BEST ANSWER

You must be making a mistake in applying the formula, because it is easy to show that the centroid comes out right for a triangle. I will show it for $C_x$; $C_y$ is similar.

Start with (per the formula) $$ \frac{1}{6A} \left( (x_0+x_1)(x_0 y_1 - x_1 y_0) + (x_1+x_2)(x_1 y_2 - x_2 y_1) + (x_2+x_0)(x_2 y_0 - x_0 y_2) \right) $$ Now add zero: $$ 0 = \frac{1}{6A} \left( (x_0 x_1 y_2 - x_1 x_0 y_2) + (x_1 x_2 y_0 - x_2 x_1 y_0) + (x_2 x_0 y_1 - x_0 x_2 y_1) \right) = \frac{1}{6A} \left( x_2 (x_0 y_1 - x_1 y_0) + x_0 (x_1 y_2 - x_2 y_1) + x_1 (x_2 y_0 - x_0 y_2) \right) $$ and group the terms in the obvious way so that the formula becomes $$ \frac{1}{6A} \left( (x_0+x_1+x_2)(x_0 y_1 - x_1 y_0) + (x_1+x_2+x_0)(x_1 y_2 - x_2 y_1) + (x_2+x_0+x_1)(x_2 y_0 - x_0 y_2) \right) =(x_0+x_1+x_2) \frac{(x_0 y_1 - x_1 y_0) + (x_1 y_2 - x_2 y_1) + (x_2 y_0 - x_0 y_2)}{6A} $$ and now we can insert the formula for A $$ 6A = 3 \left( (x_0 y_1 - x_1 y_0) + (x_1 y_2 - x_2 y_1) + (x_2 y_0 - x_0 y_2) \right) $$ so the formula gives $$ C_x = \frac{1}{3}(x_0+x_1+x_2) $$ which matches the averaging definition of the centroid.