How to find polygons in a given set of points and edges?

前端 未结 1 744
生来不讨喜
生来不讨喜 2020-12-12 00:10

Consider the following problem:

Given N points in plane and M line segments connecting them, find all polygons (convex or concave) that do not contain any other poly

相关标签:
1条回答
  • 2020-12-12 01:16

    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.

    0 讨论(0)
提交回复
热议问题