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