/*
- * Sixth 3D engine. Author: Svjatoslav Agejenko.
+ * Sixth 3D engine. Author: Svjatoslav Agejenko.
* This project is released under Creative Commons Zero (CC0) license.
*/
package eu.svjatoslav.sixth.e3d.geometry;
+import static java.lang.Math.abs;
+
/**
* Same as: 3D rectangle, rectangular box, rectangular parallelopiped, cuboid,
* rhumboid, hexahedron, rectangular prism.
*/
public class Box implements Cloneable {
+ /**
+ * The first point of the box.
+ */
public final Point3D p1;
+ /**
+ * The second point of the box.
+ */
public final Point3D p2;
+ /**
+ * Creates a new box with two points at the origin.
+ */
public Box() {
p1 = new Point3D();
p2 = new Point3D();
}
+ /**
+ * Creates a new box with two points at the specified coordinates.
+ */
public Box(final Point3D p1, final Point3D p2) {
this.p1 = p1;
this.p2 = p2;
}
- public Box addBorder(final double border) {
+
+ /**
+ * Enlarges the box by the specified border in all directions.
+ *
+ * @param border The border to enlarge the box by.
+ * If the border is negative, the box will be shrunk.
+ * @return The current box.
+ */
+ public Box enlarge(final double border) {
if (p1.x < p2.x) {
p1.translateX(-border);
return new Box(p1.clone(), p2.clone());
}
+ /**
+ * @return The depth of the box. The depth is the distance between the two points on the z-axis.
+ */
public double getDepth() {
- return Math.abs(p1.z - p2.z);
+ return abs(p1.z - p2.z);
}
+ /**
+ * @return The height of the box. The height is the distance between the two points on the y-axis.
+ */
public double getHeight() {
- return Math.abs(p1.y - p2.y);
+ return abs(p1.y - p2.y);
}
+ /**
+ * @return The width of the box. The width is the distance between the two points on the x-axis.
+ */
public double getWidth() {
- return Math.abs(p1.x - p2.x);
+ return abs(p1.x - p2.x);
}
- public void setSizeCentered(final Point3D size) {
+
+ /**
+ * Sets the size of the box. The box will be centered at the origin.
+ * Previous size and position of the box will be lost.
+ *
+ * @param size {@link Point3D} specifies box size in x, y and z axis.
+ */
+ public void setBoxSize(final Point3D size) {
p2.clone(size).scaleDown(2);
p1.clone(p2).invert();
}
*/
package eu.svjatoslav.sixth.e3d.geometry;
+/**
+ * Circle in 2D space.
+ */
public class Circle {
Point2D center;
import static java.lang.Math.sqrt;
+/**
+ * Used to represent point in a 2D space or vector.
+ */
public class Point2D implements Cloneable {
public double x, y;
y = parent.y;
}
+
/**
- * Add other point to current point.
+ * Add other point to current point. Value of other point will not be changed.
*
* @return current point.
*/
return this;
}
+ /**
+ * @return true if current point coordinates are equal to zero.
+ */
public boolean isZero() {
return (x == 0) && (y == 0);
}
return new Point2D(this);
}
- public void clone(final Point2D parent) {
- x = parent.x;
- y = parent.y;
+ /**
+ * Copy coordinates from other point to current point. Value of other point will not be changed.
+ */
+ public void clone(final Point2D otherPoint) {
+ x = otherPoint.x;
+ y = otherPoint.y;
}
- public Point2D getMiddle(final Point2D p1, final Point2D p2) {
+ /**
+ * Set current point to middle of two other points.
+ *
+ * @param p1 first point.
+ * @param p2 second point.
+ * @return current point.
+ */
+ public Point2D setToMiddle(final Point2D p1, final Point2D p2) {
x = (p1.x + p2.x) / 2d;
y = (p1.y + p2.y) / 2d;
return this;
/**
* Compute distance to another point.
+ *
+ * @param anotherPoint point to compute distance to.
+ * @return distance from current point to another point.
*/
public double getDistanceTo(final Point2D anotherPoint) {
final double xDiff = x - anotherPoint.x;
return sqrt(((xDiff * xDiff) + (yDiff * yDiff)));
}
+ /**
+ * Calculate length of vector.
+ * @return length of vector.
+ */
public double getVectorLength() {
return sqrt(((x * x) + (y * y)));
}
+ /**
+ * Invert current point.
+ *
+ * @return current point.
+ */
public Point2D invert() {
x = -x;
y = -y;
return this;
}
+ /**
+ * Round current point coordinates to integer.
+ */
public void roundToInteger() {
x = (int) x;
y = (int) y;
}
/**
- * Subtract other point from current point.
+ * Subtract other point from current point. Value of other point will not be changed.
*
* @return current point.
*/
return this;
}
+ /**
+ * Convert current point to 3D point.
+ * Value of the z coordinate will be set to zero.
+ *
+ * @return 3D point.
+ */
public Point3D to3D() {
return new Point3D(x, y, 0);
}
+ /**
+ * Set current point to zero.
+ *
+ * @return current point.
+ */
public Point2D zero() {
x = 0;
y = 0;
public class Point3D implements Cloneable {
+ // coordinates
public double x, y, z;
public Point3D() {
}
/**
- * Add other point to current point.
+ * Add other point to current point. Value of other point will not be changed.
*
+ * @param otherPoint point to add.
* @return current point.
*/
public Point3D add(final Point3D otherPoint) {
return new Point3D(this);
}
- public Point3D clone(final Point3D parent) {
- x = parent.x;
- y = parent.y;
- z = parent.z;
+ /**
+ * Copy coordinates from other point to current point. Value of other point will not be changed.
+ */
+ public Point3D clone(final Point3D otherPoint) {
+ x = otherPoint.x;
+ y = otherPoint.y;
+ z = otherPoint.z;
return this;
}
+ /**
+ * Set current point coordinates to the middle point between two other points.
+ *
+ * @param p1 first point.
+ * @param p2 second point.
+ * @return current point.
+ */
public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
x = (p1.x + p2.x) / 2d;
y = (p1.y + p2.y) / 2d;
return this;
}
+ /**
+ * @return true if current point coordinates are equal to zero.
+ */
public boolean isZero() {
return (x == 0) && (y == 0) && (z == 0);
}
/**
* Compute distance to another point.
+ *
+ * @param anotherPoint point to compute distance to.
+ * @return distance to another point.
*/
public double getDistanceTo(final Point3D anotherPoint) {
final double xDelta = x - anotherPoint.x;
return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
}
+ /**
+ * @return length of current vector.
+ */
public double getVectorLength() {
return sqrt(((x * x) + (y * y) + (z * z)));
}
+ /**
+ * Invert current point coordinates.
+ *
+ * @return current point.
+ */
public Point3D invert() {
x = -x;
y = -y;
return this;
}
+ /**
+ * Rotate current point around center point by angleXZ and angleYZ.
+ * <p>
+ * See also: <a href="https://marctenbosch.com/quaternions/">Let's remove Quaternions from every 3D Engine</a>
+ *
+ * @param center center point.
+ * @param angleXZ angle around XZ axis.
+ * @param angleYZ angle around YZ axis.
+ */
public void rotate(final Point3D center, final double angleXZ,
final double angleYZ) {
final double s1 = sin(angleXZ);
z = z2 + center.z;
}
+ /**
+ * Round current point coordinates to integer values.
+ */
public void roundToInteger() {
x = (int) x;
y = (int) y;
z = (int) z;
}
+ /**
+ * Scale down current point by factor.
+ * All coordinates will be divided by factor.
+ *
+ * @param factor factor to scale by.
+ * @return current point.
+ */
public Point3D scaleDown(final double factor) {
x /= factor;
y /= factor;
return this;
}
+ /**
+ * Scale up current point by factor.
+ * All coordinates will be multiplied by factor.
+ *
+ * @param factor factor to scale by.
+ * @return current point.
+ */
public Point3D scaleUp(final double factor) {
x *= factor;
y *= factor;
return this;
}
+ /**
+ * Set current point coordinates to given values.
+ * @param x X coordinate.
+ * @param y Y coordinate.
+ * @param z Z coordinate.
+ */
public void setValues(final double x, final double y, final double z) {
this.x = x;
this.y = y;
}
/**
- * Subtract other point from current point.
+ * Subtract other point from current point. Value of other point will not be changed.
*
* @return current point.
*/
}
/**
+ * Translate current point along X axis by given increment.
+ *
* @return current point.
*/
public Point3D translateX(final double xIncrement) {
}
/**
+ * Translate current point along Y axis by given increment.
+ *
* @return current point.
*/
public Point3D translateY(final double yIncrement) {
}
/**
+ * Translate current point along Z axis by given increment.
+ *
* @return current point.
*/
public Point3D translateZ(final double zIncrement) {
}
/**
- * Resets point to 0 coordinate in X, Y and Z axis.
+ * Resets point coordinates to zero along all axes.
*
* @return current point.
*/
final double borderSize = 10;
- final Box borderArea = containingBox.clone().addBorder(borderSize);
+ final Box borderArea = containingBox.clone().enlarge(borderSize);
return new WireframeBox(borderArea, appearance);
}
}
private void setDimensions(final Point3D size) {
- containingBox.setSizeCentered(size);
+ containingBox.setBoxSize(size);
}
private void showBorder() {
public PolygonCoordinate getMiddlePoint() {
return new PolygonCoordinate(new Point3D().computeMiddlePoint(c1.space,
- c2.space), new Point2D().getMiddle(c1.texture,
+ c2.space), new Point2D().setToMiddle(c1.texture,
c2.texture));
}
}