Module org.dyn4j

Class Polygon

    • Constructor Detail

      • Polygon

        public Polygon​(Vector2... vertices)
        Full constructor.

        Creates a new Polygon using the given vertices. The center of the polygon is calculated using an area weighted method.

        A polygon must have 3 or more vertices, of which one is not colinear with the other two.

        A polygon must also be convex and have counter-clockwise winding.

        Parameters:
        vertices - the array of vertices
        Throws:
        NullPointerException - if vertices is null or contains a null element
        IllegalArgumentException - if vertices contains less than 3 points, contains coincident points, is not convex, or has clockwise winding
    • Method Detail

      • getVertices

        public Vector2[] getVertices()
        Description copied from interface: Wound
        Returns the array of vertices in local coordinates.

        For performance, this array may be the internal storage array of the shape. Both the array elements and their properties should not be modified via this method.

        It's possible that this method will be deprecated and/or removed in later versions.

        Specified by:
        getVertices in interface Wound
        Returns:
        Vector2[]
        See Also:
        Wound.getVertexIterator()
      • getNormals

        public Vector2[] getNormals()
        Description copied from interface: Wound
        Returns the array of edge normals in local coordinates.

        For performance, this array may be the internal storage array of the shape. Both the array elements and their properties should not be modified via this method.

        It's possible that this method will be deprecated and/or removed in later versions.

        Specified by:
        getNormals in interface Wound
        Returns:
        Vector2[]
        See Also:
        Wound.getNormalIterator()
      • getVertexIterator

        public Iterator<Vector2> getVertexIterator()
        Description copied from interface: Wound
        Returns an iterator for the vertices.

        The iterator does not support the remove method and will return a new Vector2 in the next method.

        This method is safer than the Wound.getVertices() since its not possible to modify the array or its elements.

        Specified by:
        getVertexIterator in interface Wound
        Returns:
        Iterator<Vector2>
      • getNormalIterator

        public Iterator<Vector2> getNormalIterator()
        Description copied from interface: Wound
        Returns an iterator for the normals.

        The iterator does not support the remove method and will return a new Vector2 in the next method rather than the underlying value.

        This method is safer than the Wound.getNormals() since its not possible to modify the array or its elements.

        Specified by:
        getNormalIterator in interface Wound
        Returns:
        Iterator<Vector2>
      • getRadius

        public double getRadius​(Vector2 center)
        Description copied from interface: Shape
        Returns the radius of the shape if the given point was the center for this shape.
        Specified by:
        getRadius in interface Shape
        Parameters:
        center - the center point
        Returns:
        double
      • getAxes

        public Vector2[] getAxes​(Vector2[] foci,
                                 Transform transform)
        Description copied from interface: Convex
        Returns an array of separating axes to test for this Shape.

        The foci parameter is an array of circular focal points of the other Shape.

        If foci points are given, this method will return the separating axes for this Shape's voronoi regions also. The points in the foci array are assumed to be in world space.

        The returned axes are normalized and in world space.

        Specified by:
        getAxes in interface Convex
        Parameters:
        foci - the world space points representing foci of curved Shapes; can be null
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2[]
      • getFoci

        public Vector2[] getFoci​(Transform transform)
        Returns an array of world space foci points for circular curved edges.

        This method returns null if the Shape has zero curved edges.

        The returned points are in world space.

        Not applicable to this shape. Always returns null.

        Specified by:
        getFoci in interface Convex
        Parameters:
        transform - the local to world space Transform of this Convex Shape
        Returns:
        null
      • contains

        public boolean contains​(Vector2 point,
                                Transform transform)
        Description copied from interface: Shape
        Returns true if the given point is inside this Shape.

        If the given point lies on an edge the point is considered to be inside the Shape.

        The given point is assumed to be in world space.

        Specified by:
        contains in interface Shape
        Parameters:
        point - world space point
        transform - Transform for this Shape
        Returns:
        boolean
      • rotate

        public void rotate​(Rotation rotation,
                           double x,
                           double y)
        Description copied from interface: Rotatable
        Rotates the object about the given point.
        Specified by:
        rotate in interface Rotatable
        Overrides:
        rotate in class AbstractShape
        Parameters:
        rotation - the Rotation representing the rotation amount
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
      • translate

        public void translate​(double x,
                              double y)
        Description copied from interface: Translatable
        Translates the object the given amounts in the respective directions.
        Specified by:
        translate in interface Translatable
        Overrides:
        translate in class AbstractShape
        Parameters:
        x - the translation in the x direction
        y - the translation in the y direction
      • getFarthestPoint

        public Vector2 getFarthestPoint​(Vector2 vector,
                                        Transform transform)
        Description copied from interface: Convex
        Returns the point farthest in the direction of the given vector. If two points are equally distant along the given Vector2 the first one is used.

        The returned point is in world space.

        Specified by:
        getFarthestPoint in interface Convex
        Parameters:
        vector - the direction
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2
      • createMass

        public Mass createMass​(double density)
        Creates a Mass object using the geometric properties of this Polygon and the given density.

        A Polygon's centroid must be computed by the area weighted method since the average method can be bias to one side if there are more points on that one side than another.

        Finding the area of a Polygon can be done by using the following summation:

        0.5 * ∑(xi * yi + 1 - xi + 1 * yi)

        Finding the area weighted centroid can be done by using the following summation:

        1 / (6 * A) * ∑(pi + pi + 1) * (xi * yi + 1 - xi + 1 * yi)

        Finding the inertia tensor can by done by using the following equation:

        ∑(pi + 1 x pi) * (pi2 + pi · pi + 1 + pi + 12) m / 6 * ------------------------------------------- ∑(pi + 1 x pi)

        Where the mass is computed by:

        d * area

        Specified by:
        createMass in interface Shape
        Parameters:
        density - the density in kg/m2
        Returns:
        Mass the Mass of this Polygon
      • computeAABB

        public void computeAABB​(Transform transform,
                                AABB aabb)
        Description copied from interface: Shape
        Computes the AABB from this Shape after applying the given transformation and places the result in the given AABB.
        Specified by:
        computeAABB in interface Shape
        Parameters:
        transform - the Transform for this Shape
        aabb - the destination AABB