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.