There are two Rectangular photo frames. Find if the given two frames overlap or not. A frame is denoted by providing the x and y coordinates of two points: the left top corner and the right bottom corner of the frames. Two frames sharing a side are considered overlaid.
Input Format The first integer T denotes the number of test cases. For every test case, there are 2 lines of input. The first line consists of 4 integers: denoting the coordinates of the 2 points of the first frame. The first integer denotes the x coordinate and the second integer denotes the y coordinate of the left topmost corner of the first frame. The next two integers are the x and y coordinates of the right bottom corner. Similarly, the second line denotes the coordinates of the two points of the second frame in a similar fashion.
Output Format For each test-cases, output (either 1 or 0 ) denoting whether the 2 frames are overlapping. 1 denotes the frames overlap whereas 0 denotes the frames do not overlap.
Constraints 1 <= T <= 10
− 10 4 <= x <= 10 4
− 10 4 <= y <= 10 4
Time Limit 1 second
Example Input:
2
0 10 10 0
5 5 15 0
0 2 1 1
-2 -3 0 2
Output:
1
0
My code:
#include <bits/stdc++.h>
using namespace std;
struct Point
{
int x, y;
};
bool doOverlap(Point l1, Point r1, Point l2, Point 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;
}
int main()
{
int t,l1x,l1y,r1x,r1y,l2x,l2y,r2x,r2y,l1,r1,l2,r2;
cin>>t;
while(t-- >0)
{
cin>>l1x;
cin>>l1y;
cin>>r1x;
cin>>r1y;
cout<<endl;
cin>>l2x;
cin>>l2y;
cin>>r2x;
cin>>r2y;
Point l1 = {l1x,l1y};
Point r1 = {r1x,r1y};
Point l2 = {l2x,l2y};
Point r2 = {r2x,r2y};
if (doOverlap(l1, r1, l2, r2)) {
cout<<"1";
}
else{
cout<<"0";
}
}
return 0;
}