Algorithm for finding a rectangle limited by its parent

Basically what I want to do is illustrated here: alt text

I start with A and B, then B matches A to create C.

The idea is that given the rectangles TLBR A, B make C

I also need to know if it creates an empty rectangle (B outside of case A).

I tried this, but it just does not do what I want:

if(clipRect.getLeft() > rect.getLeft())
    L = clipRect.getLeft();
else
    L = rect.getLeft();

if(clipRect.getRight() < rect.getRight())
    R = clipRect.getRight();
else
    R = rect.getRight();

if(clipRect.getBottom() > rect.getBottom())
    B = clipRect.getBottom();
else
    B = rect.getBottom();

if(clipRect.getTop() < rect.getTop())
    T = clipRect.getTop();
else
    T = rect.getTop();

if(L < R && B < T)
{
    clipRect = AguiRectangle(0,0,0,0);
}
else
{
    clipRect = AguiRectangle::fromTLBR(T,L,B,R);
}

thank

+3
source share
2 answers

You seem to be mistaken in the latter condition by checking if the intersection rectangle is empty.

You check L < R && B < T, but it looks like the condition for an empty rectangle should be:

L > R || B < T.

, Min Max. :

if (x < y)
    a = x;
else
    a = y;

a = Min(x, y);

Edit

, . . (, , y.

+3

, . is_intersected(), .

, , :

C.left.x = max(A.left.x, B.left.x);
C.right.x = min(A.right.x, B.right.x);

C.left.y = max(A.left.y, B.left.y);
C.right.y = min(A.right.y, B.right.y);
+1

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


All Articles