From 7e383c71392e298dde478015129bc739f3eb4e4a Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 29 Mar 2023 23:26:27 +0300 Subject: [PATCH] Improved code readability --- .../sixth/e3d/geometry/Polygon.java | 11 +++------- .../sixth/e3d/geometry/Rectangle.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Polygon.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Polygon.java index 5475bad..d5f558e 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Polygon.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Polygon.java @@ -20,7 +20,7 @@ public class Polygon { private static boolean intersectsLine(final Point2D point, Point2D lineP1, Point2D lineP2) { - // Sort line points by y coordinate. + // Sort line points by y coordinate. if (lineP1.y > lineP2.y) { final Point2D tmp = lineP1; lineP1 = lineP2; @@ -28,21 +28,16 @@ public class Polygon { } // Check if point is within line y range. - if (point.y < lineP1.y) - return false; - if (point.y > lineP2.y) + if (point.y < lineP1.y || point.y > lineP2.y) return false; // Check if point is on the line. final double xp = lineP2.x - lineP1.x; final double yp = lineP2.y - lineP1.y; - final double yp2 = point.y - lineP1.y; - - final double crossX = lineP1.x + ((xp * yp2) / yp); + final double crossX = lineP1.x + ((xp * (point.y - lineP1.y)) / yp); return point.x >= crossX; - } public static boolean pointWithinPolygon(final Point2D point, diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Rectangle.java b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Rectangle.java index 1d11e72..966d366 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Rectangle.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Rectangle.java @@ -12,13 +12,27 @@ import static java.lang.Math.min; */ public class Rectangle { + /** + * Rectangle points. + */ public Point2D p1, p2; + /** + * Creates new rectangle with given size. + * The rectangle will be centered at the origin. + * The rectangle will be square. + * + * @param size The size of the rectangle. + */ public Rectangle(final double size) { p2 = new Point2D(size / 2, size / 2); p1 = p2.clone().invert(); } + /** + * @param p1 The first point of the rectangle. + * @param p2 The second point of the rectangle. + */ public Rectangle(final Point2D p1, final Point2D p2) { this.p1 = p1; this.p2 = p2; @@ -28,6 +42,9 @@ public class Rectangle { return abs(p1.y - p2.y); } + /** + * @return The leftmost x coordinate of the rectangle. + */ public double getLowerX() { return min(p1.x, p2.x); } @@ -36,6 +53,9 @@ public class Rectangle { return min(p1.y, p2.y); } + /** + * @return rectangle width. + */ public double getWidth() { return abs(p1.x - p2.x); } -- 2.20.1