12

let's say I have a huge set of non-overlapping rectangle with integer coordinates, who are fixed once and for all

I have another rectangle A with integer coordinates whose coordinates are moving (but you can assume that its size is constant)

What is the most efficient way to find which rectangles are intersecting (or inside) A? I cannot simply loop through my set as it is too big. Thanks

edit : the rectangles are all parallel to the axis

Jack
  • 1,398
  • 3
  • 16
  • 26
lezebulon
  • 7,607
  • 11
  • 42
  • 73
  • You definitely have to check all and each rectangle. When to do so to make it fast, well that's another question... – m0skit0 Oct 11 '11 at 14:38
  • 4
    @DougT. Reminds me of a US immigration form question. Paraphrasing it - "Are you a terrorist?" :D – Ilian Oct 11 '11 at 14:39
  • 5
    Quad Trees: http://en.wikipedia.org/wiki/Quadtree – i_am_jorf Oct 11 '11 at 14:39
  • @ m0skit0 :I was thinking of using a variant of sweep and prune, I don't think I need to check all of the rectangles – lezebulon Oct 11 '11 at 14:39
  • @m0skit0 Spatial data structures come to the rescue here. The caveat is that you usually need to be searching a lot in order to amortize the cost of building the structure. – Mranz Oct 11 '11 at 15:11
  • 1
    Also try a search for "R-trees". – Nemo Oct 11 '11 at 16:30
  • 1
    So, the initial position will have to be searched with one of the described methods. However, once you have that position, if you have up/down/left/right pointers in your objects, you should be able to find the next intersection without searching again.. – Jan Oct 14 '11 at 08:13
  • Stupid person's algorithm (assumes 'superrectangles' don't collide]: I guess you could organize the a bunch of rectangles into groups that are bounded by a 'superrectangle'. Put those superrectangles into groups, and repeat until you have one mega superrectangle. Now, see if rectangle `A` is within bounds of any of the subrectangles. If it is, divide the superrectangle, and recheck. Repeat until there's nothing left. – Mateen Ulhaq Oct 15 '11 at 03:48
  • @muntoo You are basically describing a bounding volume hierarchy (In this case it would be Bounding Area Hierarchy though) – Mranz Oct 15 '11 at 22:55

12 Answers12

9

I'll bet you could use some kind of derivation of a quadtree to do this. Take a look at this example.

Pace
  • 41,875
  • 13
  • 113
  • 156
  • 1
    The example link is broken, can somebody restore it? Thanks, – abenci Mar 29 '20 at 21:53
  • @abenci The Wayback Machine has a snapshot of the page in question: http://web.archive.org/web/20111103111457/http://lab.polygonal.de/2007/09/09/quadtree-demonstration/ – Andreas Aug 11 '23 at 09:20
7

Personally, I would solve this with a KD-Tree or a BIH-Tree. They are both adaptive spatial data structures that have a log(n) search time. I have an implementation of both for my Ray Tracer, and they scream.

-- UPDATE --

Store all of your fixed rectangles in the KD-Tree. When you are testing intersections, iterate through the KD-Tree as follows:

function FindRects(KDNode node, Rect searchRect, List<Rect> intersectionRects)

// searchRect is the rectangle you want to test intersections with
// node is the current node. This is a recursive function, so the first call
//    is the root node
// intersectionRects contains the list of rectangles intersected

int axis = node.Axis;

// Only child nodes actually have rects in them
if (node is child)
{
    // Test for intersections with each rectangle the node owns
    for each (Rect nRect in node.Rects)
    {
        if (nRect.Intersects(searchRect))
              intersectionRects.Add(nRect);
    }
}
else
{
    // If the searchRect's boundary extends into the left bi-section of the node
    // we need to search the left sub-tree for intersections
    if (searchRect[axis].Min  // Min would be the Rect.Left if axis == 0, 
                              // Rect.Top if axis == 1
                < node.Plane) // The absolute coordinate of the split plane
    {
        FindRects(node.LeftChild, searchRect, intersectionRects);
    }

    // If the searchRect's boundary extends into the right bi-section of the node
    // we need to search the right sub-tree for intersections
    if (searchRect[axis].Max  // Max would be the Rect.Right if axis == 0
                              // Rect.Bottom if axis == 1
                > node.Plane) // The absolute coordinate of the split plane
    {
        FindRects(node.RightChild, searchRect, intersectionRects);
    }
}

This function should work once converted from pseudo-code, but the algorithm is correct. This is a log(n) search algorithm, and possibly the slowest implementation of it (convert from recursive to stack based).

