Search in sources :

Example 1 with Triangle

use of com.builtbroken.mc.lib.transform.region.Triangle in project Engine by VoltzEngine-Project.

the class TriangleTest method testAcuteTriangleAreas.

/**
 * Tests area method using 4 triangles in each section
 * of pos & neg relations. It also tests the order of the points
 * when constructing the triangle to ensure there are no ordering
 * issues with how the math is handled
 */
public void testAcuteTriangleAreas() {
    final double expectedArea = 2;
    // Using zero zero
    Point zeroZero = new Point();
    Point p1 = null;
    Point p2 = null;
    Triangle a, b, c;
    for (int i = 0; i < 4; i++) {
        switch(i) {
            case 0:
                // UP
                p1 = new Point(1, 2);
                p2 = new Point(-1, 2);
                break;
            case 2:
                // DOWN
                p1 = new Point(1, -2);
                p2 = new Point(-1, -2);
                break;
            case 3:
                // RIGHT
                p1 = new Point(2, 1);
                p2 = new Point(2, -1);
                break;
            case 4:
                // LEFT
                p1 = new Point(-2, 1);
                p2 = new Point(-2, -1);
                break;
        }
        a = new Triangle(zeroZero, p1, p2);
        b = new Triangle(p1, p2, zeroZero);
        c = new Triangle(p2, zeroZero, p1);
        assertEquals("Failed area check for A" + i, expectedArea, a.getArea());
        assertEquals("Failed area check for B" + i, expectedArea, b.getArea());
        assertEquals("Failed area check for C" + i, expectedArea, c.getArea());
    }
}
Also used : Triangle(com.builtbroken.mc.lib.transform.region.Triangle) Point(com.builtbroken.mc.lib.transform.vector.Point) Point(com.builtbroken.mc.lib.transform.vector.Point)

Example 2 with Triangle

use of com.builtbroken.mc.lib.transform.region.Triangle in project Engine by VoltzEngine-Project.

the class TriangleTest method testRightTriangleAreas.

/**
 * Tests area method using 4 triangles in each section
 * of pos & neg relations. It also tests the order of the points
 * when constructing the triangle to ensure there are no ordering
 * issues with how the math is handled
 */
public void testRightTriangleAreas() {
    final double expectedArea = 2;
    // Using zero zero
    Point zeroZero = new Point();
    Point p1 = null;
    Point p2 = null;
    Triangle a, b, c;
    for (int i = 0; i < 4; i++) {
        switch(i) {
            case 0:
                // positive positive
                p1 = new Point(0, 2);
                p2 = new Point(2, 0);
                break;
            case 2:
                // neg positive
                p1 = new Point(0, 2);
                p2 = new Point(-2, 0);
                break;
            case 3:
                // positive neg
                p1 = new Point(0, -2);
                p2 = new Point(2, 0);
                break;
            case 4:
                // neg neg
                p1 = new Point(0, -2);
                p2 = new Point(-2, 0);
                break;
        }
        a = new Triangle(zeroZero, p1, p2);
        b = new Triangle(p1, p2, zeroZero);
        c = new Triangle(p2, zeroZero, p1);
        assertEquals("Failed area check for A" + i, expectedArea, a.getArea());
        assertEquals("Failed area check for B" + i, expectedArea, b.getArea());
        assertEquals("Failed area check for C" + i, expectedArea, c.getArea());
    }
}
Also used : Triangle(com.builtbroken.mc.lib.transform.region.Triangle) Point(com.builtbroken.mc.lib.transform.vector.Point) Point(com.builtbroken.mc.lib.transform.vector.Point)

Example 3 with Triangle

use of com.builtbroken.mc.lib.transform.region.Triangle in project Engine by VoltzEngine-Project.

the class TriangleTest method testObtuseTriangleAreas.

/**
 * Tests area method using 4 triangles in each section
 * of pos & neg relations. It also tests the order of the points
 * when constructing the triangle to ensure there are no ordering
 * issues with how the math is handled
 */
public void testObtuseTriangleAreas() {
    final double expectedArea = 4;
    // Using zero zero
    Point zeroZero = new Point();
    Point p1 = null;
    Point p2 = null;
    Triangle a, b, c;
    for (int i = 0; i < 4; i++) {
        switch(i) {
            case 0:
                // UP
                p1 = new Point(2, 2);
                p2 = new Point(-2, 2);
                break;
            case 2:
                // DOWN
                p1 = new Point(2, -2);
                p2 = new Point(-2, -2);
                break;
            case 3:
                // RIGHT
                p1 = new Point(2, 2);
                p2 = new Point(2, -2);
                break;
            case 4:
                // LEFT
                p1 = new Point(-2, 2);
                p2 = new Point(-2, -2);
                break;
        }
        a = new Triangle(zeroZero, p1, p2);
        b = new Triangle(p1, p2, zeroZero);
        c = new Triangle(p2, zeroZero, p1);
        assertEquals("Failed area check for A" + i, expectedArea, a.getArea());
        assertEquals("Failed area check for B" + i, expectedArea, b.getArea());
        assertEquals("Failed area check for C" + i, expectedArea, c.getArea());
    }
}
Also used : Triangle(com.builtbroken.mc.lib.transform.region.Triangle) Point(com.builtbroken.mc.lib.transform.vector.Point) Point(com.builtbroken.mc.lib.transform.vector.Point)

