4

I come across this math problem which is needed to complete my application, so I'm asking for help.

Given 2 (or more, but basically for 2) rectangles with 2 known points for each: Top-left(x1, y1) and Bottom-right(x2, y2) (I can find the length with these info, if it is a need to solve the problem).

TL(x1, y1)
   +-----------------+
   |                 |
   |                 |       TL(x3, y3)
   |                 |            +---------------------------+
   +-----------------+            |                           |
               BR(x2, y2)         +---------------------------+
                                                         BR(x4, y4)

Is there anyway to determine if they have intersection, in area, I mean, if any part of this rectangle lays on any part of another one?

I've searched and found out a little help, but it does not solve the problem:

There are 4 conditions in which the two rectangles will not intersect:

  • The left edge of one rectangle is on the right side of the right edge of the other one, means that the first one is completely laid on the right side of the second one, no intersection.

  • The right edge of one rectangle is on the left side of the left edge of the other one, means that the first one is completely laid on the left side of the second one, no intersection.

  • The top edge of one rectangle is under the bottom edge of the other one, means that the first one is completely laid under the second one, no intersection.

  • The bottom edge of one rectangle is above the top edge of the other one, means that the first one is completely laid above the second one, no intersection.

So I tried to reverse the conditions, which is if 4 of the above does not occur, the rectangles may intersect. But I still can find the condition (such as the picture above) in which 2 rectangles do not fulfill any condition, and still does not intersect.

Any help is highly appreciated, please show me the way to do it or algorithm or code (JS and PHP only please).

Big thank!

[x]

xx3004
  • 760
  • 2
  • 11
  • 20
  • 1
    The diagram fulfills condition 2. – Ignacio Vazquez-Abrams Nov 03 '11 at 04:36
  • 2
    This looks like exactly what you need: http://stackoverflow.com/questions/115426/algorithm-to-detect-intersection-of-two-rectangles – nickb Nov 03 '11 at 04:42
  • @IgnacioVazquez-Abrams: Thanks man, now I notice it. – xx3004 Nov 03 '11 at 05:10
  • @nickb: I've read that article, and really have no clue what is it about >. – xx3004 Nov 03 '11 at 05:12
  • @nnnnnn: My bad, thank you alot man, now I know I can use that theory to apply to my code. Thank you again. [x] – xx3004 Nov 03 '11 at 05:12
  • It's not "may" intersect, it's "must". If none of those conditions are met the rectangles must intersect, assuming you count one being completely contained within the other as an intersection. (EDIT: sorry, I deleted my original comment so I could change it, not realising that more comments had been left after mine.) – nnnnnn Nov 03 '11 at 05:14

2 Answers2

5

An algorithm for the intersection detection ("overlapping") of any number of rectangles could work as follows. Two data structures are used.

  • A sorted list S of the x-coordinates of the left and right edges of the rectangles.
  • An interval tree T of the intervals given by the y-coordinates (bottom/top) of the rectangles.

The algorithm sweeps over the sorted list S of the x-coordinates:

  1. If the current x-coordinate is the left edge of a rectangle R then the y-coordinates [y1, y2] of R are compared to the interval tree T. If an overlap is found, then the algorithm stops and reports OVERLAP. If there was no overlap in the tree T, then the interval [y1, y2] is inserted into the tree.

  2. If the current x-coordinate is the right edge of a rectangle R then its y-interval [y1, y2] is removed from the interval tree T.

  3. If the sorted list S is completely processed, then there was no overlap. The algorithm stops and reports NO-OVERLAP.

The time complexity for N rectangles is O(N*log(N)) because for each 2*N x-coordinates a search in the interval tree for N intervals is performed. The space complexity is O(N) for the auxiliary data structures S and T.

Jiri Kriz
  • 9,192
  • 3
  • 29
  • 36
0

This is my two cents on the problem. Let me know if this can be improved upon. The examples I do myself seem to hold true with this code, however if you can give me example coordinates that make this fail, I would still like to work on this problem :)

 <?php

//declare the points for your rectangles
//'UL' means upper left points, and 'LR' means the lower right points
$rectangle_array = array(
    $R1 = array("UL" => array("x" => 10, "y" => 20), "LR" => array("x" => 22, "y" => 5)),
    $R2 = array("UL" => array("x" => 32, "y" => 44), "LR" => array("x" => 65, "y" => 20)),
    $R3 = array("UL" => array("x" => 20, "y" => 16), "LR" => array("x" => 25, "y" => 10))
);


if (rectIntersect($rectangle_array)) {
    echo "THEY INTERSECT";
} else {
    echo "NO INTERSECTION";
}

function rectIntersect($rectangles) {
    $num_rectangles = count($rectangles);
    for ($i = 0; $i < $num_rectangles; $i++) {
        //for each rectangle, compare points to every following rectangle
        $R1 = $rectangles[$i];
        for ($k = $i; $k < ($num_rectangles - $i); $k++) {
            $R2 = $rectangles[$k + 1];
            if ($R1['LR']['x'] > $R2['UL']['x'] && $R1['UL']['x'] < $R2['LR']['x']) {
                //rectangles cross on x-axis
                if (($R1['LR']['y'] < $R2['UL']['y'] && $R1['UR']['y'] < $R2['LR']['y']) ||
                        ($R1['UL']['y'] > $R2['LR']['y'] && $R1['LR']['y'] < $R2['UL']['y'])) {
                    //rectangles intersect
                    return true;
                }
            }
        }
    }
    return false;
}

?>
donutdan4114
  • 1,262
  • 1
  • 8
  • 16
  • May I suggest you rename your variables to better indicate which point belongs to which corner of which rectangle? E.g., $r1TL (rectangle 1 top-left), $r1BR, $r2TL, $r2BR. I know the question used $x1, etc., but that's confusing. – nnnnnn Nov 03 '11 at 05:14