-- UPDATE -- Added a simple KD-Tree building algorithm

The simplest form of a KD tree that contains area/volume shapes is the following:

Rect bounds = ...; // Calculate the bounding area of all shapes you want to 
              // store in the tree
int plane = 0; // Start by splitting on the x axis

BuildTree(_root, plane, bounds, insertRects);

function BuildTree(KDNode node, int plane, Rect nodeBds, List<Rect> insertRects)

if (insertRects.size() < THRESHOLD /* Stop splitting when there are less than some
                                      number of rects. Experiment with this, but 3
                                      is usually a decent number */)
{
     AddRectsToNode(node, insertRects);
     node.IsLeaf = true;
     return;
}

float splitPos = nodeBds[plane].Min + (nodeBds[plane].Max - nodeBds[plane].Min) / 2;

// Once you have a split plane calculated, you want to split the insertRects list
// into a list of rectangles that have area left of the split plane, and a list of
// rects that have area to the right of the split plane.
// If a rect overlaps the split plane, add it to both lists
List<Rect> leftRects, rightRects;
FillLists(insertRects, splitPos, plane, leftRects, rightRects); 

Rect leftBds, rightBds; // Split the nodeBds rect into 2 rects along the split plane

KDNode leftChild, rightChild; // Initialize these
// Build out the left sub-tree
BuildTree(leftChild, (plane + 1) % NUM_DIMS, // 2 for a 2d tree
          leftBds, leftRects);
// Build out the right sub-tree
BuildTree(rightChild, (plane + 1) % NUM_DIMS,
          rightBds, rightRects);

node.LeftChild = leftChild;
node.RightChild = rightChild;

There a bunch of obvious optimizations here, but build time is usually not as important as search time. That being said, a well build tree is what makes searching fast. Look up SAH-KD-Tree if you want to learn how to build a fast kd-tree.

Mranz
  • 1,260
  • 1
  • 12
  • 20
  • Ok I'll try that, but how exactly do you store rectangles in the tree? Am I supposed to consider them as punctual objects and store just the upper-left position? – lezebulon Oct 15 '11 at 11:17
  • There are quite a few ways to build it. I will update my answer with how to build the simplest form of it. – Mranz Oct 15 '11 at 22:34
  • @Mranz I have a question concerning **int axis = node.Axis** what exactly is *node.Axis** how do you compute it? and the part **FillLists(insertRects, splitPos, plane, leftRects, rightRects);** I didn't understand these 2 parts correctly. thank you. – Hani Goc Oct 27 '13 at 00:28
  • @HaniGoc The axis is the dimension that the node splits on. If the tree is 3 dimensional, it means that 0 -> x, 1 -> y, 2 -> z. The fill lists function adds each source rectangle into the leftRects or rightRects list if the rectangle is on that side of the splitting plane. If the rectangle straddles the plane, then it gets added to both lists. – Mranz Oct 28 '13 at 21:48
  • (Way back when I used a pair of priority search trees for the (non-overlapping - essential to this approach?) rectangles "stabbed".) – greybeard Nov 25 '16 at 10:25
2

You can create two vectors of rectangle indexes (because two diagonal points uniquely define your rectangle), and sort them by one of coordinates. Then you search for overlaps using those two index arrays, which is going to be logarithmic instead of linear complexity.

Slava
  • 1,528
  • 1
  • 15
  • 23
1

Topcoder provides a way to determine if a point lies within a rectangle. It says that say we have a point x1,y1 and a rectangle. We should choose a random point very far away from current locality of reference in the rectangular co-ordinate system say x2,y2.

Now we should make a line segment with the points x1,y1 and x2,y2. If this line segment intersects odd number of sides of the given rectangle (it'll be 1 in our case, this method can be extended to general polygons as well) then the point x1,y1 lies inside the rectangle and if it intersects even number of sides it lies outside the rectangle.

Given two rectangles, we need to repeat this process for every vertex of 1 triangle to possibly lie in the second triangle. This way we'd be able to determine if two rectangles overlap even if they are not aligned to the x or y axis.

user1071840
  • 3,522
  • 9
  • 48
  • 74
1

Interval Trees: Are BSTs designed with taking 'lo' value as key in an interval. So, for example if we want to insert (23, 46) in the tree, we'd insert it using '23' in the BST.

Also, with interval trees at each node, we keep the maximum endpoint (hi value) of the sub-tree rooted at that node.

This order of insertion allows us to search all 'R' intersections in R(logN) time. [We search for first intersection in logN time and all R in RlogN time] Please refer to interval trees documentation for how insert, search is done and details of complexity.

Now for this problem, we use an algorithm known as sweep-line algorithm. Imagine we have a vertical line (parallel to y-axis) which is sweeping the 2D space and in this process intersects with the rectangles.

1) Arrange rectangles in increasing order of x-cordinates (left-edge wise) either via priority queue or via sorting . Complexity NlogN if N rectangles.

