重叠的立方体

时间:2011-02-15 21:09:53

标签: geometry

我正在尝试确定两个立方体是否重叠。我已经阅读了overlapping rectangles,但我不确定如何将其翻译成第三维。

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

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

5 个答案:

答案 0 :(得分:8)

您应该能够轻松地将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.Y2 < CubeA.Y1

所以没有重叠的条件是:

Cond1 or Cond2 or Cond3 or Cond4 or Cond5 or Cond6

因此,重叠的充分条件恰恰相反(De Morgan)

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

答案 1 :(得分:3)

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

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

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

您链接的帖子可以轻松扩展。只需添加Z。

答案 2 :(得分:2)

公认的答案是错误的,非常令人困惑。这是我想出的。

确定x平面上的重叠 void setupMPU(){ Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2) Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28) Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9) Wire.endTransmission(); Wire.beginTransmission(0b1101000); //I2C address of the MPU Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4) Wire.write(0x00010000); //Setting the gyro to full scale +/- 250deg./s Wire.endTransmission(); Wire.beginTransmission(0b1101000); //I2C address of the MPU Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5) Wire.write(0b00010000); //Setting the accel to +/- 2g Wire.endTransmission(); }

确定y平面中的重叠 if (cubeA.maxX > cubeB.minX) if (cubeA.minX < cubeB.maxX)

确定在z平面上的重叠 if (cubeA.maxY > cubeB.minY) if (cubeA.minY < cubeB.minY)

如果您将所有这些条件和在一起并且结果为假,则您知道多维数据集在某个点相交。

信用:https://silentmatt.com/rectangle-intersection/

答案 3 :(得分:0)

我想(没想太多,也许我的条件还不够)检查第一个立方体的所有顶点是否在第二个立方体之外且反向:第二个立方体的所有顶点都在第一个立方体之外。

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

答案 4 :(得分:0)

这只是改正后被接受的答案。它进行测试以查看两个对齐的长方体在X,Y和Z轴上是否有任何共同的部分,如果没有,则不可能发生碰撞。 该函数假定存在碰撞并执行测试以检查是否没有碰撞。

Function func_Intersect(ByVal cuboid1_MinX As Double, ByVal cuboid1_MaxX As Double, ByVal cuboid1_MinY As Double, ByVal cuboid1_MaxY As Double, ByVal cuboid1_MinZ As Double, ByVal cuboid1_MaxZ As Double, ByVal cuboid2_MinX As Double, ByVal cuboid2_MaxX As Double, ByVal cuboid2_MinY As Double, ByVal cuboid2_MaxY As Double, ByVal cuboid2_MinZ As Double, ByVal cuboid2_MaxZ As Double) As Boolean
    func_Intersect = True
    If cuboid1_MaxX < cuboid2_MinX Then
        func_Intersect = False
    ElseIf cuboid2_MaxX < cuboid1_MinX Then
        func_Intersect = False
    ElseIf cuboid1_MaxY < cuboid2_MinY Then
        func_Intersect = False
    ElseIf cuboid2_MaxY < cuboid1_MinY Then
        func_Intersect = False
    ElseIf cuboid1_MaxZ < cuboid2_MinZ Then
        func_Intersect = False
    ElseIf cuboid2_MaxZ < cuboid1_MinZ Then
        func_Intersect = False
    End If
End Function
相关问题