Determine If Two Rectangles Overlap

May 12, 2011 in Uncategorized

Given two axis-aligned rectangles A and B. Write a function to determine if the two rectangles overlap.


Hint:
If you are coming up with a complicated set of conditionals, you might think too hard. There is an easier way. Try to think in the opposite direction.

Two overlapping rectangles. A rectangle can be defined by its upper left and lower right corner.

Solution:
Assume that the two rectangles are given as point (P1, P2) and (P3, P4) respectively. One direct way to attempt this problem is when two rectangles overlap, one rectangle’s corner point(s) must contain in the other rectangle. Do keep in mind of the following cases:

More overlapping rectangles cases to consider.

As you can see, the conditionals can be pretty complicated as there are a total eight of them. Can we simplify it further?

A much easier and better approach would be to think from the opposite. How about asking yourself how the two rectangles cannot overlap each other? Two rectangles do not overlap when one is above/below, or to the left/right of the other rectangle.

The condition’s expression is:

! ( P2.y < P3.y || P1.y > P4.y || P2.x < P3.x || P1.x > P4.x )

Using De Morgan’s law, we can further simplify the above expression to:

( P2.y = P3.y && P1.y = P4.y && P2.x = P3.x && P1.x = P4.x )

Further Thoughts:

  • What if the two rectangles are not necessarily axis-aligned? (That is, the rectangles can be rotated around its center at a certain angle.) Solving this problem requires more Math and understanding of linear algebra, so if you’re interested see my post on: How to determine if a point is inside a rectangle.
  • Given a list of rectangles, how would you determine the set of overlapping rectangles efficiently? Why would this be useful? Imagine you have a number of windows opened on your desktop. The operating system would need to know the overlapped windows in order to repaint the dirty region as windows are being moved around.
VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
Determine If Two Rectangles Overlap, 5.0 out of 5 based on 1 rating

20 responses to Determine If Two Rectangles Overlap

  1. Given two rectangles consider their left bottom coordinates – (x1, y1) and (x2, y2) and their length and breadth – L1, B1 & L2, B2 respectively. Call rectangle with smaller x coordinate as rect A, the other one rect B.
    Now for A and B to intersect:
    segment (x1, y1) – (x1, y1+B1) must intersect with (x2, y2) – (x2, y2+B2)
    and
    segment (x1, y1) – (x1+L1, y1) must intersect with (x2, y2) – (x2 + L2, y2).

    VA:F [1.9.22_1171]
    0
  2. Suppose rect A is (X_A1, X_A2, Y_A1, Y_A2) and rect B is (X_B1, X_B2, Y_B1, Y_B2), where X_A1 < X_A2, Y_A1 < Y_A2, …

    Start from the basic idea: compare the x coordinates,
    bool x_ins = false;
    if (X_A1 X_B1);
    else x_ins = (X_A1 X_B1) && (X_A1 Y_B1) && (Y_A1 < Y_B2) );
    }

    VA:F [1.9.22_1171]
    0
  3. Notice that we don’t need to compare X_A1 and X_B1 at all. Thus, we have:

    VA:F [1.9.22_1171]
    0
  4. Your expression is wrong according to your graph, it should be:
    ! ( P2.y > P3.y || P1.y < P4.y || P2.x P4.x )

    =>

    ( P2.y ≤ P3.y && P1.y ≥ P4.y && P2.x ≥ P3.x && P1.x ≤ P4.x )

    VA:F [1.9.22_1171]
    0
  5. Ah, it eats my answer!

    Your expression is wrong according to your graph, it should be:

    ! ( P2.y > P3.y || P1.y < P4.y || P2.x P4.x )

    =>

    ( P2.y ≤ P3.y && P1.y ≥ P4.y && P2.x ≥ P3.x && P1.x ≤ P4.x )

    VA:F [1.9.22_1171]
    0
  6. VA:F [1.9.22_1171]
    0
  7. Consider the following example, the corner of one rectangle does not necessarily contain in another, does the solution work for this case?

    ____
    | |
    ___|___|___
    | | | |
    | | | |
    |___|___|___|
    | |
    |___|

    VA:F [1.9.22_1171]
    0
    • sorry for the messed graph, the corners of the first rectangle is (0,1) and (10,0) and the second rectangle (3,3) (4,-4)

      VA:F [1.9.22_1171]
      0
  8. Another way to think about this:

    For two axis-aligned rectangles A and B, with axis-aligned bounding box C,
    A and B do NOT intersect if:

    C.width > (A.width + B.width)
    OR
    C.height > (A.height + B.height)

    VA:F [1.9.22_1171]
    0
  9. How would this work for a two rectangles which overlap so as to make a symbol like + ?

    VA:F [1.9.22_1171]
    0
  10. 1337c0d3r, I don’t think the statement you made is correct:
    ” when two rectangles overlap, one rectangle’s corner point(s) must contain in the other rectangle”

    Try the following case:

    __
    ____|__|_____
    |___ |__|____|
    |__|

    VA:F [1.9.22_1171]
    0
  11. Sorry, the drawing above is messed up. I find a picture on the Internet that describes what I attempted to draw:
    http://enchantia.com/graphapp/doc/tech/overlap.gif

    Although the rectangles are overlapped, none of their corners is being contained.

    VA:F [1.9.22_1171]
    0
  12. It’s better to consider the overlap problem of two lines firstly, because this one is easier to solve and gives good hits to our real problem.

    VN:F [1.9.22_1171]
    0
  13. Can I think it like this? If the Rectangles satisfy one of the following conditions, those two will overlap:
    p1.x<=p3.x=p1.y

    p3.x<=p1.x=p3.y

    VN:F [1.9.22_1171]
    0
  14. How about seeing the distance between their repsective coordinates of centers and if it is less than half of their side ??

    VN:F [1.9.22_1171]
    0
  15. “One direct way to attempt this problem is when two rectangles overlap, one rectangle’s corner point(s) must contain in the other rectangle. ”

    The above statement is not correct.

    VN:F [1.9.22_1171]
    0
  16. See http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/

    bool BoxesIntersect(const Box2D &a, const Box2D &b)
    {
    if (a.max.x b.max.x) return false; // a is right of b
    if (a.max.y
    b.max.y) return false; // a is below b
    return true; // boxes overlap
    }

    VA:F [1.9.22_1171]
    0
  17. bool BoxesIntersect(const Box2D &a, const Box2D &b)
    {
    if (p2.x p4.x) return false; // a is right of b
    if (p1.y p3.y) return false; // a is below b
    return true; // boxes overlap
    }

    VN:F [1.9.22_1171]
    0

Leave a reply

Your email address will not be published. Required fields are marked *

You may use the <code> tag to embed your code.