I need to identify adjacent shapes in an SVG drawing where the definition of adjacent is: two shapes A and B are adjacent if more than one point on the path defining shape A is closer than e (an arbitrary threshold) to a point on shape B.
See image below - shape 1 is adjacent to shape 2, and shape 2 is adjacent to shape 3, while shapes 1 and 3 are not adjacent.
The image represents a tile, the production process of which does work if two adjacent shapes are made from the same color. For the curious, see the video for a look at the production process.
Short of coding things by hand, how can I check if two shapes are adjacent, so that I can ensure that (in an app) they won't be colored in the same way?
I have thought of 2 approaches:
- enlarge the shapes by a certain amount (by offsetting them, for example) and then see if they overlap with each other
- divide the image in a grid of small squares and check if any square contains more than shape
I expect that grid size or offset parameters will have to be set carefully to avoid that shapes 4 and 5 are identified as adjacent.
Is there a better approach than these two?
I plan to build this in SVG + javascript, using paper.js for calculating overlaps.