java—如何检查矩形棱柱体是否完全嵌套在另一个棱柱体中,包括旋转

9ceoxa92  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(259)

中的isnesting方法 Box 负责检查一个长方体示例是否完全嵌套在另一个长方体示例中,方法是检查每个维度是否在父长方体的边界内。返回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());
  }
}

但是,这里的问题是,这种方法不包括smallerbox或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));
  }
}
0s0u357o

0s0u357o1#

对两个长方体的尺寸进行排序。
按顺序比较相应的尺寸。

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;

相关问题