Example 4 with Triangle

use of com.builtbroken.mc.lib.transform.region.Triangle in project Engine by VoltzEngine-Project.

the class TriangleTest method testTriangleAreas.

// TODO include a performance test to see how fast these method calls run on average
public void testTriangleAreas() {
    // Dummy checks for the hell of it
    Triangle t = new Triangle(new Point(), new Point(), new Point());
    assertEquals("Expected zero due to all 3 points being zero zero", 0.0, t.getArea());
    t = new Triangle(new Point(0, 1), new Point(0, 2), new Point(0, 3));
    assertEquals("Expected zero due to all 3 points being in a line", 0.0, t.getArea());
    // http://www.mathopenref.com/coordtrianglearea.html
    t = new Triangle(new Point(0, 0), new Point(-1, 2), new Point(13, 5));
    assertEquals("Expected an exact match for area check one", 15.5, t.getArea());
    t = new Triangle(new Point(0, 0), new Point(-12, 27), new Point(37, 25));
    assertEquals("Expected an exact match for area check two", 649.5, t.getArea());
    t = new Triangle(new Point(0, 0), new Point(-12, 27), new Point(14, -10));
    assertEquals("Expected an exact match for area check three", 129.0, t.getArea());
    t = new Triangle(new Point(15, 15), new Point(23, 30), new Point(25, 8));
    assertEquals("Expected an exact match for area check four", 103.0, t.getArea());
    t = new Triangle(new Point(10, -6), new Point(42, -6), new Point(25, -12));
    assertEquals("Expected an exact match for area check five", 96.0, t.getArea());
}
Also used : Triangle(com.builtbroken.mc.lib.transform.region.Triangle) Point(com.builtbroken.mc.lib.transform.vector.Point)

Example 5 with Triangle

use of com.builtbroken.mc.lib.transform.region.Triangle in project Engine by VoltzEngine-Project.

the class RectangleTest method testIsWithin.

/**
 * Checks if the basic version of the point bounding box check is working
 */
public void testIsWithin() {
    Rectangle rect = new Rectangle(new Point(0, 0), new Point(2, 2));
    List<Point> points_inside = new LinkedList();
    points_inside.add(rect.cornerA());
    points_inside.add(rect.cornerB());
    points_inside.add(rect.cornerC());
    points_inside.add(rect.cornerD());
    points_inside.add(new Point(1, 1));
    List<Point> points_outside = new LinkedList();
    points_outside.add(new Point(-1, -1));
    points_outside.add(new Point(0, -1));
    points_outside.add(new Point(-1, 0));
    points_outside.add(new Point(3, 0));
    points_outside.add(new Point(0, 3));
    for (int i = 0; i < 4; i++) {
        // First two runs are inside checks
        boolean inside = i <= 1;
        boolean rotated = (i == 1 || i == 3);
        List<Point> l = inside ? points_inside : points_outside;
        for (Point vec : l) {
            boolean flag = !rotated ? rect.isWithin(vec) : rect.isWithin_rotated(vec);
            // Debug for when the checks fail and we need to know why
            if (flag != inside) {
                System.out.println("===   Debug   ===");
                if (!rotated) {
                    System.out.println("isWithinX: " + (vec.x() >= rect.min().x() && vec.x() <= rect.max().x()));
                    System.out.println("isWithinY: " + (vec.y() >= rect.min().y() && vec.y() <= rect.max().y()));
                } else {
                    double ab = new Triangle(rect.cornerA(), rect.cornerB(), vec).getArea();
                    double bc = new Triangle(rect.cornerB(), rect.cornerC(), vec).getArea();
                    double cd = new Triangle(rect.cornerC(), rect.cornerD(), vec).getArea();
                    double da = new Triangle(rect.cornerD(), rect.cornerA(), vec).getArea();
                    System.out.println("TriABP Area: " + ab);
                    System.out.println("TriBCP Area: " + bc);
                    System.out.println("TriCBP Area: " + cd);
                    System.out.println("TriDAP Area: " + da);
                    System.out.println("Total Area:  " + (ab + bc + cd + da));
                    System.out.println("Rect Area:   " + rect.getArea());
                }
                System.out.println("==================");
                // Failure message
                String msg = "Failed for ";
                msg += (!rotated ? "Normal Test " : "Rotated Test ");
                msg += vec + " ";
                msg += (inside ? " should be inside the rect!" : " should be outside the rect!");
                fail(msg);
            }
        }
    }
}
Also used : Rectangle(com.builtbroken.mc.lib.transform.region.Rectangle) Triangle(com.builtbroken.mc.lib.transform.region.Triangle) Point(com.builtbroken.mc.lib.transform.vector.Point) LinkedList(java.util.LinkedList) Point(com.builtbroken.mc.lib.transform.vector.Point)

Aggregations

Triangle (com.builtbroken.mc.lib.transform.region.Triangle)5 Point (com.builtbroken.mc.lib.transform.vector.Point)5 Rectangle (com.builtbroken.mc.lib.transform.region.Rectangle)1 LinkedList (java.util.LinkedList)1