1

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;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • If it shows "wrong answer", the website should give you the corresponding test case. Then you have to test and debug it. You have to provide "expected output" and your output. – Louis Go Jun 29 '20 at 05:33
  • @Louis No, often they only tell you howmany, or even that not all have passed. – Yunnosch Jun 29 '20 at 05:34
  • https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Yunnosch Jun 29 '20 at 05:38
  • Do you really get "wrong answer"? Not "time limit exceeded"? – Yunnosch Jun 29 '20 at 05:41
  • ` if (l1.x >= r2.x || l2.x >= r1.x) return false; ` This seems wierd. Question stated "Two frames sharing a side are considered overlaid." while your equal sign means not. – Louis Go Jun 29 '20 at 05:42

2 Answers2

0

You should check overlapping in both axis, X and Y. use your fingers to demonstrate two shapes and move them to overlap one another. you will see that moving the shapes only sideways will answer one out of some test cases. just a sketch of the possibilities

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Yair
  • 111
  • 1
  • 3
  • 1
    OP's algorithm is okay, because consider it's not overlaid is easier than consider overlaid. However sharing side is not checking correctly. – Louis Go Jun 29 '20 at 05:52
  • "You should check ... use your fingers to demonstrate two shapes and move them to overlap one another." is a good way of approaching this. But I do not understand "you will see that moving the shapes only sideways will answer one out of some test cases". Please elaborate. Maybe identify the one test case. The test case which fails according to accepted answer is not in your diagram. Maybe identify the test cases which fail and the part of OPs code which causes that. Or outline an algorithm which does not fail. All in all, I suspect that this does not really answer the question. – Yunnosch Jun 29 '20 at 07:05
0

Two frames sharing a side are considered overlaid.

While your code consider sharing a side is NOT overlaid by using >= and <=.

Following is the example without equal signs.

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; 
} 
Louis Go
  • 2,213
  • 2
  • 16
  • 29