# Answer to Question #20009 in C++ for gulfa

Question #20009
how to rectangle overlapp with the if condition
1
2012-12-06T11:59:38-0500
#include <iostream>
#include <conio.h>

using namespace std;

struct Rectangle
{
int x, y;
int width, height;

Rectangle(int x, int y, int width, int height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}

void print(const char* name) const
{
cout << name << endl;
cout << "& left X: " << x << endl;
cout << "& bottom Y: " << y << endl;
cout << "& width: " << width << endl;
cout << "& height: " << height << endl << endl;
}
};

bool RectanglesOverlap(Rectangle r1, Rectangle r2)
{
if ((r1.x + r1.width < r2.x) || (r1.x > r2.x + r2.width))
return false;
if ((r1.y + r1.height < r2.y) || (r1.y > r2.y + r2.height))
return false;
return true;
}

int main()
{
Rectangle r1(2, 5, 4, 3);
Rectangle r2(7, 9, 5, 6);
Rectangle r3(4, 1, 3, 8);

r1.print("R1:");
r2.print("R2");
r3.print("R3");

if (RectanglesOverlap(r1, r2))
cout << "R1 and R2 overlap." << endl;
if (RectanglesOverlap(r2, r3))
cout << "R2 and R3 overlap." << endl;
if (RectanglesOverlap(r1, r3))
cout << "R1 and R3 overlap." << endl;

getch();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!