Archive 17/01/2023.

[SOLVED] finding if 2 Rects intersect

slapin

Hi, all!

A simple question - I have 2 Rects
for example


Rect r1(-500, -500, 500, 500);
Rect r2(-500, -100, 300, 5);

and I need to check if these are intersecting. How to do that in most effective way in AngelScript?
I need it for quadtree implementation.

slapin

For reference - if rectangkes do not intersect, the Clip result will have infinite size.
I.e.

Rect r1(-1000, -1000,  -500, -500);
Rect r2(500, 5000,  1000, 1000);
r1.Clip(r2);
if (r1.length == M_INFINITY)
    Print("do not intersect")
else
    Print("intersect")

So this is a way to test for intersection.

cadaver

Would make sense to add a similar IsInside() overload as for BoundingBox, which would return the intersection result.

slapin

Yeah, I think that would be more logical way.

TheComet

QRect (from Qt) overloads the binary | & and bool operators, which will return a new rect that contains both, a new rect that defines the intersection, and returns true if position and size are not 0, respectively. I feel like those should be added to Urho as well. Checking if they intersect would then look like:

if(r1 & r2) { // the two intersect } else { // no intersection }