Intersection of Rectanges

Hello everyone,

I am stuck in this assignment where I need to find intersection of squares. So far I created a vector of squares( there are only 3 squares necessary). I input the value for width and height, which are the same, because the shape is a square.
There is Rectangle Class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Rectangle : Shape {

    Rectangle(Point xy, int ww, int hh) : w(ww), h(hh)//left top point, width and height
    {
        add(xy);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive side");
    }

    Rectangle(Point x, Point y) : w(y.x-x.x), h(y.y-x.y)// left top point, bottom right point
    {
        add(x);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive width or height");
    }
    void draw_lines() const;

    int height() const { return h; }
    int width() const { return w; }
private:
    int h;    // height
    int w;    // width
};



Based on this class I need to write a code how three rectangles intersect and create objects out those intersection. For instance, if two rectangles intersect each other, another rectangle is created(it is basically rectangle intersection)

Thanks guys
Last edited on
Google "axis aligned rectangle overlap".
Thanks man. One more question:

I am trying to create PaintSquare structure where i will evaluate intersection and paint the intersection by creating an object of this intersection but for some reason compiler return error variables cannot be resolved: a.x, a.input...

This is header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

struct Rectangle : Shape {

    Rectangle(Point xy, int ww, int hh) : w(ww), h(hh)
    {
        add(xy);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive side");
    }

    Rectangle(Point x, Point y) : w(y.x-x.x), h(y.y-x.y)
    {
        add(x);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive width or height");
    }
    void draw_lines() const;

    int height() const { return h; }
    int width() const { return w; }
private:
    int h;    // height
    int w;    // width
};


also Point structure:
1
2
3
4
5
6
struct Point {
    int x, y;
    Point(int xx, int yy) : x(xx), y(yy) { }
    Point() :x(0), y(0) { }
};


and this is my code:
1
2
3
4
5
6
7
8
9
10
struct PaintRectangle:Rectangle{

	bool overlap(Graph_lib::Rectangle a){
		bool retVal = true;
	    int x,y;
		int input;
        retVal= (x >=a.x + a.input || y >= a.y + a.input || x+input<=a.x || y+input >= a.y) ?true:false;
       return retVal;
	}
};
Last edited on
Post the definition of Shape.
If a pair of rectangles intersect then the left edge of the intersection is the maximum i.e., rightmost) of the left edges of the pair. Similar logic gives the right, top, and bottom edges.

If the rectangles don't intersect then the process above results in an invalid rectangle: one where the top is below the bottom, or the "right" side is actually on the left.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Rect {
    Rect(double t, double b, double l, double r) :
        top(t), bottom(b), left(l), right(r)
    {}
    double top, bottom, left, right;
};

// Determine if rectangles r1 and r2 intersect. So they do,
// return true and set "result" to the intersection. If they
// don't then return false and "result" is undefined.
bool intersect(const Rect &r1, const Rect &r2, Rect &result)
{
    result.top = max(r1.top, r2.top);
    result.bottom = min(r1.bottom, r2.bottom);
    result.left = max(r1.left, r2.left);
    result.right = min(r1.right, r2.right);
    return (result.top < result.bottom && result.left < result.right);
}

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
class Shape  {        // deals with color and style, and holds sequence of lines 
public:
    void draw() const;                 // deal with color and draw lines
    virtual void move(int dx, int dy); // move the shape +=dx and +=dy

    void set_color(Color col) { lcolor = col; }
    Color color() const { return lcolor; }
    void set_style(Line_style sty) { ls = sty; }
    Line_style style() const { return ls; }
    void set_fill_color(Color col) { fcolor = col; }
    Color fill_color() const { return fcolor; }

    Point point(int i) const { return points[i]; } // read only access to points
    int number_of_points() const { return int(points.size()); }

    virtual ~Shape() { }
protected:
    Shape();    
    virtual void draw_lines() const;   // draw the appropriate lines
    void add(Point p);                 // add p to points
    void set_point(int i,Point p);     // points[i]=p;
private:
    vector<Point> points;              // not used by all shapes
    Color lcolor;                      // color for lines and characters
    Line_style ls; 
    Color fcolor;                      // fill color

    Shape(const Shape&);               // prevent copying
    Shape& operator=(const Shape&);
};






