2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.geometry;
7 import static java.lang.Math.abs;
10 * Same as: 3D rectangle, rectangular box, rectangular parallelopiped, cuboid,
11 * rhumboid, hexahedron, rectangular prism.
13 public class Box implements Cloneable {
16 * The first point of the box.
18 public final Point3D p1;
20 * The second point of the box.
22 public final Point3D p2;
25 * Creates a new box with two points at the origin.
33 * Creates a new box with two points at the specified coordinates.
35 public Box(final Point3D p1, final Point3D p2) {
42 * Enlarges the box by the specified border in all directions.
44 * @param border The border to enlarge the box by.
45 * If the border is negative, the box will be shrunk.
46 * @return The current box.
48 public Box enlarge(final double border) {
51 p1.translateX(-border);
52 p2.translateX(border);
54 p1.translateX(border);
55 p2.translateX(-border);
59 p1.translateY(-border);
60 p2.translateY(border);
62 p1.translateY(border);
63 p2.translateY(-border);
67 p1.translateZ(-border);
68 p2.translateZ(border);
70 p1.translateZ(border);
71 p2.translateZ(-border);
79 return new Box(p1.clone(), p2.clone());
83 * @return The depth of the box. The depth is the distance between the two points on the z-axis.
85 public double getDepth() {
86 return abs(p1.z - p2.z);
90 * @return The height of the box. The height is the distance between the two points on the y-axis.
92 public double getHeight() {
93 return abs(p1.y - p2.y);
97 * @return The width of the box. The width is the distance between the two points on the x-axis.
99 public double getWidth() {
100 return abs(p1.x - p2.x);
105 * Sets the size of the box. The box will be centered at the origin.
106 * Previous size and position of the box will be lost.
108 * @param size {@link Point3D} specifies box size in x, y and z axis.
110 public void setBoxSize(final Point3D size) {
111 p2.clone(size).scaleDown(2);
112 p1.clone(p2).invert();