Class Polygon

  • All Implemented Interfaces:
    Shape2D

    public class Polygon
    extends java.lang.Object
    implements Shape2D
    Encapsulates a 2D polygon defined by it's vertices relative to an origin point (default of 0, 0).
    • Constructor Summary

      Constructors 
      Constructor Description
      Polygon()
      Constructs a new polygon with no vertices.
      Polygon​(float[] vertices)
      Constructs a new polygon from a float array of parts of vertex points.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      float area()
      Returns the area contained within the polygon.
      boolean contains​(float x, float y)
      Returns whether an x, y pair is contained within the polygon.
      boolean contains​(Vector2 point)
      Returns whether the given point is contained within the shape.
      void dirty()
      Sets the polygon's world vertices to be recalculated when calling getTransformedVertices.
      Rectangle getBoundingRectangle()
      Returns an axis-aligned bounding box of this polygon.
      Vector2 getCentroid​(Vector2 centroid)  
      float getOriginX()
      Returns the x-coordinate of the polygon's origin point.
      float getOriginY()
      Returns the y-coordinate of the polygon's origin point.
      float getRotation()
      Returns the total rotation applied to the polygon.
      float getScaleX()
      Returns the total horizontal scaling applied to the polygon.
      float getScaleY()
      Returns the total vertical scaling applied to the polygon.
      float[] getTransformedVertices()
      Calculates and returns the vertices of the polygon after scaling, rotation, and positional translations have been applied, as they are position within the world.
      Vector2 getVertex​(int vertexNum, Vector2 pos)  
      int getVertexCount()  
      float[] getVertices()
      Returns the polygon's local vertices without scaling or rotation and without being offset by the polygon position.
      float getX()
      Returns the x-coordinate of the polygon's position within the world.
      float getY()
      Returns the y-coordinate of the polygon's position within the world.
      void rotate​(float degrees)
      Applies additional rotation to the polygon by the supplied degrees.
      void scale​(float amount)
      Applies additional scaling to the polygon by the supplied amount.
      void setOrigin​(float originX, float originY)
      Sets the origin point to which all of the polygon's local vertices are relative to.
      void setPosition​(float x, float y)
      Sets the polygon's position within the world.
      void setRotation​(float degrees)
      Sets the polygon to be rotated by the supplied degrees.
      void setScale​(float scaleX, float scaleY)
      Sets the amount of scaling to be applied to the polygon.
      void setVertex​(int vertexNum, float x, float y)
      Set vertex position
      void setVertices​(float[] vertices)
      Sets the polygon's local vertices relative to the origin point, without any scaling, rotating or translations being applied.
      void translate​(float x, float y)
      Translates the polygon's position by the specified horizontal and vertical amounts.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Polygon

        public Polygon()
        Constructs a new polygon with no vertices.
      • Polygon

        public Polygon​(float[] vertices)
        Constructs a new polygon from a float array of parts of vertex points.
        Parameters:
        vertices - an array where every even element represents the horizontal part of a point, and the following element representing the vertical part
        Throws:
        java.lang.IllegalArgumentException - if less than 6 elements, representing 3 points, are provided
    • Method Detail

      • getVertices

        public float[] getVertices()
        Returns the polygon's local vertices without scaling or rotation and without being offset by the polygon position.
      • getTransformedVertices

        public float[] getTransformedVertices()
        Calculates and returns the vertices of the polygon after scaling, rotation, and positional translations have been applied, as they are position within the world.
        Returns:
        vertices scaled, rotated, and offset by the polygon position.
      • setOrigin

        public void setOrigin​(float originX,
                              float originY)
        Sets the origin point to which all of the polygon's local vertices are relative to.
      • setPosition

        public void setPosition​(float x,
                                float y)
        Sets the polygon's position within the world.
      • setVertices

        public void setVertices​(float[] vertices)
        Sets the polygon's local vertices relative to the origin point, without any scaling, rotating or translations being applied.
        Parameters:
        vertices - float array where every even element represents the x-coordinate of a vertex, and the proceeding element representing the y-coordinate.
        Throws:
        java.lang.IllegalArgumentException - if less than 6 elements, representing 3 points, are provided
      • setVertex

        public void setVertex​(int vertexNum,
                              float x,
                              float y)
        Set vertex position
        Parameters:
        vertexNum - min=0, max=vertices.length/2-1
        Throws:
        java.lang.IllegalArgumentException - if vertex doesnt exist
      • translate

        public void translate​(float x,
                              float y)
        Translates the polygon's position by the specified horizontal and vertical amounts.
      • setRotation

        public void setRotation​(float degrees)
        Sets the polygon to be rotated by the supplied degrees.
      • rotate

        public void rotate​(float degrees)
        Applies additional rotation to the polygon by the supplied degrees.
      • setScale

        public void setScale​(float scaleX,
                             float scaleY)
        Sets the amount of scaling to be applied to the polygon.
      • scale

        public void scale​(float amount)
        Applies additional scaling to the polygon by the supplied amount.
      • dirty

        public void dirty()
        Sets the polygon's world vertices to be recalculated when calling getTransformedVertices.
      • area

        public float area()
        Returns the area contained within the polygon.
      • getVertexCount

        public int getVertexCount()
      • getVertex

        public Vector2 getVertex​(int vertexNum,
                                 Vector2 pos)
        Returns:
        Position(transformed) of vertex
      • getBoundingRectangle

        public Rectangle getBoundingRectangle()
        Returns an axis-aligned bounding box of this polygon. Note the returned Rectangle is cached in this polygon, and will be reused if this Polygon is changed.
        Returns:
        this polygon's bounding box Rectangle
      • contains

        public boolean contains​(float x,
                                float y)
        Returns whether an x, y pair is contained within the polygon.
        Specified by:
        contains in interface Shape2D
      • contains

        public boolean contains​(Vector2 point)
        Description copied from interface: Shape2D
        Returns whether the given point is contained within the shape.
        Specified by:
        contains in interface Shape2D
      • getX

        public float getX()
        Returns the x-coordinate of the polygon's position within the world.
      • getY

        public float getY()
        Returns the y-coordinate of the polygon's position within the world.
      • getOriginX

        public float getOriginX()
        Returns the x-coordinate of the polygon's origin point.
      • getOriginY

        public float getOriginY()
        Returns the y-coordinate of the polygon's origin point.
      • getRotation

        public float getRotation()
        Returns the total rotation applied to the polygon.
      • getScaleX

        public float getScaleX()
        Returns the total horizontal scaling applied to the polygon.
      • getScaleY

        public float getScaleY()
        Returns the total vertical scaling applied to the polygon.