I'm parsing some data, given as an array of line segments that describe several closed, arbitrary shapes/polygons. These shapes can be concave. Here's a simplified example of what I'm looking at:
However, the data I'm provided has the segments in arbitrary order. Per the example, my data would be something like {V,E,D,X,U,A,Z,C,B,W,Y}
. So, plotting the segments displays the correct shapes, but doing any operations on the shapes doesn't get any easier.
I am trying to sort the array above so that each closed shape's segments follow in connecting order, and each shape's segments are grouped together.
So
{V,E,D,X,U,A,Z,C,B,W,Y}
would become
[ {A,B,C,D,E} , {X,Y,Z} , {U,V,W} ]
The ordering of each group of line segments doesn't matter to me, only that the individual segments are in order. I also do not care about the particular starting segment of each group.
So that
[ {Y,Z,X} , {C,D,E,A,B} , {W,U,V} ]
is an equally valid outcome.
I'm not experienced with traversing geometry, and my rudimentary attempts and cursory online searches didn't yield any quick solutions. I looked into concave hulls, but that seems overkill given the data already knows the connections between the points.
After a day of head-wrangling, experimenting with suggestions here, and writing some helper classes, I came up with something pretty conventional. In rough pseudo-code, my solution is:
The code relies on the assumptions that my data contains only closed shapes (i.e. each shape's data neatly terminates on the same exact coordinates), and that all data creates shapes, not just orphan lines. I'm not sure how efficient this is, but given that this is used once as a pre-processing step, it's sufficient.