# How can I calculate the centroid of polygon?

What is the way to calculate the centroid of polygon? I have a concave polygon of 16 points, and I want know the centroid of that.

thanks

-
You've seen this? –  Guess who it is. Dec 11 '11 at 14:09
As for a Mathematica routine: PolygonCentroid[pts_?MatrixQ] := With[{dif = Map[Det, Partition[pts, 2, 1]]}, ((ListConvolve[{1, 1}, #] & /@ Transpose[pts]).dif)/(3 Total[dif])]. Maybe somebody can do better than me... –  Guess who it is. Dec 11 '11 at 14:19
thanks but i will be delighted if u could a bit explain me thx –  Csabi Dec 11 '11 at 14:22
Explain what? The formula? The code? You're a bit vague... –  Guess who it is. Dec 11 '11 at 14:30

## 2 Answers

I. CONSIDERATIONS.
(In 2 parts. The formulas are at the end of each.)

Case: Point List

I have sixteen marbles of equal mass. Let us agree they all lie in one plane.

Q1: What is the center of mass of the set of marbles?

The center of mass of a sphere is its center. So let the coordinates of the marble centers be:

$\;\;\;\;\;Pts = A_1, A_2.... A_{16}.$

To find the centroid C, average them:

$(1)\;\;\;\;\;C =(A_1+A_2+....+A_{16})/16.$

Case: Polygon

Now, using (Pts) as the vertices, I cut out a 16-sided polygon, Jerome.

Q2: What is the center of mass of Jerome?

This is not the same question. How can I ask it correctly? Consider (1) above. What has changed? I see two problems:

1. There are many concave polygons through 16 given points. Which did I cut out? I must uniquely identify the region (area) I want to measure. Here are two ways:

a) Break up Jerome into distinct, convex regions. If a set of points are the vertices of a convex polygon, that polygon is unique. Each convex region of Jerome is uniquely determined by its vertices.

b) Arrange Pts so that the order (A1 -> A2 -> A3 .... ->An) follows the perimeter in a single direction, without leaps or self-intersections. A unique perimeter is a unique polygon.

Both ways allow me to draw the figure. Note that the simple average (mean) does not distinguish order; it can't give the correct answer.

2. Jerome is a plane figure: Its mass is proportional to area. But "the mean of coordinates" is not the same thing as "proportional to area". Area of what? Again, the mean can't, in general, give an answer.

I now have a unique definition of the task. I have also discarded the simple average where it can't apply, without knowing the answer or doing any math. Going a bit further, it seems that the mean could be right if the vertices are equally distributed about a common center (this hunch turns out to be correct; e.g. regular polygons).

Now to solve the problem...

(A correct formula is given here: Why doesn't a simple mean give the position of a centroid in a polygon? , but I want to know how to obtain that formula.)

-

II. SOLVE THE PROBLEM

Why doesn't a simple mean give the position of a centroid in a polygon? leads to formulas.

Frown. I want to know how to set up and solve the problem. A solution can always be converted to a formula. The reverse --figuring out how a problem was solved by looking at the formula-- is very hard. So I will solve problem from scratch.

A working demonstration of the method below can be found here. The linked document is a Geogebra worksheet; feel free to download it, examine the code, and use it as you see fit.

A. Observation
The centroid of triangle $\Delta ABC$ is the simple average of its vertices:
$\;\;\;D = (A+ B + C)/3$.
What? Point order does not matter here: take the vertices A,B,C, in any order, they are the same triangle. To prove the formula is correct, I might Integrate the triangle area; Find the balance point of figure ABC; or, with compass and straightedge, draw the intersection of the medians. I say, the point is the same in every case.

B. Procedure
I will convert the problem into one I know how to solve:
Proposition: Find the centroid of a set of weighted points.

1. Chop Jerome up into mutually exclusive triangles.

2. Say there are m triangles. For each triangle $\;\Delta_k, \;k =1, 2, \ldots, m,\;$ find the centroid $C_k$, and the area (weight) $w_k$. The set of weighted points is then $[\frac{w_k C_k}{J}],$ where J is the area of Jerome.

3. Solution: Add 'em up. I say, the resulting point is the centroid of the polygon.

I have a general solution (I can always carry out this procedure). But it's messy: I don't have a triangulation rule. Now what? A good way to generalize is to start with a case I can solve.

C. Case: Jerome is Convex
It is commonly known that, given a convex polygon $P$, we may choose any vertex, $V,$ draw segments from $V$ to every non-adjacent vertex, and $P$ is correctly triangulated.

I will also use the following formulas:
-Let any two sides of $\Delta ABC$ be vectors ${\rm u =(u_1, u_2),\;\; v= (v_1, v_2)}.\;$ Then
$\;\;\;{\rm Area}_{\Delta ABC} = \tfrac{1}{2}|{\rm u \times v}|,\;\;\;\;\;$where (the determinant) ${\rm u \times v}={\rm u_1 v_2-u_2 v_1}$.

-Let sides $AB, AC$ of $\Delta ABC$ be vectors ${\rm u, v}.\;$ Then the centroid D= (A+B+C)/3 can be written
$\;\;\;D = A + \tfrac{1}{3}|{\rm u + v}|.$

I have gathered what I need.

D. Solution

1. Let the counterclockwise path of my n-gon be given, in order, by
$\;\;[A_i] = A_1, A_2, \ldots, A_n$
For covenience, I choose $V=A_1$.

2. Draw the n-1 vectors from A_1 to the other vertices:
$\;\;[{\rm a_i}] = (A_{k+1}-A_1),\;\; k = 1, 2, \ldots n\!-\!1$

3. I have n-2 adjacent triangles with centroids
$\;\;[C_i] = A_1 + \tfrac{1}{3}{\rm (a_k+a_{k+1})},\;\; k = 1, 2, \ldots n\!-\!2$
And areas (weights)
$\;\;[w_i] = \tfrac{1}{2}{\rm (a_k\times a_{k+1})},\;\; k = 1, 2, \ldots n\!-\!2$
(I dropped the absolute value: counter-clockwise, ${\rm u \times v}$ is positive.) Then
Total area = $\sum_{k=1}^{n-2} w_k$ and

4. The centroid $C_J$ of Jerome is the sum of weighted triangle 'roids, divided by total area:
$\;\;C_J= {\large \frac{\sum_{k=1}^{n-2} w_k C_k}{\sum_{k=1}^{n-2} w_k}}$,

which can be written

$$(2)\;\;C_J= A_1+{\large \frac{1}{3} \frac{\sum_{k=1}^{n-2} ({\rm a_k+ a_{k+1}})({\rm a_k \times a_{k+1}})}{\sum_{k=1}^{n-2} ({\rm a_k \times a_{k+1}})} }$$

I say, that this is in fact the complete solution: the determinants give signed areas: +/- according as the direction of rotation from $A_k$ to $A_{k+1}$, about $A_1$ is positive or negative, preserving measure in either case.

Being the thing to be done.

-