Package arc.math.geom

Class Polygon

java.lang.Object
arc.math.geom.Polygon
All Implemented Interfaces:
Shape2D

public class Polygon extends 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
    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

    Modifier and Type
    Method
    Description
    float
    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(Vec2 point)
    Returns whether the given point is contained within the shape.
    void
    Sets the polygon's world vertices to be recalculated when calling getTransformedVertices.
    Returns an axis-aligned bounding box of this polygon.
    float
    Returns the x-coordinate of the polygon's origin point.
    float
    Returns the y-coordinate of the polygon's origin point.
    float
    Returns the total rotation applied to the polygon.
    float
    Returns the total horizontal scaling applied to the polygon.
    float
    Returns the total vertical scaling applied to the polygon.
    float[]
    Calculates and returns the vertices of the polygon after scaling, rotation, and positional translations have been applied, as they are position within the world.
    float[]
    Returns the polygon's local vertices without scaling or rotation and without being offset by the polygon position.
    float
    Returns the x-coordinate of the polygon's position within the world.
    float
    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
    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 Details

    • 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:
      IllegalArgumentException - if less than 6 elements, representing 3 points, are provided
  • Method Details

    • getVertices

      public float[] getVertices()
      Returns the polygon's local vertices without scaling or rotation and without being offset by the polygon position.
    • 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:
      IllegalArgumentException - if less than 6 elements, representing 3 points, are provided
    • 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.
    • translate

      public void translate(float x, float y)
      Translates the polygon's position by the specified horizontal and vertical amounts.
    • 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.
    • getBoundingRectangle

      public Rect 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 Rect
    • 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(Vec2 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.
    • setRotation

      public void setRotation(float degrees)
      Sets the polygon to be rotated by the supplied degrees.
    • 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.