/** * Test if a point is inside a triangle. 1 if the point is on the ccw side, * -1 if the point is on the cw side, and 0 if it is on neither. * @param t0 First point of the triangle. * @param t1 Second point of the triangle. * @param t2 Third point of the triangle. * @param p The point to test. * @return Value 1 or -1 if inside triangle, 0 otherwise. */ public static int pointInsideTriangle(Vector2f t0, Vector2f t1, Vector2f t2, Vector2f p) { int val1 = counterClockwise(t0, t1, p); if (val1 == 0) { return 1; } int val2 = counterClockwise(t1, t2, p); if (val2 == 0) { return 1; } if (val2 != val1) { return 0; } int val3 = counterClockwise(t2, t0, p); if (val3 == 0) { return 1; } if (val3 != val1) { return 0; } return val3; }
@Ignore @Test public void testCounterClockwise() { for (int i = 0; i < 100; i++) { Vector2f p0 = randomVector(); Vector2f p1 = randomVector(); Vector2f p2 = randomVector(); int fastResult = fastCounterClockwise(p0, p1, p2); int slowResult = FastMath.counterClockwise(p0, p1, p2); assert fastResult == slowResult; } // duplicate test Vector2f p0 = new Vector2f(0,0); Vector2f p1 = new Vector2f(0,0); Vector2f p2 = new Vector2f(0,1); int fastResult = fastCounterClockwise(p0, p1, p2); int slowResult = FastMath.counterClockwise(p0, p1, p2); assertEquals(slowResult, fastResult); } }
/** * Test if a point is inside a triangle. 1 if the point is on the ccw side, * -1 if the point is on the cw side, and 0 if it is on neither. * @param t0 First point of the triangle. * @param t1 Second point of the triangle. * @param t2 Third point of the triangle. * @param p The point to test. * @return Value 1 or -1 if inside triangle, 0 otherwise. */ public static int pointInsideTriangle(Vector2f t0, Vector2f t1, Vector2f t2, Vector2f p) { int val1 = counterClockwise(t0, t1, p); if (val1 == 0) { return 1; } int val2 = counterClockwise(t1, t2, p); if (val2 == 0) { return 1; } if (val2 != val1) { return 0; } int val3 = counterClockwise(t2, t0, p); if (val3 == 0) { return 1; } if (val3 != val1) { return 0; } return val3; }
/** * Test if a point is inside a triangle. 1 if the point is on the ccw side, * -1 if the point is on the cw side, and 0 if it is on neither. * @param t0 First point of the triangle. * @param t1 Second point of the triangle. * @param t2 Third point of the triangle. * @param p The point to test. * @return Value 1 or -1 if inside triangle, 0 otherwise. */ public static int pointInsideTriangle(Vector2f t0, Vector2f t1, Vector2f t2, Vector2f p) { int val1 = counterClockwise(t0, t1, p); if (val1 == 0) { return 1; } int val2 = counterClockwise(t1, t2, p); if (val2 == 0) { return 1; } if (val2 != val1) { return 0; } int val3 = counterClockwise(t2, t0, p); if (val3 == 0) { return 1; } if (val3 != val1) { return 0; } return val3; }