two rectangle overlap

hi

I got an assignment this week.But i dont understand it clearly. Here is the question


"A rectangle in x-y space can be defined with the x-y coordinates of the lower left and upper right corners. Write a programe that determines if the two rectangles overlap. Here user inputs the corners of the rectangle a total of 4 x-y pairs. Print out whether the rectangles overlap.

A sample run should be as follows

rectangle 1

Enter the upper x coordinate for rectangle1: 100
Enter the upper y coordinate for rectangle1: 200
Enter the lower x coordinate for rectangle1: 300
Enter the lower y coordinate for rectangle1: 400

rectangle 2

Enter the upper x coordinate for rectangle2: 200
Enter the upper y coordinate for rectangle2: 250
Enter the lower x coordinate for rectangle2: 400
Enter the lower y coordinate for rectangle2: 400

There is an overlap in the two rectangles. "

I have to submit on thursday so please help me on this


Thank in advance
let's call those coordinates as Lx1,Ly1,Rx1,Ry1 and Lx2,Ly2,Rx2,Ry2
if Lx2>Rx1||Rx2<Lx1||Ly2>Ry1||Ry2<Ly1 the two rectangles will not overlap
Hello, here is the assignment's code;)
i think that does what u want
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>                                                                                                                     
#include <iostream>
using namespace std;
typedef struct point
{
	int x;
	int y;
};

bool IsOverLap(point atop1,point abuttom1, point atop2, point abuttom2)
{
	if ((atop1.x <= atop2.x && abuttom2.x <= abuttom1.x) &&(atop1.y >= atop2.y && abuttom2.y >= abuttom1.y))
	{
		return true;
	} else
	if ((atop2.x<= atop1.x && abuttom1.x <= abuttom2.x) &&(atop2.y >= atop1.y && abuttom1.y >= abuttom2.y))
	{
		return true;
	}
	else
	{
		return false;
	}
}
int main()
{
	point top1,top2,buttom1,buttom2;
	cout<<"First Top point"<<endl;
	cin>>top1.x;
	cin>>top1.y;

	cout<<"First buttom point"<<endl;
	cin>>buttom1.x;
	cin>>buttom1.y;

	cout<<"Second Top point"<<endl;
	cin>>top2.x;
	cin>>top2.y;

	cout<<"Second buttom point"<<endl;
	cin>>buttom2.x;
	cin>>buttom2.y;

	if (IsOverLap(top1,buttom1,top2,buttom2))
	{
		printf("They are overlapped\n");
	}
	else
	{
		printf("They are not overlapped\n");                                               
	};

	getchar();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.