Problem
Write a function to check if two rectangles defined as below, have common area or not.
The functions take the top left and bottom right coordinate as input
and return 1 if they have common area, otherwise return 0.
Solution
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct{
int x;
int y;
} Point;
bool IsPointInside(Point &p, Point& topLeft, Point& bottomRight)
{
if(p.x < bottomRight.x && p.x > topLeft.x && p.y < bottomRight.y && p.y > topLeft.y){
return true;
}
return false;
}
bool HasCommonArea(Point& topLeft1, Point& bottomRight1, Point& topLeft2, Point& bottomRight2)
{
if(IsPointInside(topLeft1, topLeft2, bottomRight2)){
return true;
}
if(IsPointInside(bottomRight1, topLeft2, bottomRight2)){
return true;
}
Point bottomLeft = {topLeft1.x, bottomRight1.y};
if(IsPointInside(bottomLeft, topLeft2, bottomRight2)){
return true;
}
Point topRight = {bottomRight1.x, topLeft1.y};
if(IsPointInside(topRight, topLeft2, bottomRight2)){
return true;
}
return false;
}
int main(int argc, char* argv[])
{
for(int i = 0; i < 20; ++ i){
Point topLeft1;
Point bottomRight1;
Point topLeft2;
Point bottomRight2;
topLeft1.x = rand() % 20 + 1;
topLeft1.y = rand() % 20 + 1;
bottomRight1.x = rand() % 20 + 1 + topLeft1.x;
bottomRight1.y = rand() % 20 + 1 + topLeft1.y;
cout << "Rectangle 1 is ";
cout << "Topleft(" << setw(2) <<topLeft1.x << ", ";
cout << setw(2) <<topLeft1.y << ")";
cout << "bottomRight(" << setw(2) <<bottomRight1.x << ", ";
cout << setw(2) <<bottomRight1.y << ")" << endl;
topLeft2.x = rand() % 20 + 1;
topLeft2.y = rand() % 20 + 1;
bottomRight2.x = rand() % 20 + 1 + topLeft2.x;
bottomRight2.y = rand() % 20 + 1 + topLeft2.y;
cout << "Rectangle 2 is ";
cout << "Topleft(" << setw(2) << topLeft2.x << ", ";
cout << setw(2) <<topLeft2.y << ")";
cout << "bottomRight(" << setw(2) <<bottomRight2.x << ", ";
cout << setw(2) <<bottomRight2.y << ")" << endl;
if(HasCommonArea(topLeft1, bottomRight1, topLeft2, bottomRight2)){
cout << "The two rectangles have common area" << endl;
}
else{
cout << "The two rectangles DON'T have common area" << endl;
}
cout << "-------------------------------" << endl << endl;
}
return 0;
}
Output
Rectangle 1 is Topleft( 2, 8)bottomRight(17, 9)
Rectangle 2 is Topleft(10, 5)bottomRight(29, 24)
The two rectangles have common area
-------------------------------
Rectangle 1 is Topleft( 3, 5)bottomRight( 9, 11)
Rectangle 2 is Topleft( 2, 8)bottomRight( 4, 20)
The two rectangles have common area
-------------------------------
Rectangle 1 is Topleft(16, 3)bottomRight(24, 20)
Rectangle 2 is Topleft(12, 5)bottomRight(15, 19)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft(13, 3)bottomRight(15, 20)
Rectangle 2 is Topleft(19, 16)bottomRight(27, 23)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft(12, 19)bottomRight(22, 32)
Rectangle 2 is Topleft( 8, 20)bottomRight(24, 35)
The two rectangles have common area
-------------------------------
Rectangle 1 is Topleft( 4, 12)bottomRight( 7, 26)
Rectangle 2 is Topleft(14, 5)bottomRight(16, 17)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft(14, 9)bottomRight(22, 14)
Rectangle 2 is Topleft( 3, 18)bottomRight(21, 38)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 4, 2)bottomRight(14, 21)
Rectangle 2 is Topleft(17, 16)bottomRight(28, 19)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 9, 7)bottomRight(10, 10)
Rectangle 2 is Topleft( 5, 9)bottomRight(12, 15)
The two rectangles have common area
-------------------------------
Rectangle 1 is Topleft(11, 10)bottomRight(22, 21)
Rectangle 2 is Topleft( 7, 2)bottomRight(21, 11)
The two rectangles have common area
-------------------------------
Rectangle 1 is Topleft(10, 4)bottomRight(15, 19)
Rectangle 2 is Topleft(17, 1)bottomRight(24, 18)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft(12, 9)bottomRight(17, 29)
Rectangle 2 is Topleft( 7, 4)bottomRight(25, 23)
The two rectangles have common area
-------------------------------
Rectangle 1 is Topleft(19, 3)bottomRight(29, 5)
Rectangle 2 is Topleft(14, 16)bottomRight(34, 35)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 5, 11)bottomRight(23, 18)
Rectangle 2 is Topleft(14, 7)bottomRight(16, 13)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 5, 13)bottomRight(16, 23)
Rectangle 2 is Topleft(18, 14)bottomRight(36, 27)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 7, 11)bottomRight( 9, 28)
Rectangle 2 is Topleft(16, 8)bottomRight(32, 23)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft(12, 13)bottomRight(23, 24)
Rectangle 2 is Topleft( 2, 5)bottomRight( 9, 16)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 8, 12)bottomRight(16, 30)
Rectangle 2 is Topleft(18, 8)bottomRight(32, 12)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 6, 10)bottomRight(16, 29)
Rectangle 2 is Topleft( 2, 9)bottomRight( 5, 16)
The two rectangles DON'T have common area
-------------------------------
Rectangle 1 is Topleft( 7, 11)bottomRight(21, 20)
Rectangle 2 is Topleft( 1, 12)bottomRight( 4, 28)
The two rectangles DON'T have common area
-------------------------------
Press any key to continue . . .