Geometry – Finding the Visual Center of a Concave Polygon

geometry

I have an area I want to place a label within. I'm currently finding the centroid and placing the label there. In the case of a concave polygon, though, often the centroid is actually outside the boundary. In this case, I want to correct the position of the label and place it roughly in the "visual center" of the polygon. This is tricky because I don't even know what I would define as the visual center. Perhaps somewhere halfway along the "skeleton".

Is there some kind of calculation I can do? It doesn't have to be the absolute center – just somewhere roughly "in the middle" under some definitions. I've already pre-calculated the centroid if that's useful.

Best Answer

The point you're looking for is the Pole of Inaccessibility of the polygon. It's commonly used to position labels within maps.

One possible approach is the "Polylabel" algorithm, which uses quadtrees to subdivide the polygon until the point can be found:

Polyline algorithm using quadtrees

The company Mapbox (note: no affiliation) has released an implementation under the ISC License: https://github.com/mapbox/polylabel.

Related Question