查找受其父级约束的矩形的算法

发布于 2024-10-09 18:09:34 字数 798 浏览 0 评论 0原文

基本上我想要做的事情如下所示: alt text

我从 A 和 B 开始,然后 B 符合 A 来创建 C。

这个想法是,给定 TLBR 矩形A,B,使 C

我还需要知道它是否产生一个空矩形(B 在 A 情况之外)。

我尝试过,但它没有达到我想要的效果:

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);
}

谢谢

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

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

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

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

I tried this but it just isn't doing 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);
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归途 2024-10-16 18:09:35

您在检查相交矩形是否为空的最终条件中似乎存在错误。

您检查 L <研发B< T,但似乎空矩形的条件应该是:

L >右|| B< T。

顺便说一句,您可以使用 MinMax 函数使代码更简单、更易于阅读。您有很多这种模式:

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

可以简单地写为

a = Min(x, y);

编辑

另一个错误是您采用了最大底部和最小顶部。您应该采用最小底部和最大顶部。 (假设矩形对应于屏幕坐标,其中顶部实际上具有较低的 y 值。

You seem to have a mistake in the final condition checking whether or not the intersection rectangle is empty.

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

L > R || B < T.

By the way, you can make your code a little simpler and easier to read by using Min and Max functions. You have a lot of this pattern:

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

Which can be written simply as

a = Min(x, y);

Edit

Another mistake is that you take the maximum bottom and the minimum top. You should be taking the minimum bottom and the maximum top. (Assuming the rectangles correspond to screen coordinates, where the top actuallly has lower y values.

电影里的梦 2024-10-16 18:09:35

从逻辑上讲,这是两个不同的问题。我首先会编写一个返回适当布尔值的 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);

Logically, these are two different problems. I would first write an is_intersected() function returning an appropriate boolean value.

If the rects do intersect, I would then perform a clip operation that resembled the following pseudocode:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文