Find if 2 Rectangles overlap

By | May 24, 2014

Given two rectangles with their base parallel to each other . You have to find whether the rectangles overlap or not.
Ex Rect1(2,3,5,6) Rect2(1,3,1,10)
Where a rectangle is specified by coordinates of it’s TopLeft (x1,y1) and BottomMRight(x2,y2) coordinates in sequence
for rect1 TopLeft(2,3) and BottomRight(5,6).

The problem is very simple . Since the rectangle have base parallel to each other we have tocheck simply if
the bottom (y2) of one rectangle is greater than the top(y1) or if the left(x1) of one rectangle is greater than the right(x2) of other rectangle.Vice Versa .
Here’s the code for this problem :

Leave a Reply

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