Intersection of two convex polygons

耗尽温柔 提交于 2019-11-29 08:41:17

问题


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


回答1:


For each edge V1-V2 in the first polygon,
    Let H := Half-plane tangenting V1-V2, with the remaining
        vertices on the "inside".
    Let C := New empty polygon.
    For each edge V3-V4 in the second polygon,
        Let X := The intersection between V3-V4 and H.
        If V3 inside H, and V4 is outside H then,
            Add V3 to C.
            Add X to C.
        Else if both V3 and V4 lies outside H then,
            Skip.
        Else if V3 outside H, and V4 is inside H then,
            Add X to C.
        Else
            Add V3 to C.
    Replace the second polygon with C.

This should suffice for simple usage; 10-20 vertices and not recalculating every frame. — O(n2)


Here is a few links:

  • Computer Graphics I – Polygon Clipping and Filling (pdf)
  • rosettacode.org – Sutherland-Hodgman polygon clipping



回答2:


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.).




回答3:


Besides @Yola's nice plane-sweep description, there is a linear-time algorithm described in Computational Geometry in C, Chapter 7. And C & Java code is available (at the same link). There are several tricky degenerate cases, e.g., when the two polygons intersect at a point, or the intersection is a segment.



来源:https://stackoverflow.com/questions/13101288/intersection-of-two-convex-polygons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!