Intersection of N rectangles

前端 未结 6 523
野趣味
野趣味 2020-12-25 15:18

I\'m looking for an algorithm to solve this problem:

Given N rectangles on the Cartesian coordinate, find out if the intersection of those rectangles is empty or no

6条回答
  •  伪装坚强ぢ
    2020-12-25 15:50

    Observation 1: given a polygon A and a rectangle B, the intersection A ∩ B can be computed by 4 intersection with half-planes corresponding to each edge of B.

    Observation 2: cutting a half plane from a convex polygon gives you a convex polygon. The first rectangle is a convex polygon. This operation increases the number of vertices at most per 1.

    Observation 3: the signed distance of the vertices of a convex polygon to a straight line is a unimodal function.

    Here is a sketch of the algorithm:

    Maintain the current partial intersection D in a balanced binary tree in a CCW order.

    When cutting a half-plane defined by a line L, find the two edges in D that intersect L. This can be done in logarithmic time through some clever binary or ternary search exploiting the unimodality of the signed distance to L. (This is the part I don't exactly remember.) Remove all the vertices on the one side of L from D, and insert the intersection points to D.

    Repeat for all edges L of all rectangles.

提交回复
热议问题