What is the fastest way to find the “visual” center of an irregularly shaped polygon?

I have found a very good solution to this from MapBox called Polylabel. The full source is available on their Github too.

Essentially it tries to find the visual centre of the polygon as T Austin said.

enter image description here

Certain details suggest this may be a practical solution:

Unfortunately, calculating [the ideal solution ] is both complex
and slow. The published solutions to the problem require either
Constrained Delaunay Triangulation or computing a straight skeleton as
preprocessing steps — both of which are slow and error-prone.

For our use case, we don’t need an exact solution — we’re willing to
trade some precision to get more speed. When we’re placing a label on
a map, it’s more important for it to be computed in milliseconds than
to be mathematically perfect.

A quick note about usage though. The source code works great for Javascript out of the box however if you intend on using this with a “normal” polygon then you should wrap it in an empty array as the functions here take GeoJSONPolygons rather than normal polygons i.e.

var myPolygon = [[x1, y1], [x2, y2], [x3, y3]];
var center = polylabel([myPolygon]);

Leave a Comment