Area of overlapping rectangles

Hello. I have a task and it says that I gotta find the area of the overlap of 2 rectangles by entering their bottom left coordinate /x,y/, height and weight. Can't use structures and classes. I just don't know how to find the area by javing that info. Thanks.
You can calculate the (x,y) coordinates of all of the other corners of the rectangle using the bottom-left (x,y) coordinate and the width & height of the rectangle.

So now you have the coordinates of each corner of your two rectangles R1 and R2. Two of the corners of the rectangle in the overlap region, R3, will be shared with corners of R1 & R2. In the example below:

 
              +----------+
              |       R2 |
 +----------------+      |
 |R1          |R3 |      |
 |            +---|------+
 |                |
 |                |
 +----------------+


The bottom-left corner of R3 is the bottom-left corner of R2. The top-right corner of R3 is the top-right corner of R1.

So the width of R3 is
x (top-right R1) - x (bottom-left R2)

The height of R3 is
y (top-right R1) - y (bottom-left R2)


If you can get something working for the above case then you should be able to extend it to work for different relative orientations of R1 & R2.

Good luck!
Last edited on
okay so for calculating the other coordinates.. is this okay?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
double x1 = 0, y1 = 0, x2 = 0, y2 = 0; //bottom left coordinates
	cout << "Enter the coordnates of the first rectangle: " << endl;
	cin >> x1 >> y1;
	cout << "Enter the coordinates of the second rectangle: " << endl;
	cin >> x2 >> y2;
	double height1 = 0, width1 = 0, height2 = 0, width2 = 0;
	cout << "Enter the height and the width of the first rectangle: " << endl;
	cin >> height1 >> width1;
	cout << "Enter the height and the width of the second rectangle: " << endl;
	cin >> height2 >> width2;

	if (height1 < 0 || height2 < 0 || width1 < 0 || width2 < 0)
	{
		cout << "invalid" << endl;
		return 1;
	}

	double upperLeftX1 = 0, upperLeftY1 = 0, upperLeftX2 = 0, upperLeftY2 = 0;
	upperLeftX1 = x1;
	upperLeftY1 = y1 + height1;
	upperLeftX2 = x2;
	upperLeftY2 = y2 + height2;
Last edited on
Looks good to me - just need to add the calculations for upperRight & lowerRight to complete the set of coordinates.
Yes, I did them but now I am confused about the intersecting area, I mean the area of the place where the 2 rectangles overlap because we can have many diff cases.
Can I use something like that:

left=max(bottomLeftX1, bottomLeftX2);
right=min(upperRightX1, upperRightX2);
bottom=max(bottomLeftY1, bottomLeftY2);
top=min(upperRightY1, upperRghtY2);

and the the area of the intersection is =(right-left)*(top-bottom)

cause i tried and i got as a result -2
so maybe the intersection i gotta use abs?
This is a diagram showing my naming of the left & right x-coordinates of the two rectangles and the top & bottom y-coordinates of the two rectangles:

  (l1,t1)  (r1,t1)
      +------+
      |      |
      |      |
      |R1    |          
      +------+          
  (l1,b1)  (r1,b1)

  
  (l2,t2)  (r2,t2)
      +------+
      |      |
      |      |
      |R2    |          
      +------+          
  (l2,b2)  (r2,b2)

Here are the 4 main cases for the rectangle overlaps and the width & height calculations for each case:

+------------------------+
|  Case a)               |
|                        |
|          +-------+     |
|          |    R2 |     |
|      +------+    |     |
|      |   |R3|    |     |
|      |   +--|----+     |
|      |R1    |          |
|      +------+          |
|                        |
|  w = r1 - l2           |
|  h = t1 - b2           |
|                        |
+------------------------+
|  Case b)               |
|                        |
|      +------+          |
|      |      |          |
|      |   +--|----+     |
|      |R1 |R3|    |     |
|      +------+    |     |
|          |    R2 |     |
|          +-------+     |
|                        |
|  w = r1 - l2           |
|  h = t2 - b1           |
|                        |
+------------------------+
|  Case c)               |
|                        |
|      +------+          |
|      |    R1|          |
| +----|--+   |          |
| |    |R3|   |          |
| |    +------+          |
| |R2     |              |
| +-------+              |
|                        |
|  w = r2 - l1           |
|  h = t2 - b1           |
|                        |
+------------------------+
|  Case d)               |
|                        |
| +-------+              |
| |R2     |              |
| |    +------+          |
| |    |R3|   |          |
| +----|--+   |          |
|      |    R1|          |
|      +------+          |
|                        |
|  w = r2 - l1           |
|  h = t1 - b2           |
|                        |
+------------------------+

So the width of R3 is either r2 - l1 or r1 - l2. You could calculate both of these: the one that is +ve will be the correct value; the one that is -ve will be the incorrect value. You can do the same for the height.

There are other cases beyond just these 4, but getting your program working for these 4 would be a good place to start.
Last edited on
1
2
3
4
5
6
7
left=max(bottomLeftX1, bottomLeftX2);
right=min(upperRightX1, upperRightX2);
bottom=max(bottomLeftY1, bottomLeftY2);
top=min(upperRightY1, upperRightY2);

if ( right > left && top > bottom ) area = (right-left)*(top-bottom);
else                                area = 0.0;

Last edited on
Topic archived. No new replies allowed.