Intersection of two convex polygons

后端 未结 3 798
执笔经年
执笔经年 2020-12-29 09:30

I have two convex polygons. Polygons are implemented as cyclic lists of their vertices. How to find an intersection of this two polygons?

3条回答
  •  -上瘾入骨i
    2020-12-29 09:40

    You can benefit from the fact that both polygons are convex. And with this knowledge you can achieve O(n) time by using the followin sweep line algorithm:

    Find the topmost points in both polygons. For simplicity suppose you have no horizontal edges. Create lists of edges contributing to the Left and Right boundaries of a polygon.

    While sweeping down the plane you store 4 edges. left_edge_C1, right_edge_C1, left_edge_C2, right_edge_C2. You don't need any complex to strore the edges, because there are only four of them. You can find next event point just by iterating all possible options.

    At each event point some edge appear on the boundary of their intersection. Basically, at each event point you can have one of three cases (see the pic.).

    enter image description here

提交回复
热议问题