I have two convex polygons. Polygons are implemented as cyclic lists of their vertices. How to find an intersection of this two polygons?
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.).