2011-02-15 91 views
5

我想确定两个立方体是否重叠。我已阅读overlapping rectangles,但我不确定如何将其翻译为第三维。重叠立方体

我的目标是生成一些随机定位和大小不重叠的立方体。

这些立方体表示在x,y,z笛卡尔平面上。

回答

6

您应该可以很容易地修改Determine if two rectangles overlap each other?以达到您的目的。

假设您有CubeACubeB。的6个条件中的任何一个保证没有重叠可存在:

Cond1. If A's left face is to the right of the B's right face, 
      - then A is Totally to right Of B 
       CubeA.X2 < CubeB.X1 
Cond2. If A's right face is to the left of the B's left face, 
      - then A is Totally to left Of B 
       CubeB.X2 < CubeA.X1 
Cond3. If A's top face is below B's bottom face, 
      - then A is Totally below B 
       CubeA.Z2 < CubeB.Z1 
Cond4. If A's bottom face is above B's top face, 
      - then A is Totally above B 
       CubeB.Z2 < CubeA.Z1 
Cond5. If A's front face is behind B's back face, 
      - then A is Totally behind B 
       CubeB.Y2 < CubeA.Y1 
Cond6. If A's left face is to the left of B's right face, 
      - then A is Totally to the right of B 
       CubeB.X2 < CubeA.X1 

所以对于没有重叠的条件是:

Cond1 or Cond2 or Cond3 or Cond4 or Cond5 or Cond6 

因此,对于重叠的充分条件是相反的(德摩根)

Not Cond1 AND Not Cond2 And Not Cond3 And Not Cond4 And Not Cond5 And Not Cond6 
+0

你是什么意思的边缘,可能是面孔? – 2011-02-16 00:56:39

+0

@Dave,你说得对。改变。 – btilly 2011-02-16 01:13:48

+0

您的算法是否假定立方体是轴对齐的? Sry打扰你^^ – 2011-02-16 02:58:08

3

立方体由6个矩形(好的,方形)面组成。

如果满足以下条件,两个立方体不会相交。

  • 2个立方体的面没有相交。
  • 一个立方体不完全包含其他。

您链接的帖子可以很容易地扩展。只需加上Z.

0

我想(没多想,也许我的条件不够)检查第一个立方体的所有顶点是否超出第二个,并且是倒数:第二个顶点的所有顶点都不在第一个顶点。

若要检查顶点是否在立方体中,请将其坐标转换为与立方体相关的坐标系(将平移应用于立方体中心和立方体旋转)。然后简单地检查每个坐标(x,y,z)是否小于半边