检查两个框是否重叠的算法 [英] Algorithm to check if two boxes overlap

查看:160
本文介绍了检查两个框是否重叠的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经了解了矩形的算法,但是对于x,y,z和height为给定值的框感到困惑。不重叠的条件是
1)框B上方的A框
2)框B下方的A框
3)框B的左侧A框
4)框A的右侧方框B

I have understood the algorithm in case of rectangles but I am confused with the boxes with x, y, z and height as value given. The conditions for not overlapping are 1) Box A above Box B 2) Box A below Box B 3) Box A left of Box B 4) Box A right of Box B

我正确吗?请指出一些遗漏的点。

Am I correct? Please guide some missing points.

推荐答案

两个轴对齐的框(任意尺寸)在且仅当所有轴的投影都重叠时交叠。轴投影只是该轴的坐标范围。

Two axes aligned boxes (of any dimension) overlap if and only if the projections to all axes overlap. The projection to an axis is simply the coordinate range for that axis.

上图中的蓝色和绿色框重叠,因为它们在两个轴上的投影重叠。蓝色和橙色框不重叠,因为它们在x轴上的投影不重叠(请注意,它们在y轴上的投影 do 重叠)。绿色和橙色框不重叠,因为它们在y轴上的投影不重叠(而它们在x轴上的投影确实重叠)。

The blue and the green boxes in the image above overlap because their projections to both axes overlap. The blue and the orange box do not overlap, because their projections to the x-axis do not overlap (note that their projections to the y-axis do overlap). The green and the orange box do not overlap, because their projections to the y-axis don't overlap (while their projections to the x-axis do overlap).

因此,当涉及到一维盒(间隔)的代码时,我们有:

So when it comes to code for 1D boxes (intervals) we have:

box1 = (xmin1, xmax1)
box2 = (xmin2, xmax2)
isOverlapping1D(box1,box2) = xmax1 >= xmin2 and xmax2 >= xmin1

对于2D盒子(矩形),我们有:

For 2D boxes (rectangles) we have:

box1 = (x:(xmin1,xmax1),y:(ymin1,ymax1))
box2 = (x:(xmin2,xmax2),y:(ymin2,ymax2))
isOverlapping2D(box1,box2) = isOverlapping1D(box1.x, box2.x) and 
                             isOverlapping1D(box1.y, box2.y)

对于3D我们拥有的盒子:

For 3D boxes we have:

box1 = (x:(xmin1,xmax1),y:(ymin1,ymax1),z:(zmin1,zmax1))
box2 = (x:(xmin2,xmax2),y:(ymin2,ymax2),z:(zmin2,zmax2))
isOverlapping3D(box1,box2) = isOverlapping1D(box1.x, box2.x) and 
                             isOverlapping1D(box1.y, box2.y) and
                             isOverlapping1D(box1.z, box2.z)

这篇关于检查两个框是否重叠的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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