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
Two rectangles do not overlap if one of the following conditions is true.
1) if l1.x > r2.x OR l2.x > r1.x // If one rectangle is on left side of other Case (3)
2) if l1.y < r2.y OR l2.y < r1.y // If one rectangle is above other Case(4) not presented here.
Case 1) and 2) are Overlapping.
hence here it should return false.
Code : Following code returns true if two rectangles overlap.
struct Coord
{
int x, y;
};
bool RectOverlap(Coord l1, Coord r1, Coord l2, Coord r2)
{
if (l1.x > r2.x || l2.x > r1.x) return false;
if (l1.y < r2.y || l2.y < r1.y) return false;
return true; //else in all condition
}
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
Two rectangles do not overlap if one of the following conditions is true.
1) if l1.x > r2.x OR l2.x > r1.x // If one rectangle is on left side of other Case (3)
2) if l1.y < r2.y OR l2.y < r1.y // If one rectangle is above other Case(4) not presented here.
Case 1) and 2) are Overlapping.
hence here it should return false.
Code : Following code returns true if two rectangles overlap.
struct Coord
{
int x, y;
};
bool RectOverlap(Coord l1, Coord r1, Coord l2, Coord r2)
{
if (l1.x > r2.x || l2.x > r1.x) return false;
if (l1.y < r2.y || l2.y < r1.y) return false;
return true; //else in all condition
}