2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.geometry;
8 * Utility class for polygon operations.
10 public class Polygon {
12 private static boolean intersectsLine(final Point2D point, Point2D p1,
16 final Point2D tmp = p1;
26 final double xp = p2.x - p1.x;
27 final double yp = p2.y - p1.y;
29 final double yp2 = point.y - p1.y;
31 final double crossX = p1.x + ((xp * yp2) / yp);
33 return point.x >= crossX;
37 public static boolean pointWithinPolygon(final Point2D point,
38 final Point2D p1, final Point2D p2, final Point2D p3) {
40 int intersectionCount = 0;
42 if (intersectsLine(point, p1, p2))
45 if (intersectsLine(point, p2, p3))
48 if (intersectsLine(point, p3, p1))
51 return intersectionCount == 1;