Module org.dyn4j

Class Triangle

    • Constructor Detail

      • Triangle

        public Triangle​(Vector2 point1,
                        Vector2 point2,
                        Vector2 point3)
        Full constructor.

        Creates a new triangle using the given points. The center will be the area weighted center of the points.

        A triangle must have 3 non-null points of which one is not colinear with the other two.

        Parameters:
        point1 - the first point
        point2 - the second point
        point3 - the third point
        Throws:
        NullPointerException - if point1, point2, or point3 is null
        IllegalArgumentException - if point1, point2, and point3 contain coincident points or has clockwise winding
    • Method Detail

      • contains

        public boolean contains​(Vector2 point,
                                Transform transform)
        Returns true if the point is inside the Triangle.

        The equation of a plane is:

        N · (P - A) = 0

        Where A is any point on the plane.
        Create two axes (Vector2s), we will choose Vab and Vac.

        Vac = C - A Vab = B - A

        Where A, B, and C are the vertices of the Triangle.
        From this we can say that you can get to any point on the plane by going some u distance on Vac and some v distance on Vab where A is the origin.

        P = A + u * Vac + v * Vab

        Simplifing P - A

        Vpa = u * Vac + v * Vab

        We still need another equation to solve for u and v:
        Dot the equation by Vac to get

        Vpa · Vac = (u * Vac + v * Vab) · Vac

        Dot the equation by Vab to get the other

        Vpa · Vab = (u * Vac + v * Vab) · Vab

        Distribute out both equations

        Vpa · Vac = u * Vac · Vac + v * Vab · Vac Vpa · Vab = u * Vac · Vab + v * Vab · Vab

        Solving the first equation for u:

        u = (Vpa · Vac - v * Vab · Vac) / (Vac · Vac)

        Substitute one into the other:

        Vpa · Vab = (Vpa · Vac - v * Vab · Vac) / (Vac · Vac) * Vac · Vab + v * Vab · Vab Vpa · Vab = (Vpa · Vac / Vac · Vac) * Vac · Vab - v * (Vab · Vac / Vac · Vac) * Vac · Vab + v * Vab · Vab Vpa · Vab = (Vpa · Vac / Vac · Vac) * Vac · Vab + v * (Vab · Vab - (Vab · Vac / Vac · Vac) * Vac · Vab) v = (Vpa · Vab - (Vpa · Vac / Vac · Vac) * Vac · Vab) / (Vab · Vab - (Vab · Vac / Vac · Vac) * Vac · Vab)

        Which reduces to:

        v = ((Vpa · Vab) * (Vac · Vac) - (Vpa · Vac) * (Vac · Vab)) / ((Vab · Vab) * (Vac · Vac) - (Vab · Vac) * (Vac · Vab))

        Once v is obtained use either equation to obtain u:

        u = (v * Vab · Vab - Vpa · Vab) / Vac · Vab

        We know that the point is inside the Triangle if u and v are greater than zero and u + v is less than one.
        Specified by:
        contains in interface Shape
        Overrides:
        contains in class Polygon
        Parameters:
        point - world space point
        transform - Transform the Shape's transform
        Returns:
        boolean