In this post we look at how to calculate the centroid of an arbitrary polygon with n-vertices.
Polygon with n-vertices
Consider a polygon with n-vertices which is bounded by n edges.
The edges of the polygon may be expressed as follows.
\[\begin{cases}x = x_i + (x_{i+}-x_i)\,t \\ y = y_i + (y_{i+}-y_i)\,t \end{cases}\quad \mbox{with} \quad \begin{cases} i = 0,1,2,\cdots,n-1 \\ i+=i+1\mod n \end{cases}\quad \mbox{and} \quad 0 \le t \le 1\]
We know the formula of the centroid is given by:
\[\vec C = \frac{1}{A}\int _A \vec r\, dA= \frac{1}{A}\iint \vec r \,dx\,dy=\frac{1}{A}(m_x,m_y)=(C_x, C_y)\]
We know how to calculate A. As for the sum of the first moment of area, we can convert the area integral into a line integral by employing Green's theorem, which states that:
Green's Theorem
Let C be a positively oriented, piecewise smooth, simple closed curve in a plane, and let D be the region bounded by C. If L and M are functions of (x, y) defined on an open region containing D and have continuous partial derivatives there, then
\[\iint \left( \frac{\partial M}{\partial x} - \frac{\partial L}{\partial y} \right) dx\,dy= \oint \left( L\,dx + M\,dy \right)\]
By substitution of \(M(x,y)=x\) and \(L(x,y)=0\) Green's Theorem becomes:
\[\iint x\,dx\,dy = \oint \frac{1}{2}x^2\,dy\]
which can be used for solving the centroid equation above.
Calculating the Centroid
x-coordinate of the centroid
By reference to the Green's therem, we can say that:
\[m_x = \iint x\,dx\,dy = \oint \frac{1}{2}x^2 \,dy\]
We need to perform the line integral around the perimeter of the polygon, edge by edge.
We have defined the y-coordinate of each edge as:
\[y = y_i + (y_{i+}-y_i)\,t\]
Based on which we can say that:
\[\frac{dy}{dt} = (y_{i+}-y_i)\]
It follows that:
\[m_x = \sum_{i=0}^{n-1}\frac{1}{2}\int_0^1\left[x_i + (x_{i+}-x_i)\,t\right]^2\,(y_{i+}-y_i)dt\]
Which we can start expanding as follows:
\[\begin{equation}\begin{split} m_x &= \frac{1}{2}\sum_{i=0}^{n-1}\int_0^1(y_{i+}-y_i)\left[x_i^2 + (x_{i+}-x_i)^2t^2+2x_i(x_{i+}-x_i)t\right]\,dt\\&=\frac{1}{2}\sum_{i=0}^{n-1}(y_{i+}-y_i)\left[x_i^2t + \frac{1}{3}(x_{i+}-x_i)^2t^3+x_i(x_{i+}-x_i)t^2\right]_0^1\\&=\frac{1}{2}\sum_{i=0}^{n-1}(y_{i+}-y_i)\left[x_i^2 + \frac{1}{3}(x_{i+}-x_i)^2+x_i(x_{i+}-x_i)\right]\\&=\frac{1}{6}\sum_{i=0}^{n-1}(y_{i+}-y_i)\left[x_{i+}^2 +x_ix_{i+} +x_i^2\right]\\\end{split}\end{equation}\]\[m_y = \iint y\,dx\,dy = \oint -\frac{1}{2}y^2 \,dx = -\frac{1}{2}\sum_{i=0}^{n-1}(x_{i+}-x_i)\int_0^1\left[y_i + (y_{i+}-y_i)\,t\right]^2\,dt\]
The last expression is a telescoping sequence as below:
\[\begin{equation}\begin{split}y_1(x_1^2+x_1x_0+x_o^2)-y_o(x_1^2+x_1x_0+x_0^2)\\+y_2(x_2^2+x_2x_1+x_1^2)-y_1(x_2^2+x_2x_1+x_1^2)\\\cdot\,\cdot\,\cdot\,\cdot\,\\+y_n(x_n^2+x_nx_{n-}+x_{n-}^2)-y_{n-}(x_n^2+x_nx_{n-}+x_{n-}^2)\\\end{split}\end{equation}\]
which in turn can be expressed as:
\[\begin{equation}\begin{split}y_1x_1^2+y_1x_1x_0+y_1x_0^2-y_0x_1^2-y_0x_1x_0-y_0x_0^2\\+y_2x_2^2+y_2x_2x_1+y_2x_1^2-y_1x_2^2-y_1x_2x_1-y_1x_1^2\\\cdot\,\cdot\,\cdot\,\cdot\,\\+y_nx_n^2+y_nx_nx_{n-}+y_nx_{n-}^2-y_{n-}x_n^2-y_{n-}x_nx_{n-}-y_{n-}x_{n-}^2\\\end{split}\end{equation}\]
The first and last term of each line cancel themselves out, leaving you with:
\[\frac{1}{6}\sum_{i=0}^{n-1}(y_{i+}x_ix_{i+}+y_{i+}x_i^2-y_ix_{i+}^2- y_ix_i x_{i+})\]
which can then be simplied to:
\[\frac{1}{6}\sum_{i=0}^{n-1}(y_{i+}x_i-y_ix_{i+})(x_i+x_{i+})\]
y-coordinate of the centroid
It is seen that everything is the same if we just exchange x and y, except for the minus sign, hence:
\[\begin{equation}\begin{split}m_y &= -\frac{1}{6}\sum_{i=0}^{n-1}(x_{i+}y_i-x_i y_{i+})(y_i+y_{i+})\\&=\frac{1}{6}\sum_{i=0}^{n-1}(x_iy_{i+}-x_{i+}y_i)(y_i+y_{i+})\end{split}\end{equation}\]
Useful References
Centroid - Wikipedia
geometry - Why doesn't a simple mean give the position of a centroid in a polygon? - Mathematics Stack Exchange