In PaintRectangle::overlap():
* On lines 5 and 6 you wrote some variables you don't use. You should probably remove them, although I have a hard time understanding your intent.
* PaintRectangle::overlap() accepts a Rectangle, but the function attempts to access members of a PaintRectangle. Did you intend to accept a PaintRectangle?
* None of the classes you posed has a member named input.
input is basically width of rectangle, and x and y belong to Point(). I need PaintRectangle class to show intersection of rectangle and paint it in different color.
This is a part of my main program, maybe that helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main ()
try
{
	srand(time(NULL)); // This will ensure a really randomized number by help of time.
    const int length = 600;
    cout<<"Please enter length of squares"<<endl;
	int value; cin>>value;
	while(value<0&&value>600){
		cout<<"Wrong Value! Please re-enter the length"<<endl;
		cin>>value;
	}
	Point tl(100,100);    // top-left corner of our window
	Simple_window win(tl,length,length,"Programming Assignment#6");
       int x,y;
              x=axis(value);
              y=axis(value);
              Graph_lib::Rectangle r(Point(x,y), value, value);// top-left corner, width, height
              win.attach(r);
              r.set_fill_color(Color::red);     // color the inside of the rectangle
              r.set_color(Color::invisible);



input is basically width of rectangle
Again, none of the classes you've posted declare any member named 'input'. Are you sure you posted correctly?
Additionally, why is a width named 'input' instead of 'width'?
Ok i will explain in much more details.

Input is not a good variable name for the width I know.

I am not sure if I am correctly creating this structure.

In the main you get a prompt to input the width of square. And the program generates 3 squares randomly(obviously 3 Points(left top Points)

Object is "Rectangle r(Point(x,y), value, value);" where Point is top left corner of Rectangle and value(input) is input for the width of Rectangle.

What I was trying to do is

1) Creating vectors of 3 squares

2)After squares have been generated the computer checked for overlap(). Because Rectangle structure already exists, I don't need to create it. The only thing i need to create is PaintRectangle where i will use bool function to check for intersection and then create a new Rectangle which is the intersection of Squares 1 and 2 || Squares 2 and 3 ||
Squares 1 and 3. It will be painted in different color to see this intersection.

I got all information and logics i just need to implement it. I cant quite understand how to do it. I get all these errors. What should I use in my PaintedRectangle knowing that i got already Rectangle class, should i inherit it in PaintedRectangle structure or there is easier way to do it.
Please, help me with this one cuz i really need to submit it already.
Cheers

You don't need a separate Rectangle struct within PaintedRectangle. In fact, the intersection of two rectangles is independent of PaintedRectangle (although PaintedRectangle uses it). Look at my previous post. It shows a function that tells whether
two rectangles intersect and creates the intersecting rectangle when they do. Use this to
determine the intersecting rectangle. Then do whatever you want with it.

You may find that you don't need PaintedRectangle at all. A painted rectangle isn't a thing (a class), it's more like painting a rectangle is something that you do (i.e., maybe your Rect class needs a paint method).
I understand your logics, dhayden. But in my case it is a little different because my Rectangle structure has been already created and it is in Graph.h. But the problem arises when the constructor of that structure has Point(x,y) and i don't know how to compare these points.

how can i compare if my constructor Rectangle(Point xy, int ww, int hh) : w(ww), h(hh) has this point.

if I use bool function how can i use those parameters inside constructors and compare the objects.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Rectangle : Shape {

    Rectangle(Point xy, int ww, int hh) : w(ww), h(hh)
    {
        add(xy);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive side");
    }

    Rectangle(Point x, Point y) : w(y.x-x.x), h(y.y-x.y)
    {
        add(x);
        if (h<=0 || w<=0) error("Bad rectangle: non-positive width or height");
    }

    void draw_lines() const;

    int height() const { return h; }
    int width() const { return w; }
private:
    int h;    // height
    int w;    // width
};


This is my main()
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
int main ()
	try{
		srand(time(NULL));
		   cout<<"Enter the width of the rectangle:  " << endl;
		   int width;cin>>width;
		   Point tl(100,100);    // top-left corner of our window
		   Simple_window win(tl,800,800,"Programming Assignment#6");
		   Vector_ref<Graph_lib::Rectangle>r;
		   int x,y;
		         for(int i=0;i<3;++i){
		       	  x=axis(width);
		       	  y=axis(width);
		       	  r.push_back( new Graph_lib:: Rectangle(Point(x,y),width,width));
		       	  r[r.size()-1].set_fill_color(Color::red);     // color the inside of the rectangle
		       	  win.attach(r[r.size()-1]);
            }
		    win.set_label("Programming Assignment#6");
		    win.wait_for_button();               // Display!

	}
	catch(exception& e) {
	    // some error reporting
	    return 1;
	}
	catch(...) {
	    // some more error reporting
	    return 2;
	}

	int axis(int value){
	    int x;
		    if(value<600&&value>=500){x=rand()%99+1;}
		    else if(value<500&&value>=400){x=rand()%199+1;}
		    else if(value<400&&value>=300){x=rand()%299+1;}
		    else if(value<300&&value>=200){x=rand()%399+1;}
		    else if(value<200&&value>=100){x=rand()%499+1;}
		    else{x=rand()%599;}
		  return x;
	}


1
2
3
bool instersect(Rectangle &r1, Rectangle &r2, Rectangle&result){
//Should I compare Points and width in here?
}
Last edited on
Topic archived. No new replies allowed.