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

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

中的isnesting方法 Box 负责检查一个长方体示例是否完全嵌套在另一个长方体示例中,方法是检查每个维度是否在父长方体的边界内。返回true的示例。返回false的示例。

  1. class Box {
  2. private float width;
  3. private float height;
  4. private float length;
  5. Box(float width, float height, float length) {
  6. this.width = width;
  7. this.height = height;
  8. this.length = length;
  9. }
  10. public boolean isNesting(Box outsideBox) {
  11. if(this.length <= outsideBox.length && this.width <= outsideBox.width && this.height <= outsideBox.height)
  12. return true;
  13. return false;
  14. }
  15. public Box biggerBox(Box oldBox) {
  16. return new Box(1.25f*oldBox.getWidth(), 1.25f*oldBox.getHeight(), 1.25f*oldBox.getLength());
  17. }
  18. public Box smallerBox(Box oldBox) {
  19. return new Box(.25f*oldBox.getWidth(), .25f*oldBox.getHeight(), .25f*oldBox.getLength());
  20. }
  21. }

但是,这里的问题是,这种方法不包括smallerbox或basebox可能具有的不同旋转。我该如何整合这个逻辑?

  1. class BoxTester {
  2. public static void main(String[] args){
  3. Box baseBox = new Box(2.5f, 5.0f, 6.0f);
  4. Box biggerBox = baseBox.biggerBox(baseBox);
  5. Box smallerBox = baseBox.smallerBox(baseBox);
  6. System.out.println(baseBox.isNesting(smallerBox));
  7. }
  8. }
0s0u357o

0s0u357o1#

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

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

相关问题