mapbox / polylabel

A fast algorithm for finding the pole of inaccessibility of a polygon (in JavaScript and C++)
Other
1.44k stars 151 forks source link

How to apply algorithm to SVG, to find if Path 1 is in Path 2 ❓ #98

Closed NMC92 closed 2 years ago

NMC92 commented 2 years ago

Hi, I was wondering if it is possible to apply this algorithm when calculating the pole of inacessibility of an SVG path. I'm doing this to build a hierarchy between SVG paths, in a parent-child manner. I've tried checking if point[cx(), cy()] is inside the parent path, but that doesn't guarantee the point is inside both parent and child paths, especially if the child path has an irregular shape, like a 'U'. I've tried using the CLI algorithm on GEOJson files and, while it does work, when I export to .SVG on mapshaper.com the svg file returns empty. I've tried using a polyfill for GetPathData to obtain all the points of the path, to try emulating a polygon, but I don't know if it really works lilke that because the code freezes up on me. And I've also tried checking if, for all points of path 2, they're either inStroke or inFill of path 1, but that's rather time exhaustive. I don't know how to reliably apply this when it comes to svg paths. All help would be appreciated. Ideally, the pole of inacessibility would be in the fill of the path, and the goal would be to find the P of I in the fill of the path 2. Thanks!

NMC92 commented 2 years ago

I managed to fix my problem with polylabel and geojson2svg. I aditionally set the precision to 0.000001.