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.

## Notation

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

## Centroid formula

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 6*A.*

Interesting ,would this describe

A moving average of a polygon?

You probably know this, but using this technique you can easily get the higher moments of a polygon too. I use it all the time for a “moment of inertia” of a polygon – if we assume that it’s a thin sheet of even density.

I’ve used it for a fast approximation to a minimum bounding box – diagonalize the moment of inertia tensor to get the principle axes and then assume that the minimum bounding box is aligned with the principle axes. In my case, it was sufficient for all but the most pathological polygons and much faster than trying to find the true minimum bounding box.

Small typo twice: Cx instead of Cy.

Thanks. Fixed.