计算 x/y 网格上两个矩形之间的重叠? [英] Calculate overlap between two rectangles on x/y grid?

查看:35
本文介绍了计算 x/y 网格上两个矩形之间的重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在上一个问题中,我询问了一种计算此网格中两点之间距离的方法.原来是欧几里得距离:

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

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

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

解决方案

首先,计算每个矩形的 x 和 y 范围(因为你有一个圆环几何,所以它是 mod gridsize).

所以,给定你的 Rectangle-1,计算:

<块引用>

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

对于 Rectangle-2 也是如此:

<块引用>

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

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

<块引用>

Reactangle-1: x-regions: (0, 20)y 区域:(0, 20)矩形 2:x 区域:(495, 500), (0, 5)y 区域:(0, 10)

如果两个矩形之间的任何(两个)x 和 y 区域具有非空交点,则您的矩形重叠.这里 Rectangle-1 的 (0, 20) x 区域和 Rectangle-2 的 (0, 5) x 区域有一个非零交点, (0, 20) 和 (0, 10) y 也是如此-地区.

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)

解决方案

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.

这篇关于计算 x/y 网格上两个矩形之间的重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