Intersection of two convex polygons

DaZzz picture DaZzz · Oct 27, 2012 · Viewed 13.3k times · Source

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

Answer

Markus Jarderot picture Markus Jarderot · Oct 28, 2012
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: