Find if two rectangles overlap

Given two rectangles, find if the given two rectangles overlap or not.

Note that a rectangle can be represented by two coordinates, top left and bottom right. So mainly we are given following four coordinates.
l1: Top Left coordinate of first rectangle.
r1: Bottom Right coordinate of first rectangle.
l2: Top Left coordinate of second rectangle.
r2: Bottom Right coordinate of second rectangle.

rectanglesOverlap

We need to write a function bool doOverlap(l1, r1, l2, r2) that returns true if the two given rectangles overlap.

One solution is to one by one pick all points of one rectangle and see if the point lies inside the other rectangle or not. This can be done using the algorithm discussed here.
Following is a simpler approach. Two rectangles do not overlap if one of the following conditions is true.
1) One rectangle is above top edge of other rectangle.
2) One rectangle is on left side of left edge of other rectangle.

We need to check above cases to find out if given rectangles overlap or not. Following is C++ implementation of the above approach.

#include<stdio.h>

struct Point
{
    int x, y;
};

// Returns true if two rectangles (l1, r1) and (l2, r2) overlap
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
    // If one rectangle is on left side of other
    if (l1.x > r2.x || l2.x > r1.x)
        return false;

    // If one rectangle is above other
    if (l1.y < r2.y || l2.y < r1.y)
        return false;

    return true;
}

/* Driver program to test above function */
int main()
{
    Point l1 = {0, 10}, r1 = {10, 0};
    Point l2 = {5, 5}, r2 = {15, 0};
    if (doOverlap(l1, r1, l2, r2))
        printf("Rectangles Overlap");
    else
        printf("Rectangles Don't Overlap");
    return 0;
} 

Output:

Rectangles Overlap

Time Complexity of above code is O(1) as the code doesn’t have any loop or recursion.

This article is compiled by Aman Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above





  • Vishal Gupta

    cant we directly say that if length of diagonal =then it will overlap?

  • abhamish mishra

    @geeksforgeeks:disqus .. will the solution work if the rectangles are not axis aligned?

  • namnorireports

    Other tipical way of checking that, is to check if the max of the min is less than the min of the max.

    if ((max(l1.x, l2.x) < min(r1.x, r2.x)) && ((max(l1.y, l2.y) < min(r1.y, r2.y))))
    return true;
    else
    return false;

  • Kanhaiya Kumawat

    @geeksforgeeks:disqus you should try avoid posting the solutions with obvious errors. I am regular follower of the site. Post like this with obvious errors (which are corrected later), certainly not good. Otherwise you are doing awesome job!!

  • Ayush Srivastava

    @geeksforgeeks:disqus I hope you people soon post a “set-2″ of this problem for “non-aligned axis”. Keep up the good work. You are helping millions of geeks!! Thanks for the wonderful site. You people are amazing. :)

  • Akshat Gupta

    I think for the condition if one rectangle is above other the ‘>’ signs should be reversed to ‘<'. Condition should be if (l1.y < r2.y || l2.y < r1.y) Correct me if I am wrong.

    • GeeksforGeeks

      Thanks for pointing this out. We have corrected the condition.

  • Nitin

    The question simply assumes that rectangles are axis aligned. It will not work for other rectangles.

    • GT

      Yeah right. This works only for axis aligned rectangles. Question needs to be updated.

      • GeeksforGeeks

        Nitin & GT, thanks for sharing your thoughts. We will update the post soon.

  • Prince John Wesley

    Both the predicate inside doOverlap function are same. Fix it.

    • GeeksforGeeks

      Thanks for pointing this out. We have updated the post.