Formatting update
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Box.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.geometry;
6
7 /**
8  * Same as: 3D rectangle, rectangular box, rectangular parallelopiped, cuboid,
9  * rhumboid, hexahedron, rectangular prism.
10  */
11 public class Box implements Cloneable {
12
13     public final Point3D p1;
14     public final Point3D p2;
15
16     public Box() {
17         p1 = new Point3D();
18         p2 = new Point3D();
19     }
20
21     public Box(final Point3D p1, final Point3D p2) {
22         this.p1 = p1;
23         this.p2 = p2;
24     }
25
26     public Box addBorder(final double border) {
27
28         if (p1.x < p2.x) {
29             p1.translateX(-border);
30             p2.translateX(border);
31         } else {
32             p1.translateX(border);
33             p2.translateX(-border);
34         }
35
36         if (p1.y < p2.y) {
37             p1.translateY(-border);
38             p2.translateY(border);
39         } else {
40             p1.translateY(border);
41             p2.translateY(-border);
42         }
43
44         if (p1.z < p2.z) {
45             p1.translateZ(-border);
46             p2.translateZ(border);
47         } else {
48             p1.translateZ(border);
49             p2.translateZ(-border);
50         }
51
52         return this;
53     }
54
55     @Override
56     public Box clone() {
57         return new Box(p1.clone(), p2.clone());
58     }
59
60     public double getDepth() {
61         return Math.abs(p1.z - p2.z);
62     }
63
64     public double getHeight() {
65         return Math.abs(p1.y - p2.y);
66     }
67
68     public double getWidth() {
69         return Math.abs(p1.x - p2.x);
70     }
71
72     public void setSizeCentered(final Point3D size) {
73         p2.clone(size).scaleDown(2);
74         p1.clone(p2).invert();
75     }
76
77 }