Geometry { public static boolean isInsideTriangle(Vector2 p, Vector2 pA, Vector2 pB, Vector2 pC) { Vector2 pointB = new Vector2(pB.x - pA.x, pB.y - pA.y); Vector2 pointC = new Vector2(pC.x - pA.x, pC.y - pA.y); Vector2 point = new Vector2(p.x - pA.x, p.y - pA.y); Double scalar = pointB.x * pointC.y - pointC.x * pointB.y; Double weightA = ( point.x * ( pointB.y - pointC.y ) + point.y * ( pointC.x - pointB.x ) + pointB.x * pointC.y - pointC.x * pointB.y ) / scalar; Double weightB = ( point.x * pointC.y - point.y * pointC.x ) / scalar; Double weightC = ( point.y * pointB.x - point.x * pointB.y ) / scalar; return isBetweenZeroAndOne(weightA) && isBetweenZeroAndOne(weightB) && isBetweenZeroAndOne(weightC); }  static boolean isInsideTriangle(Vector2 p, Vector2 pA, Vector2 pB, Vector2 pC); static boolean isInsideTriangle(Vector2 p, Triangle triangle); static boolean areTrianglesColliding(Triangle triangle1, Triangle triangle2); static Vector2 coordinatesAfterRotation(Vector2 coordinates, Double rotation);  }
@Test public void testIsInsideTriangle() throws Exception { Vector2 p = new Vector2(2d, 3d); Vector2 pA = new Vector2(1d, 1d); Vector2 pB = new Vector2(4d, 2d); Vector2 pC = new Vector2(2d, 7d); assertThat(Geometry.isInsideTriangle(p, pA, pB, pC)).isTrue(); }
@Test public void testIsNotInsideTriangle() throws Exception { Vector2 p = new Vector2(1.5d, 5d); Vector2 pA = new Vector2(1d, 1d); Vector2 pB = new Vector2(4d, 2d); Vector2 pC = new Vector2(2d, 7d); assertThat(Geometry.isInsideTriangle(p, pA, pB, pC)).isFalse(); }
@Test public void testIsInsideTriangle2() throws Exception { Vector2 p = new Vector2(2d, 3d); Vector2 pA = new Vector2(1d, 1d); Vector2 pB = new Vector2(4d, 2d); Vector2 pC = new Vector2(2d, 7d); Triangle triangle = new Triangle(pA, pB, pC); assertThat(Geometry.isInsideTriangle(p, triangle)).isTrue(); }
@Test public void testIsNotInsideTriangle2() throws Exception { Vector2 p = new Vector2(1.5d, 5d); Vector2 pA = new Vector2(1d, 1d); Vector2 pB = new Vector2(4d, 2d); Vector2 pC = new Vector2(2d, 7d); Triangle triangle = new Triangle(pA, pB, pC); assertThat(Geometry.isInsideTriangle(p, triangle)).isFalse(); }