From 3e2b906bd05617a4d9725de39ac14c9ea1f8736c Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Mon, 27 Feb 2023 00:53:42 +0200 Subject: [PATCH] Updated readability of the code. --- .../eu/svjatoslav/sixth/e3d/geometry/Box.java | 50 ++++++++++-- .../svjatoslav/sixth/e3d/geometry/Circle.java | 3 + .../sixth/e3d/geometry/Point2D.java | 55 +++++++++++-- .../sixth/e3d/geometry/Point3D.java | 78 +++++++++++++++++-- .../sixth/e3d/gui/GuiComponent.java | 4 +- .../renderer/raster/slicer/BorderLine.java | 2 +- 6 files changed, 170 insertions(+), 22 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Box.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Box.java index 6e3b1d7..e461531 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Box.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Box.java @@ -1,29 +1,51 @@ /* - * 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); @@ -57,19 +79,35 @@ public class Box implements Cloneable { 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(); } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Circle.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Circle.java index 36dd69c..5402fc4 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Circle.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Circle.java @@ -4,6 +4,9 @@ */ package eu.svjatoslav.sixth.e3d.geometry; +/** + * Circle in 2D space. + */ public class Circle { Point2D center; diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java index a82382f..c272047 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point2D.java @@ -6,6 +6,9 @@ package eu.svjatoslav.sixth.e3d.geometry; 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; @@ -23,8 +26,9 @@ public class Point2D implements Cloneable { 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. */ @@ -34,6 +38,9 @@ public class Point2D implements Cloneable { return this; } + /** + * @return true if current point coordinates are equal to zero. + */ public boolean isZero() { return (x == 0) && (y == 0); } @@ -43,12 +50,22 @@ public class Point2D implements Cloneable { 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; @@ -60,6 +77,9 @@ public class Point2D implements Cloneable { /** * 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; @@ -68,23 +88,35 @@ public class Point2D implements Cloneable { 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. */ @@ -94,10 +126,21 @@ public class Point2D implements Cloneable { 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; diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java index e2c9868..1c8700a 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Point3D.java @@ -12,6 +12,7 @@ import static java.lang.Math.*; public class Point3D implements Cloneable { + // coordinates public double x, y, z; public Point3D() { @@ -45,8 +46,9 @@ public class Point3D implements Cloneable { } /** - * 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) { @@ -60,13 +62,23 @@ public class Point3D implements Cloneable { 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; @@ -74,6 +86,9 @@ public class Point3D implements Cloneable { return this; } + /** + * @return true if current point coordinates are equal to zero. + */ public boolean isZero() { return (x == 0) && (y == 0) && (z == 0); } @@ -92,6 +107,9 @@ public class Point3D implements Cloneable { /** * 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; @@ -101,10 +119,18 @@ public class Point3D implements Cloneable { 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; @@ -112,6 +138,15 @@ public class Point3D implements Cloneable { return this; } + /** + * Rotate current point around center point by angleXZ and angleYZ. + *

+ * See also: Let's remove Quaternions from every 3D Engine + * + * @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); @@ -135,12 +170,22 @@ public class Point3D implements Cloneable { 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; @@ -148,6 +193,13 @@ public class Point3D implements Cloneable { 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; @@ -155,6 +207,12 @@ public class Point3D implements Cloneable { 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; @@ -162,7 +220,7 @@ public class Point3D implements Cloneable { } /** - * Subtract other point from current point. + * Subtract other point from current point. Value of other point will not be changed. * * @return current point. */ @@ -179,6 +237,8 @@ public class Point3D implements Cloneable { } /** + * Translate current point along X axis by given increment. + * * @return current point. */ public Point3D translateX(final double xIncrement) { @@ -187,6 +247,8 @@ public class Point3D implements Cloneable { } /** + * Translate current point along Y axis by given increment. + * * @return current point. */ public Point3D translateY(final double yIncrement) { @@ -195,6 +257,8 @@ public class Point3D implements Cloneable { } /** + * Translate current point along Z axis by given increment. + * * @return current point. */ public Point3D translateZ(final double zIncrement) { @@ -213,7 +277,7 @@ public class Point3D implements Cloneable { } /** - * Resets point to 0 coordinate in X, Y and Z axis. + * Resets point coordinates to zero along all axes. * * @return current point. */ diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java index e72de54..3f16d26 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java @@ -39,7 +39,7 @@ public class GuiComponent extends AbstractCompositeShape implements final double borderSize = 10; - final Box borderArea = containingBox.clone().addBorder(borderSize); + final Box borderArea = containingBox.clone().enlarge(borderSize); return new WireframeBox(borderArea, appearance); } @@ -109,7 +109,7 @@ public class GuiComponent extends AbstractCompositeShape implements } private void setDimensions(final Point3D size) { - containingBox.setSizeCentered(size); + containingBox.setBoxSize(size); } private void showBorder() { diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java index ebf741a..080bc0d 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/slicer/BorderLine.java @@ -33,7 +33,7 @@ public class BorderLine implements Comparator { 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)); } } -- 2.20.1