计算 x/y 网格上两个矩形之间的重叠?

发布于 2024-08-19 08:26:28 字数 481 浏览 9 评论 0原文

我需要计算两个矩形在特殊 x/y 网格上的重叠(数量或是/否)。网格为 500x500,但边和角相连(连续)。所以499之后的下一个点又变成0了。

在上一个问题中,我询问了一种计算该网格中两点之间的距离的方法。这就是欧几里得距离:

sqrt(min(|x1 - x2|, gridwidth - |x1 - x2|)^2 + min(|y1 - y2|, gridheight - |y1-y2|)^2)

计算两个矩形(由点 (x,y)、宽度和高度定义)是否在此网格中重叠的良好数学方法是什么?

矩形-1 ([x=0,y=0], w=20, h=20) 和矩形-2 ([x=495,y=0], w= 10, h=10) 应该有重叠。重叠的矩形(不是真正需要的,但)应该是([x=0,y=0], w=5, h=10

I need to calculate the overlap (amount or yes/no) that two rectangles make on a special x/y grid. The grid is 500x500 but the sides and corners connect (are continuous). So the next point after 499 becomes 0 again.

In a previous question I asked for a way to calculate the distance between two points in this grid. This turned out to be the Euclidean distance:

sqrt(min(|x1 - x2|, gridwidth - |x1 - x2|)^2 + min(|y1 - y2|, gridheight - |y1-y2|)^2)

What is the good mathematical way of calculating if two rectangles (defined by a point (x,y), width and height) have overlap in this grid?

Rectangle-1 ([x=0,y=0], w=20, h=20) and Rectangle-2 ([x=495,y=0], w=10, h=10) should have overlap. The overlapping rectangle (not really needed but) should be ([x=0,y=0], w=5, h=10)

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

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

发布评论

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

评论(1

泪是无色的血 2024-08-26 08:26:28

首先,计算每个矩形的 x 和 y 范围
(因为你有一个圆环几何体,所以可以对网格大小进行修改)。

因此,给定您的 Rectangle-1,计算:

<前><代码>x1 = x = 0,x2 = x + w =​​ 20
y1 = y = 0,y2 = y + h = 20

矩形 2 相同:

<前><代码>x3 = 495,x4 = 505 mod 500 = 5
y3 = 0,y4 = 10

为每个矩形创建 x 和 y“区域”:

Reactangle-1:x 区域:(0, 20)
              y 区域:(0, 20)

矩形 2:x 区域:(495, 500), (0, 5)
              y 区域:(0, 10)

如果两个矩形之间的任何(两个)x 和 y 区域具有非空交集,则矩形重叠。
这里,矩形 1 的 (0, 20) x 区域和矩形 2 的 (0, 5) x 区域有非空交集,(0, 20) 和 (0, 10) y 也有非空交集-地区。

First, compute the x and y range for each rectangle
(because you have a torus geometry do it mod gridsize).

So, given your Rectangle-1, compute:

x1 = x = 0, x2 = x + w = 20
y1 = y = 0, y2 = y + h = 20

Same for Rectangle-2:

x3 = 495, x4 = 505 mod 500 = 5
y3 = 0,   y4 = 10

Create the x and y "regions" for each rectangle:

Reactangle-1: x-regions: (0, 20)
              y-regions: (0, 20)

Rectangle-2:  x-regions: (495, 500), (0, 5)
              y-regions: (0, 10)

If any (both) x and y regions between the two rectangles have a non-null intersection, then your rectangles overlap.
Here the (0, 20) x-region of Rectangle-1 and the (0, 5) x-region of Rectangle-2 have a non-null intersection and so do the (0, 20) and (0, 10) y-regions.

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