2) As this line sweeps from left to right, following are the intersection cases:

  • If line intersects the left side of a rectangle never seen, add the y co-ords of the rectangle's side to the interval tree. [say (x1,y1) and (x1,y2) are left edge co-ordinates of the rectangle add interval (y1, y2) to the interval tree] ---> (NlogN)

  • Do a range search on the interval tree. [say (x1,y1) and (x1,y2) are left edge co-ordinates of the rectangle, take the interval (y1,y2) and do an interval intersection query on the tree to find all intersections] ---> RlogN (in practice)

  • If line intersects the right side of a rectangle, remove it's y-coords from the interval tree as the rectangle is now processed completely. ----> NlogN

Total complexity : NlogN + RlogN

user1071840
  • 3,522
  • 9
  • 48
  • 74
1

You can do a random "walking" algorithm ... basically create a list of neighbors for all your fixed position rectangles. Then randomly pick one of the fixed-position rectangles, and check to see where the target rectangle is in comparison to the current fixed-position rectangle. If it's not inside the rectangle you randomly picked as the starting point, then it will be in one of the eight directions which correspond to a given neighbor of your current fixed position rectangle (i.e., for any given rectangle there will be a rectangle in the N, NE, E, SE, S, SW, W, NW directions). Pick the neighboring rectangle in the closest given direction to your target rectangle, and re-test. This is essentially a randomized incremental construction algorithm, and it's performance tends to be very good for geometric problems (typically logarithmic for an individual iteration, and O(n log n) for repeated iterations).

Jason
  • 31,834
  • 7
  • 59
  • 78
  • 2
    _Very_ effective if the moving box moves slowly. Very ineffective if it teleports :( – Mooing Duck Oct 14 '11 at 22:15
  • Haha ... that definitely made me LOL :) ... That being said, is the box moving when you're searching for it? Shouldn't the search be taking place within a given time-slice of the box movement simulation? – Jason Oct 14 '11 at 22:59
1

Create a matrix containing "quadrant" elements, where each quadrant represents an N*M space within your system, with N and M being the width and height of the widest and tallest rectangles, respectively. Each rectangle will be placed in a quadrant element based on its upper left corner (thus, every rectangle will be in exactly one quadrant). Given a rectangle A, check for collisions between rectangles in the A's own quadrant and the 8 adjacent quadrants.

This is an algorithm I recall seeing recommended as a simple optimization to brute force hit-tests in collision detection for game design. It works best when you're mostly dealing with small objects, though if you have a couple large objects you can avoid wrecking its efficiency by performing collision detection on them separately and not placing them in a quadrant, thus reducing quadrant size.

Brian
  • 25,523
  • 18
  • 82
  • 173
  • Technically the quadrants don't have to be bigger than the rectangles, _if_ each quadrant knows _all_ of the rectangles that touch it. Then you only have to check the quadrants you're in. Faster, but takes more memory. – Mooing Duck Oct 14 '11 at 22:16
1

As they are not overlapping I would suggest an approach similar (but not equal) to Jason Moore (B). Sort your array by x of upper left corner. And sort a copy by y of upper left corner. (of course you would just sort pointers to them to save memory).

Now you once create two sets Sliding_Window_X and Sliding_Window_Y.

You search with binary search once your x-coordinate (upper left) for your A window in the x-sorted array and your y-coordinate. You put your results into the corrospondng Sliding_Window_Set. Now you add all following rectangles in the ordered array that have a lower x(y) (this time lower right) coordinate than your lower right of A.

The result is that you have in your Sliding_Window-sets the windows that overlap with your A in one coordinate. The overlapping of A is the intersection of Sliding_Window_X and _Y.

The Sliding_Window sets can be easily represented by just 2 numbers (begin and end index of the corrosponding sorted array).

As you say you move A, it is now really easy to recalculate the overlap. Depending on the direction you can now add/remove Elements to the Sliding_Window set. I.e. you take just the next element from the sorted array at the front/end of the set and maybe remove on at the end.

flolo
  • 15,148
  • 4
  • 32
  • 57
0

