There is a set of 2D polylines, each polyline defined by an ordered array of vertices (each vertex is joined with the previous one by a line, if drawn). Together, when drawn, these polylines form one or more contours, but they can be fragmented (a single contour consisting of several polylines)
The task is to build new polylines, based on the original points, so that the new ones form only "outermost" contours. This is illustrated by the pictures. The first one is the original set of poly lines, each shown with a different color. The second picture is what I'd like to have: 4 arrays-polylines (4 contours)
I need to build an algorithm to transform the first pic into the second one. I suppose there should be a simple solution but not sure which side to tackle this task from. How to approach this? Thanks for any clues!

