From: Svjatoslav Agejenko Date: Sun, 21 May 2023 00:08:23 +0000 (+0300) Subject: Improved code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=sixth-3d.git;a=commitdiff_plain;h=a2131986d65a769e3d589e4e0370d4af0ce10c38 Improved code readability --- diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/TextPointer.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/TextPointer.java index 67291f5..91cd6e6 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/TextPointer.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/TextPointer.java @@ -4,6 +4,8 @@ */ package eu.svjatoslav.sixth.e3d.gui; +import static java.lang.Integer.compare; + /** * A pointer to a character in a text using row and column. *

@@ -49,20 +51,32 @@ public class TextPointer implements Comparable { return result; } + /** + * Compares this pointer to another pointer. + * + * @param textPointer The pointer to compare to. + * @return

+ */ @Override public int compareTo(final TextPointer textPointer) { - if (textPointer.row > row) + if (row < textPointer.row) return -1; - if (textPointer.row < row) + if (row > textPointer.row) return 1; - return Integer.compare(column, textPointer.column); - + return compare(column, textPointer.column); } /** - * Checks if this pointer is between the specified pointers. + * Checks if this pointer is between the argument pointers. + *

+ * This pointer is considered to be between the pointers if it is bigger or equal to the start pointer + * and smaller than the end pointer. * * @param start The start pointer. * @param end The end pointer. diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java index 6665676..dd706db 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/UserRelativityTracker.java @@ -8,6 +8,13 @@ import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.math.TransformsStack; import eu.svjatoslav.sixth.e3d.math.Vertex; +/** + * Tracks the position of the user in the 3D space. + *

+ * It can be used to determine the angle between the user and the object. + * Also, it can be used to determine the distance between the user and the object. + */ + public class UserRelativityTracker { private final static int minimalSliceFactor = 5; diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java b/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java index 69b3095..50423b1 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/math/Vertex.java @@ -9,8 +9,9 @@ import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.gui.RenderingContext; /** - * It is used to store coordinates of vertices of 3D objects. - * It is also used to store coordinates of points in 3D space. + * Vertex is a point where two or more lines, line segments, or rays come together. + * In other words, it's a corner of a polygon, polyhedron, or other geometric shape. + * For example, a triangle has three vertices, a square has four, and a cube has eight. */ public class Vertex { @@ -23,11 +24,13 @@ public class Vertex { * Vertex coordinate relative to the viewer after transformation. * Visible vertices have positive z coordinate. * Viewer is located at (0, 0, 0). + * No perspective correction is applied. */ public Point3D transformedCoordinate; /** - * Vertex coordinate in pixels relative to the top left corner of the screen after transformation. + * Vertex coordinate in pixels relative to the top left corner of the screen after transformation + * and perspective correction. */ public Point2D onScreenCoordinate; diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java index f77f71e..8c3e49f 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/textcanvas/TextCanvas.java @@ -19,6 +19,8 @@ import java.io.StringReader; import static eu.svjatoslav.sixth.e3d.renderer.raster.Color.BLACK; import static eu.svjatoslav.sixth.e3d.renderer.raster.Color.WHITE; +import static java.lang.Math.PI; +import static java.lang.Math.abs; public class TextCanvas extends TexturedRectangle { @@ -132,29 +134,26 @@ public class TextCanvas extends TexturedRectangle { @Override public void beforeTransformHook(final TransformsStack transformPipe, final RenderingContext context) { + ensureOptimalRenderMode(context); + } - final double textRelativeSize = context.width - / getRelativityTracker().getDistanceToUser(); + private void ensureOptimalRenderMode(RenderingContext context) { + // if the text is too far away, use texture + final double textRelativeSize = context.width / getRelativityTracker().getDistanceToUser(); if (textRelativeSize < 2d) { - if (renderMode == RenderMode.CHARACTERS) - setRenderMode(RenderMode.TEXTURE); + setRenderMode(RenderMode.TEXTURE); return; } - final double piHalf = Math.PI / 2; - - final double deviation = Math.abs(getRelativityTracker().getAngleXZ() + // if user is looking at the text from the side, use texture + final double piHalf = PI / 2; + final double deviation = abs(getRelativityTracker().getAngleXZ() + piHalf) - + Math.abs(getRelativityTracker().getAngleYZ() + piHalf); + + abs(getRelativityTracker().getAngleYZ() + piHalf); final double maxDeviation = 0.5; - - if (deviation > maxDeviation) { - if (renderMode == RenderMode.CHARACTERS) - setRenderMode(RenderMode.TEXTURE); - } else if (renderMode == RenderMode.TEXTURE) - setRenderMode(RenderMode.CHARACTERS); + setRenderMode(deviation > maxDeviation ? RenderMode.TEXTURE : RenderMode.CHARACTERS); } public void clear() {