Rectangle Intersection - Determine if two given rectangles intersect each other or not

Q: Rectangle Intersection: Determine if two given rectangles intersect each other or not

Definitions:

Intersect: Two Rectangles intersect each other if the share a common area.

Assumptions:

  • Both the rectangles are aligned with the x and y axes.
  • Rectangles just touching each other are considered as non-intersecting.

First let’s create a data structure for use in our problem. Rectangle Intersection algorithms are usually used in graphics problems. Depending on the precision needed to represent the co-ordinates, you may choose the required data type. In this case, I am going to use a LONG.

I am also basing the struct off of the Windows co-ordinate space, where the origin is on the top left of the screen. The x-axis moves towards the right and the y-axis moves towards the bottom.

struct
{
    LONG    left;
    LONG    top;
    LONG    right;
    LONG    bottom;
} RECT;

bool IntersectRect(const RECT * r1, const RECT * r2)
{
	return ! ( r2->left > r1->right
		|| r2->right < r1->left
		|| r2->top > r1->bottom
		|| r2->bottom < r1->top
		);
}

The above algorithm finds the conditions at which the rectangles do not intersect and then negates the values to get the result. There is a straight forward way of doing the same algorithm, but it requires far more conditions than the one above. Consider the diagram below


  S1   |         S2          |   S3
       |                     |
    TOP|LEFT                 |
-----------------------------------
       |                     |
       |                     |
       |                     |
  S4   |         S5          |   S6
       |                     |
       |                     |
-----------------------------------
       |                RIGHT|BOTTOM
       |                     |
  S7   |         S8          |   S9

This is an illustration of one of the rectangles (R1). The top left corner of R2 can lie in any of the 9 segments, S1 through S9. Based on each of these cases, and depending on the position of the other vertices, it can be determined whether the two rectangles R1 and R2 intersect each other. E.g. If R2’s top left corner is in S1, the bottom right corner needs to be in S5 or beyond (S5,S6,S8,S9) for the rectangles to intersect. Or if the top left corner of R2 is in S5, it is an automatic case of intersection. Clearly, in this case there are a lot of cases to consider and therefore and lot of conditions to check.

The conditions where the rectangles do not intersect, is where the bounds of one Rectangle is beyond the bounds of the other. The conditions are pretty straightforward as shown in the code above. Negating this value solves the original problem.

There is one more conditions (negation) that can be avoided by changing the inner condition. I will leave that as an exercise for the reader.

NOTE: For all invalid rectangles passed to the function, the return value is always false. You can also choose to return error codes after checking for validity.

Comments

I would like to see a

I would like to see a continuation of the topic

Is there a special reason

Is there a special reason why we need to use negation? It looks more natural to me in this form:
r2->left < r1->right && r2->right > r1->left && r2->top < r1->bottom && r2->bottom > r1->top

Thanks for pointing that

Thanks for pointing that out, Nerdler. I have corrected it.

"The top left corner of R2

"The top left corner of R2 can like in any of the 9 segments..."

Did you mean "lie in any of the 9 segments..." here?

I only mentioned that the

I only mentioned that the algorithm is based of the windows co-ordinate system. The decision of what defines a rectangle intersection rests with the application. Some application wants overlapping edges to count as an intersection, while others don't

in windows top,left is

in windows top,left is inclusive and right,bottom in exclusive (directx) then you should add some =

[...] In the last post, we

[...] In the last post, we looked into methods to determine whether or not two given rectangles intersect each other. Lets go one step ahead this time and find the intersecting rectangle. [...]

It is mentioned that y axis

It is mentioned that y axis moved towards the bottom.
So the above code is correct according to that assumption.

You say you assume:

You say you assume: "Rectangles just touching each other are considered as non-intersecting."

But by requiring the sides to be strictly unequal *before* negation, you consider rectangles that are just touching to interdsect. For example, if rectangle one shared its right edge with rectanlge two's left, the first comparison would be false and the negation could then be true. (In your next post, you swap the comparisons around disregarding equality, so this assumption is then correct.)

It depends on the

It depends on the co-ordinate system you are using

I think the inequalities of

I think the inequalities of the last two comparisons should be switched so you would have:
r2->top bottom ||
r2->bottom > r1->top

[...] In the last post, we

[...] In the last post, we looked into methods to determine whether or not two given rectangles intersect each other. Lets go one step ahead this time and find the intersecting rectangle. [...]

Just a note: the last 2

Just a note: the last 2 tests in the conditional for IntersectRect have the inequality reversed. It should be

r2->top bottom ||
r2->bottom > r1->top