Let your set of rectangle be (Xi1,Yi1,Xi2,Yi2) where i varies from 0 to N.

Rectangle A and B can NOT be intersecting if Ax1 > Bx2 || Ay1 < By2 || Bx1 > Ax2 || By1 < Ay2.

Create tree which is optimized for range/interval (For exa: segment tree or interval tree) See http://w3.jouy.inra.fr/unites/miaj/public/vigneron/cs4235/l5cs4235.pdf

Use this tree to find set of triangle while your triangle is changing coordinates.

Jack
  • 1,398
  • 3
  • 16
  • 26
0

By calculating the area of each rectangle and and checking the length L, height H and area of rectangles whether exceeds or not the length and height and area of a rectangle A

nurmurat
  • 151
  • 1
  • 11
0

Method (A)

You could use an interval tree or segment tree. If the trees were created so that they would be balanced this would give you a run time of O(log n). I assume this type of preprocessing is practical because it would only happen once (it seems like you are more concerned with the runtime once the rectangle starts moving than you are with the amount of initial preprocessing for the first time). The amount of space would be O(n) or O(n log n) depending on your choice above.

Method (B)

Given that your large set of rectangles are of fixed size and never change their coordinates and that they are non-overlapping, you could try a somewhat different style of algorithm/heuristic than proposed by others here (assuming you can live with a one-time, upfront preprocessing fee).

Preprocessing Algorithm [O(n log n) or O(n^2) runtime {only run once though}, O(n) space]

  1. Sort the rectangles by their horizontal coordinates using your favorite sorting algorithm (I am assuming O(n log n) run time).
  2. Sort the rectangles by their vertical coordinates using your favorite sorting algorithm (I am assuming O(n log n) run time)
  3. Compute a probability distribution function and a cumulative distribution function of the horizontal coordinates. (Runtime of O(1) to O(n^2) depending on method used and what kind of distribution your data has)

    a) If your rectangles' horizontal coordinates follow some naturally occurring process then you can probably estimate their distribution function by using a known distribution (ex: normal, exponential, uniform, etc.).

    b) If your rectangles' horizontal coordinates do not follow a known distribution, then you can calculate a custom/estimated distribution by creating a histogram.

  4. Compute a probability distribution function and a cumulative distribution function of the vertical coordinates.

    a) If your rectangles' vertical coordinates follow some naturally occurring process then you can probably estimate their distribution function by using a known distribution (ex: normal, exponential, uniform, etc.).

    b) If your rectangles' vertical coordinates do not follow a known distribution, then you can calculate a custom/estimated distribution by creating a histogram.

Real-time Intersection Finding Algorithm [Anywhere from O(1) to O(log n) to O(n) {note: if O(n), then the constant in front of n would be very small} run time depending on how well the distribution functions fit the dataset]

  1. Taking the horizontal coordinate of your moving rectangle and plug it into the cumulative density function for the horizontal coordinates of the many rectangles. This will output a probability (value between 0 and 1). Multiply this value times n (where n is the number of many rectangles you have). This value will be the array index to check in the sorted rectangle list. If the rectangle of this array index happens to be intersecting then you are done and can proceed to the next step. Otherwise, you have to scan the surrounding neighbors to determine if the neighbors intersect with the moving rectangle. You can attack this portion of the problem multiple ways:

    a) do a linear scan until finding the intersecting rectangle or finding a rectangle on the other side of the moving rectangle

    b) calculate a confidence interval using the probability density functions you calculated to give you a best guess at potential boundaries (i.e. an interval where an intersection must lie). Then do a binary search on this small interval. If the binary search fails then revert back to a linear search in part (a).

  2. Do the same thing as step 1, but do it for the vertical portions rather than the horizontal parts.

  3. If step 1 yielded an intersection and step 2 yielded an intersection and the intersecting rectangle in step 1 was the same rectangle as in step 2, then the rectangle must intersect with the moving rectangle. Otherwise there is no intersection.

Jason Moore
  • 3,294
  • 15
  • 18
0

Use an R+ tree, which is most likely precisely the specific tree structure you are looking for. R+ trees explicitly do not allow overlapping in the internal (non-leaf) structure in exchange for speed. As long as no object exists in multiple leaves at once, there is no overlap. In your implementation, rather than support overlap, whenever an object needs to be added to multiple leaves, just return true instead.

Here is a detailed description of the data structure, including how to manage rectangles: The R+-tree: A dynamic index for multi-dimensional objects

VoidStar
  • 5,241
  • 1
  • 31
  • 45