8757c83caac441bebf0aa41616fa1ed53fa12021
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Box.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.geometry;
11
12 /**
13  * Same as: 3D rectangle, rectangular box, rectangular parallelopiped, cuboid,
14  * rhumboid, hexahedron, rectangular prism.
15  */
16 public class Box implements Cloneable {
17
18     public final Point3D p1;
19     public final Point3D p2;
20
21     public Box() {
22         p1 = new Point3D();
23         p2 = new Point3D();
24     }
25
26     public Box(final Point3D p1, final Point3D p2) {
27         this.p1 = p1;
28         this.p2 = p2;
29     }
30
31     public Box addBorder(final double border) {
32
33         if (p1.x < p2.x) {
34             p1.translateX(-border);
35             p2.translateX(border);
36         } else {
37             p1.translateX(border);
38             p2.translateX(-border);
39         }
40
41         if (p1.y < p2.y) {
42             p1.translateY(-border);
43             p2.translateY(border);
44         } else {
45             p1.translateY(border);
46             p2.translateY(-border);
47         }
48
49         if (p1.z < p2.z) {
50             p1.translateZ(-border);
51             p2.translateZ(border);
52         } else {
53             p1.translateZ(border);
54             p2.translateZ(-border);
55         }
56
57         return this;
58     }
59
60     @Override
61     public Box clone() {
62         return new Box(p1.clone(), p2.clone());
63     }
64
65     public double getDepth() {
66         return Math.abs(p1.z - p2.z);
67     }
68
69     public double getHeight() {
70         return Math.abs(p1.y - p2.y);
71     }
72
73     public double getWidth() {
74         return Math.abs(p1.x - p2.x);
75     }
76
77     public void setSizeCentered(final Point3D size) {
78         p2.clone(size).scaleDown(2);
79         p1.clone(p2).invert();
80     }
81
82 }