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

查看:160
本文介绍了计算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


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



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


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


与Rectangle-2相同:


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


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


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

Rectangangle-2:

  x区域:(495,500),(0,5)
y-regions:(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天全站免登陆