2 * Sixth 3D engine. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 3 of the GNU Lesser General Public License
6 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.sixth.e3d.geometry;
12 public class Polygon {
14 private static boolean intersectsLine(final Point2D point, Point2D p1,
18 final Point2D tmp = p1;
28 final double xp = p2.x - p1.x;
29 final double yp = p2.y - p1.y;
31 final double yp2 = point.y - p1.y;
33 final double crossX = p1.x + ((xp * yp2) / yp);
35 return point.x >= crossX;
39 public static boolean pointWithinPolygon(final Point2D point,
40 final Point2D p1, final Point2D p2, final Point2D p3) {
42 int intersectionCount = 0;
44 if (intersectsLine(point, p1, p2))
47 if (intersectsLine(point, p2, p3))
50 if (intersectsLine(point, p3, p1))
53 return intersectionCount == 1;