簡體   English   中英

如何檢查矩形棱柱是否完全嵌套在另一個棱柱中,包括旋轉

[英]How to check if a rectangular prism is fully nested within another, including rotation

Box中的 isNesting 方法負責檢查一個盒子實例是否完全嵌套在另一個盒子中,方法是檢查每個維度是否在父盒子的邊界內。 返回 true 的示例 返回 false 的示例

class Box {

  private float width;
  private float height;
  private float length;

  Box(float width, float height, float length) {
    this.width = width;
    this.height = height;
    this.length = length;
  }
  public boolean isNesting(Box outsideBox) {
    if(this.length <= outsideBox.length && this.width <= outsideBox.width && this.height <= outsideBox.height)
      return true;

    return false;
  }

  public Box biggerBox(Box oldBox) {
    return new Box(1.25f*oldBox.getWidth(), 1.25f*oldBox.getHeight(), 1.25f*oldBox.getLength());
  }

  public Box smallerBox(Box oldBox) {
    return new Box(.25f*oldBox.getWidth(), .25f*oldBox.getHeight(), .25f*oldBox.getLength());
  }
}

但是,這里的問題是此方法不涵蓋 smallBox 或 baseBox 可能具有的不同旋轉。 我將如何結合這個邏輯?

class BoxTester {
  public static void main(String[] args){
    Box baseBox = new Box(2.5f, 5.0f, 6.0f);
    Box biggerBox = baseBox.biggerBox(baseBox);
    Box smallerBox = baseBox.smallerBox(baseBox);

    System.out.println(baseBox.isNesting(smallerBox));
  }
}

對兩個長方體的維度進行排序。

按順序比較相應的尺寸。

 public boolean isNesting(Box outsideBox) {
    if(this.dim[0] <= outsideBox.dim[0] && this.dim[1] <= outsideBox.dim[1] && this.dim[2] <= outsideBox.dim[2])
        return true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM