Para Parasolian left a comment on my post about computing the area of a polygon, suggesting that I “say something similar about computing the centroid of a polygon using a similar formula.” This post will do that, and at the same time discuss vectorization.
We start by listing the vertices starting anywhere and moving counterclockwise around the polygon:
It will simplify notation below if we duplicate the last point:
The formula the centroid depends on the formula for the area, where the area of the polygon is
Hadamard product and dot product
We can express the area formula more compactly using vector notation. This will simplify the centroid formulas as well. To do so we need to define two ways of multiplying vectors: the Hadamard product and the dot product.
The Hadamard product of two vectors is just their componentwise product. This is a common operation in R or Python, but less common in formal mathematics. The dot product is the sum of the components of the Hadamard product.
If x and y are vectors, the notation xy with no further explanation probably means the dot product of x or y if you’re reading a math or physics book. In R or Python,
x*y is the Hadamard product of the vectors.
Here we will use a circle ∘ for Hadamard product and a dot · for inner product.
Now let x with no subscript be the vector
and let x‘ be the same vector but shifted
We define y and y‘ analogously. Then the area is
The formula for the centroid in summation form is
where A is the area given above.
We can write this in vector form as
You could evaluate v = x∘ y‘ – x‘∘y first. Then A is half the dot product of v with a vector of all 1’s, and the centroid x and y coordinates are inner products of v with x + x‘ and y + y‘ respectively, divided by 6A.