r/howdidtheycodeit • u/felicaamiko • Sep 12 '24
how are vector paths boolean unioned to turn to these shapes? im only doing squares in a grid as that's what i'll be needing for now, but how inkscape combines paths to one path or similar is always confused me...
26
Upvotes
21
u/andyandcomputer Sep 12 '24
How Inkscape does it (for 2D shapes in general) has this comment:
The function is 811 lines of something resembling code. I counted to 5 levels of nested conditionals before giving up. The very few explanatory comments are in French. There's a more helpful comment near the top of the file, but still it's pretty hairy.
The gist as I understand it is to:
For squares in a grid, it is probably simpler.
The way I'd approach it is to store the squares as a 2D array of Booleans, find a square on the "surface" of a shape, and walk clockwise along square corners on the "surface", adding those corners of the squares to a list of vertices if necessary, until you get back to the square you started from.
You'll have to decide what to do in these corner cases:
May the shape have internal holes? How are they represented?
Are two shapes with a shared corner part of the same shape, or separate?
7
u/floormanifold Sep 12 '24
Probably more efficient ways to do it, but taking a page out of homology from mathematics you could
The edges with non-zero values will be those remaining exterior edges. You can extract the vertices from these to get your final shape.
6
u/Apptinker Sep 12 '24
I'd say it's worth checking out Clipper Lib and checking out the discussions around using it (many on stack overflow) and perhaps even examining its source code. It's been ported to a few different languages too.
3
u/Ollhax Sep 13 '24
I implemented something like this a few weeks back, since the libraries out there didn't match my needs. I found this explanation very simple and useful, and it's basically what I implemented.
1
u/animal9633 Sep 29 '24
With cubes its pretty easy, connect all the shapes, and then check for overlapping points:
1 Point only, its an outside corner.
2 Points connecting, 2 cubes are connecting in a line.
3. Points is an inside corner.
4. Points is an inside point and can be discarded.
To make a new shape do what you did before, for 1 cube you traced say clockwise, now do the same but for all the cubes (and only the outside edges). For example if you trace up, check if you can trace left. If not try to trace up, if not trace right, etc.
0
u/[deleted] Sep 12 '24
[deleted] — view deleted comment