X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgeometry%2FPolygon.java;h=d5f558ea5e34c3edd4b878e8e37d9891d76a175d;hb=7e383c71392e298dde478015129bc739f3eb4e4a;hp=6e23beabc81d83d26d8ec97aabec747e46da1634;hpb=59baa428fb2d9e7f0fe5423f4cea47f2d6245914;p=sixth-3d.git 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 6e23bea..d5f558e 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Polygon.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/geometry/Polygon.java @@ -1,39 +1,43 @@ /* - * Sixth 3D engine. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - * + * Sixth 3D engine. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.sixth.e3d.geometry; +/** + * Utility class for polygon operations. + */ public class Polygon { - private static boolean intersectsLine(final Point2D point, Point2D p1, - Point2D p2) { - if (p1.y > p2.y) { - final Point2D tmp = p1; - p1 = p2; - p2 = tmp; + /** + * Checks if point is on the right side of the line. + * @param point point to check + * @param lineP1 line start point + * @param lineP2 line end point + * @return true if point is on the right side of the line + */ + private static boolean intersectsLine(final Point2D point, Point2D lineP1, + Point2D lineP2) { + + // Sort line points by y coordinate. + if (lineP1.y > lineP2.y) { + final Point2D tmp = lineP1; + lineP1 = lineP2; + lineP2 = tmp; } - if (point.y < p1.y) - return false; - if (point.y > p2.y) + // Check if point is within line y range. + if (point.y < lineP1.y || point.y > lineP2.y) return false; - final double xp = p2.x - p1.x; - final double yp = p2.y - p1.y; + // 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 - p1.y; - - final double crossX = p1.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,