conor conor - 1 year ago 141
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:

| |
| (x4,y4) |
| +----------+
| | | |
+-------|----+ |
| (x2,y2) |
| |

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.


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download