If I have two rectangles whose positions are deifned using two 2D vectors (i.e. top left, bottom right) how can I check for the points they are intersecting?
I assume you actually want the result of the intersection, not only the test if both rectangles intersect.
The intersection of rect1 = (l1, t1, r1, b1) and rect2 = (l2, t2, r2, b2) is again a rectangle:
rectIntersection = ( max(l1, l2), max(t1, t2), min(r1, r2), min(b1, b2) )
rectIntersection is of course empty if left >= right || top >= bottom assuming a rectangle is left/top-inclusive and right/bottom-exclusive.
The rectangles intersect if
l1 < r2 && l2<r1 && t1<b2 && t2<t1
Assuming the origin is on left-top of the screen.
If Check if the the top left of one rectangle (x3,y3) is lesser than the bottom right of another rectangle (x2,y2) then the two are intersecting.
The points are (x2,|y2-y3|) and (|x2-x3|,y2).
This is for a rectangle1 and reactangle2 to the right of rectangle1.
Apply inverse to the left translation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With