How round is your shape?
YouTuber webgoatguy uploaded a video comparing ways to measure the ‘circularity’ of a shape. The goal is to assign shapes a value from 0 to 1, where 1 is a perfect circle. This inspired me to come up with my own ideas for measuring circularity.
Let me recap a few of the current methods.
Isoperimetric ratio. The ratio of the square of a shape’s perimeter to its area . A circle minimises this at , so we can calculate to score the shape. Pro: Easy to calculate. Con: By introducing some wrinkles, shapes can look arbitrarily close to a circle yet score very low.
Maybe this is what you want, but I claim that these are all pretty circular.
Inradius over circumradius. The ratio of the radii of the largest circle contained inside the shape to the smallest circle that contains the shape. Pro: Also intuitive, fixes the perimeter problem. Cons: Only the innermost and outermost parts matter. The region bounded between the inner and outer circles is a ‘dead zone’: modifying the shape in this region does not change its circularity. It can also be difficult to calculate the inradius.
Min over max distance from boundary to a chosen origin. We could choose a point to maximise this value, but this is hard to find in practice. The easiest thing is to just choose the centroid. Similar issue to the previous one, easier to calculate but a little more arbitrary.
Intersection over union. Used in computer vision for measuring similarity of shapes. Calculate , where is the shape and is a comparison circle. Ideally we would choose the circle that maximises IoU, which again is not necessarily easy to compute, but we can approximate with the centroid and either choose a radius that maximises IoU or pick the circle with the same area as .
Other approaches have their own issues: methods involving arc-length parameterisation fail for fractals, and parameterisation with respect to angle only works if the shape is star-convex (meaning some point has direct line of sight to all points). Even making use of a unique ‘inside’ assumes that the boundary is not self-intersecting.
A new approach
Let’s think about this intuitively. When I imagine a shape becoming more circular, I imagine how its edges would round out as it is compelled to assume a smoother and smoother shape. To me, this suggests some kind of energy field. The circularity would then correspond to the potential energy, i.e. how much the points are pulled along their trajectory towards the circle.
Now that’s a circle.
The solution: second polar moment of inertia
The pairwise potential energy of a region is defined as
where is the potential function. There are plenty of potential functions we could choose, but let’s choose . This means that the shape acts as a cloud of points where every pair of points is connected by a spring. Since the potential energy for a fixed area is minimised by a circle, we can compare the potential energy of to the potential energy of a circle, and use the ratio to calculate a score.
How do we calculate this? Well, since
we can split up this integral. If we place the origin at the centroid of the shape, then the cross-term becomes
and the other terms equal , where is the area and is the second polar moment of inertia. It is quite easy to calculate , again because of the separability of .
The polar moment of inertia of a circle is . So the relative score is
Reparameterising
The values are a bit high—a square scores 0.955. Claude suggested reparameterising by , which ensures that a perturbation of a circle causes a change in score that is linear with respect to amplitude.
Example
Below you can try this out. Draw a shape and see how circular it is, or look at the example shapes.
And that’s all… right? In the words of DJ Khaled:
Another one
During a discussion, it was suggested to me that this still might not capture an intuitive idea of ‘circularity’. The polar moment of inertia approach doesn’t care about shape at all—only the positions of the points matter. Here are two ‘worms’ of the same width and length, but one is coiled up. Do we really want to say that the coiled worm is less circular than the straight worm? If we made the coiled worm follow the field lines from before, we would find the layers ‘merging’ together into an indistinct blob, paying absolutely no heed to the boundaries separating different layers of the coil.
How can we take the shape into account, but be flexible enough to allow for this kind of ‘bending’?
Fire and skeletons
Imagine a field of grass. Your local arsonist pours gasoline around the outside and sets fire to the entire perimeter simultaneously. As the fire spreads from the outside in, it eats away at the perimeter until the ‘wave fronts’ meet in the middle. The time it takes for a particular blade of grass to get burned is precisely its distance to the boundary. The points where two wave fronts meet (that is, the points without a unique nearest boundary point) form the skeleton, or medial axis.
There is a lot of information to be gleaned from the skeleton. In fact, if you know the skeleton and you know the distance from each point of $X to the boundary (the distance function), you can reconstruct the original shape!
Notice that we can deform the skeleton while keeping the distance function. For example, the two worms from before have the same distance function but warped skeletons. By comparing their skeletons and distance functions, we can see that they are deformed versions of the ‘same shape’.
Ok, so how do we use this? The grassfire transform of a shape is the function that maps a point to its distance from the boundary of . This is effectively a Signed Distance Field (SDF) computed inside the shape. The integral is maximised uniquely by the circle out of all shapes of fixed area. There is a nice way to see this: As the fire eats away at , it burns the whole perimeter at once, simultaneously shrinking the perimeter. The circle has the smallest perimeter relative to its area, so it takes a relatively long time to burn a particular patch of grass in expectation. (This is a good life lesson—if you are ever stuck in place while being attacked by an arsonist who is afraid of standing in grass, you will probably live longest in a circular field!)
Now, as before, our circularity measure compares the shape against the circle. With the circle’s value ,
We see that both of the worms have the same circularity now.
An interesting effect of this measure is that outward spikes don’t change the circularity much, but inward spikes change it a lot. This is because a circle with a cavity creates much faster ‘burning’ from the inside out (equivalently, it can be ‘straightened out’ into a tube), while a spike burns quickly without changing the area much. Whether that makes sense depends on your preference.
Computing
I chose to integrate because I’m numerically integrating using grid sampling, so it doesn’t make a difference whether I use or .
However, integrating is directly related to the first approach: both find the integral over square distance, but the first is with distance to the centroid and the second is with distance to the boundary.
Unfortunately, the grassfire transform approach is not so easy to compute exactly. In the first approach we could use Green’s Theorem to convert it to an integral across the boundary, which we can then calculate for a polygon with a shoelace method. For the grassfire approach, the square distance to boundary is not differentiable, so we can’t apply Green’s Theorem across the entire boundary. However, we can break up the polygon into a generalised Voronoi tiling, where within each cell every point shares the same nearest boundary feature (edge or vertex). Now the function is both smooth and separable within this region.
I have not done the Voronoi tiling, because it is very difficult to get working correctly, although there are O(n) algorithms.
It’s really easier to just approximate it with numerical integration. There are various methods to quickly compute a rasterised Signed Distance Field inside a shape, and then you can just add up all the values.
Example
Ok, here is the same program as before but using the grassfire circularity score. See which one you prefer!
So, that’s it. Two scoring strategies for comparing shapes to a circle. Is any of this useful? Probably not, but I learned some interesting things along the way and I hope you did too.
Other circularity functions I thought about
- Fourier coefficient energy. Add up the square norms of all the Fourier coefficients of order at least 2. This seems to underweight the details of shapes.
- Logarithmic capacity. The total charge a region can hold while maintaining a given potential. Fun but hard to calculate and it underweights inward spikes. Its counterpart, the conformal radius, underweights outward spikes.