How to determine if two overlap areas are contained in a set of rectangles?

struct Rect
{
double left, right, top, bottom;
};

std::vector<Rect> vec;

we now have N (N> 1000) rectangles, which efficient algorithm determines if any of the two of them overlap?

updates: all of these rectangles are parallel to the coordinate system.

+4
source share
4 answers

You can imagine a rectangle using two segments : open the segment (x1, y1) to (x1, y2) and close the segment (x2, y1) to (x2, y2) with x1 <x2 and y1 <y2.

First, we can sort all these segments O (nlogn) times by the x coordinate .

-, , , (y1, y2) , , . , , , , . O (logn).

, O (nlogn).

+5

, . , , H, ditto V . H V , , 2D .

2D , O (n log n), , - .

0

, D * D, , :

std::vector<Rect*> grid[D][D];

, 20x20, 1000+ , . , D , , 5-50 vector ( , ), .

:

  • nullptr , - ,
  • - , , , vector empty()

( , , , / , .., , , , . , , ( ). , , true/false --- .)

0

, RH GΓΌting W Schilling, " ", Information Sciences 42 (1987).
  ( ), , , and-conquer , "-", : " , , , ?"

0

All Articles