Build graph with segment ends as vertices and segments as edges, then find cycles using DFS.
Note that the same edges might belong to multiple cycles (polygons) like your 2-5
and there might be many variants to select cycles. To exclude ambiguity, you can build fundamental set of cycles
Edit. As weston noticed in comments, resulted polygons might contains others. So sketch of more geometric approach:
Build lists of adjacency for graph.
Sort edges in ever list by polar angle.
Choose the most bottom vertex A.
If it's degree is 0, remove vertex, if 1, remove vertex and edge.
Otherwise get edge E with the smallest angle from this vertex.
Walk to pair vertex B.
Choose the most left edge F from B.
Move along to F edge into C.
If dead end, remove F and vertex C and return to B.
Repeat moving using left-hand rule until meet vertex A or dead end vertex.
In the walk process remove edges outgoing from vertices of degree 2 or mark them as used.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…