计算 x/y 网格上两个矩形之间的重叠?
我需要计算两个矩形在特殊 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,计算每个矩形的 x 和 y 范围
(因为你有一个圆环几何体,所以可以对网格大小进行修改)。
因此,给定您的 Rectangle-1,计算:
矩形 2 相同:
为每个矩形创建 x 和 y“区域”:
如果两个矩形之间的任何(两个)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:
Same for Rectangle-2:
Create the x and y "regions" for each rectangle:
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.