find rectangle overlap

hi everybody there,
I received a job from the my lecturer to complete a code that find two rectangle intersection.
here is the code:

//#include<rectangle.h>
#include <iostream>
using namespace std;

/*

*/

// constructor for unit rectangle
// constructor for given points
struct Point {
float x;
float y;
};

struct Rectangle {
struct Point itsLowerLeft;
struct Point itsLowerRight;
struct Point itsUpperLeft;
struct Point itsUpperRight;
};

struct Point makepoint (float x, float y) ;
struct Rectangle Rectangle();
struct Rectangle Rectangle(Point lowerLeft, Point upperRight);
struct Rectangle Rectangle(float base, float height);
struct Rectangle Rectangle(float base, float height, Point offset);

float getWidth(struct Rectangle a);
float getHeight(struct Rectangle a);
float getArea(struct Rectangle a);
bool isPointInRectangle(struct Rectangle r, Point p);
bool isOverlap(struct Rectangle a, struct Rectangle b);



struct Point makepoint (float x, float y)
{
struct Point temp;
temp.x = x;
temp.y =y;
return temp;
}




// constructor for unit rectangle
struct Rectangle Rectangle()
{
struct Rectangle temp;
temp.itsLowerLeft= makepoint(0.0,0.0);
temp.itsLowerRight=makepoint(0.0,0.0);
temp.itsUpperLeft=makepoint(0.0,0.0);
temp.itsUpperRight=makepoint(0.0,0.0);
return temp;
}

struct Rectangle Rectangle(struct Point LowerLeft, struct Point UpperRight)
{
struct Rectangle temp;
temp.itsLowerLeft=LowerLeft;
temp.itsLowerRight=makepoint(UpperRight.x,LowerLeft.y);
temp.itsUpperLeft=makepoint(LowerLeft.x,UpperRight.y);
temp.itsUpperRight=UpperRight; // makepoint(upperRight.x,UpperLeft.y);
return temp;
}
struct Rectangle Rectangle(float base, float height)
{
struct Rectangle temp;
temp.itsLowerLeft=makepoint(0.0,0.0);
temp.itsLowerRight=makepoint(base,0.0);
temp.itsUpperLeft=makepoint(0.0,height);
temp.itsUpperRight=makepoint(base,height);
return temp;

}


// constructor for width and length and origin offset
struct Rectangle Rectangle(float base, float height, Point offset)
{
struct Rectangle temp;
temp.itsLowerLeft=offset;
temp.itsLowerRight=makepoint(offset.x+base,offset.y);
temp.itsUpperLeft=makepoint(offset.x,offset.y+height);
temp.itsUpperRight=makepoint(offset.x+base,offset.y+height);
return temp;

}

// get width of rectangle
float getWidth(struct Rectangle a){
return (a.itsLowerRight.x- a.itsLowerLeft.x) ;

}

// get height of rectangle
float getHeight(struct Rectangle a)
{return (a.itsUpperRight.y-a.itsUpperLeft.x);


}

// get area of rectangle
float getArea(struct Rectangle a)
{
return getHeight(a) *getWidth(a);
}

// look for overlap between two rectangles
// bool isOverlap(struct Rectangle a, struct Rectangle b)
bool ispointinRec(struct Point p, struct Rectangle r){
return p.x >= r.itsLowerLeft.x && p.x < r.itsUpperRight.x
&& p.y >= r.itsLowerLeft.y && p.y < r.itsUpperRight.y;


}
int main() {
struct Rectangle a, b, c;
struct Point pt1, pt2, pt3;
float bs = 3;
float height = 6;
pt1 = makepoint(0.0,0.0);
pt2 = makepoint(1.0,1.0);
pt3 = makepoint(1.0,2.0);
a = Rectangle(pt1, pt2);
b = Rectangle(bs, height);
c = Rectangle(bs, height, pt3);
cout << "------------ a ---------- " << endl;
cout << "Width of a: " << getWidth(a) << endl;
cout << "Height of a: " << getHeight(a) << endl;
cout << "Area of a: " << getArea(a) << endl;
cout << "------------ b ---------- " << endl;
cout << "Width of b: " << getWidth(b) << endl;
cout << "Height of b: " << getHeight(b) << endl;
cout << "Area of b: " << getArea(b) << endl;
cout << "------------ c ---------- " << endl;
cout << "Width of c: " << getWidth(c) << endl;
cout << "Height of c: " << getHeight(c) << endl;
cout << "Area of c: " << getArea(c) << endl;
cout << "------------ isOverlap a & b ---------- " << endl;
if (isOverlap(a,b) == true)
cout << "YES " << endl;
else
cout << "NO " << endl;

cout << "------------ isOverlap a & c ---------- " << endl;
if (isOverlap(a,c) == true)
cout << "YES " << endl;
else
cout << "NO " << endl;

cout << "------------ isOverlap b & c ---------- " << endl;
if (isOverlap(b,c) == true)
cout << "YES " << endl;
else
cout << "NO " << endl;

return 0;
}


I think we should finish the overlap function that is before the main function
please help me.I should know the way to do it the final exam is coming soooon
thanks
Hi!

You declaration:

bool isPointInRectangle(struct Rectangle r, Point p);

Your definition:
1
2
3
4
5
bool ispointinRec(struct Point p, struct Rectangle r){
return p.x >= r.itsLowerLeft.x && p.x < r.itsUpperRight.x
&& p.y >= r.itsLowerLeft.y && p.y < r.itsUpperRight.y;

}


They don't match.

Is your responsibility that you have to make this function?

bool isOverlap(struct Rectangle a, struct Rectangle b);
In addition you should use the Insert Code tag at notation.

Sorry, I'm busy now, later..

1
2
3
4
5
6
7
8
9
10
11
12
bool isOverlap(struct Rectangle a, struct Rectangle b)
{
	bool ret = false;
	
	if (isPointInRectangle(a, b.itsLowerLeft) || isPointInRectangle(a, b.itsLowerRight) 
	|| isPointInRectangle(a, b.itsUpperLeft) || isPointInRectangle(a, b.itsUpperRight)
	|| isPointInRectangle(b, a.itsLowerLeft) || isPointInRectangle(b, a.itsLowerRight) 
	|| isPointInRectangle(b, a.itsUpperLeft) || isPointInRectangle(b, a.itsUpperRight
	)ret = true;
	
	return ret;
}
what if they overlap like this:
1
2
3
4
5
6
7
8
9
10
11

    _________
    |       |
    |       |
____|       |___
|   |       |   |
|   |       |   |
|___|       |___|
    |       |
    |_______|



You are right.

Then some extra code should be inserted :)

If the rectangles are oblique then it is a bit complecated. I think I filter out the most cases and yours the last. So it should be look at that some of one line of the first rectangle goes through the lines of the other rectangle. It is a geometry task.

thanks a lot guys and you screw
sorry I was having many work to do with my study
Topic archived. No new replies allowed.