conor conor - 2 months ago 17
C Question

C - Calculate area between two rectangles

I have been searching for solutions to my problem but haven't found any that I could understand fully, feel free to link to solutions.

I want to calculate the x,y co-ordinates of a rectangle which is the intersection of two other 2D, normal rectangles:

(x0,y0)
+------------+
| |
| (x4,y4) |
| +----------+
| | | |
+-------|----+ |
| (x2,y2) |
| |
+----------+
(x5,y5)


Basically I just need co-ordinates for the intersection rectangle. I'll be implementing this in C but the answer can be in pseudo code.

Thanks

Edit: what i am looking for is an algorithm to find the rectangle of intersection between any two 2d, normal rectangles, not a solution solely for the example above

Answer

The co-ordinates of the top-left corner are given by: (max(x4, x0), max(y4, y0)).

The co-ordinates of the bottom-right corner are given by: (min(x2, x5), min(y2, y5)).

If max(x4, x0) > min(x2, x5) or max(y4,y0) > min(y2, y5) then there is no intersection.