Checking the intersection points between two rectangles?

If I have two rectangles whose positions are being distorted using two 2D vectors (i.e. top left, bottom right), how can I check the points that they intersect?

+3
source share
4 answers

I assume that you really want to get the result of the intersection, not just 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, of course, is empty if left >= right || top >= bottom, assuming the rectangle is left / top and right / bottom exception.

Rectangles intersect if

l1 < r2 && l2<r1 && t1<b2 && t2<t1
+4

, left-top .

, (x3,y3) , (x2,y2), .

(x2,|y2-y3|) (|x2-x3|,y2).

1 reactangle2 1.

.

+2

, , , X, Y, . {T1, L1, B1, R1} {T2, L2, B2, R2} (, , , ). , (X>L1) (X<R1) (Y>T1) (Y<B1), 2. (X>L1) (X<R2) , (L1<R2). , (L2<R1), (T1<B2) (T2<B1).

4 , , . , , .

+1
source

If you are more interested in a function for completing a task than for implementing an algorithm, the
IntersectRect function in Windows.

0
source

Source: https://habr.com/ru/post/1724722/


All Articles