Intersection of two convex polygons

后端 未结 3 801
执笔经年
执笔经年 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条回答
  •  感情败类
    2020-12-29 09:49

    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

提交回复
热议问题