Class Poly2

java.lang.Object
edu.cornell.gdiac.math.Poly2

public class Poly2 extends Object
A class to represent a simple polygon.

This class is intended to represent any polygon (including non-convex polygons). that does not have self-interections (as these can cause serious problems with the mathematics). Most polygons are simple, meaning that they have no holes. However, this class does support complex polygons with holes, provided that the polygon is not implicit and has an corresponding mesh.

To define a mesh, the user should provide a set of indices which will be used in rendering. These indices should represent a triangulation of the polygon. However, this class performs no verification. It will not check that a mesh is in proper form, nor will it search for holes or self-intersections. These are the responsibility of the programmer.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    com.badlogic.gdx.utils.ShortArray
    The vector of indices in the triangulation
    com.badlogic.gdx.utils.FloatArray
    The vector of vertices in this polygon
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty polygon.
    Poly2(float[] vertices)
    Creates a polygon with the given vertices
    Poly2(float[] vertices, int voffset, int vlength)
    Creates a the polygon with the given vertices
    Poly2(float[] vertices, short[] indices)
    Creates a polygon with the given vertices and indices
    Poly2(float x, float y, float w, float h)
    Creates a polygon for the given rectangle.
    Poly2(com.badlogic.gdx.math.Rectangle rect)
    Creates a polygon for the given rectangle.
    Poly2(Poly2 poly)
    Creates a polygon that is a copy of the given one.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(float offset)
    Uniformly translates all of the vertices of this polygon.
    add(com.badlogic.gdx.math.Vector2 offset)
    Non-uniformly translates all of the vertices of this polygon.
    short[][]
    Returns the connected boundary components for this polygon.
    void
    Clears the contents of this polygon (both vertices and indices)
    boolean
    contains(float x, float y)
    Returns true if this polygon contains the given point.
    boolean
    contains(com.badlogic.gdx.math.Vector2 point)
    Returns true if this polygon contains the given point.
    div(float scale)
    Uniformly scales all of the vertices of this polygon.
    div(com.badlogic.gdx.math.Vector2 scale)
    Nonuniformly scales all of the vertices of this polygon.
    short[]
    Returns the setx of indices that are on a boundary of this polygon
    com.badlogic.gdx.math.Rectangle
    Returns the bounding box for the polygon
    com.badlogic.gdx.math.Rectangle
    getBounds(com.badlogic.gdx.math.Rectangle rect)
    Sets given rectangle to be the bounding box for the polygon
    com.badlogic.gdx.math.Vector2[]
    Returns the list of vertices
    boolean
    incident(float x, float y, float err)
    Returns true if this polygon contains the given point.
    boolean
    incident(com.badlogic.gdx.math.Vector2 point, float err)
    Returns true if the given point is on the boundary of this polygon.
    int
    Returns the number of indices in the polygon.
    com.badlogic.gdx.graphics.g2d.PolygonRegion
    makePolyRegion(com.badlogic.gdx.graphics.g2d.TextureRegion region)
    Converts the current Poly2 object to PolygonRegion.
    void
    push(float x, float y)
    Adds a point to the end of this polygon
    void
    push(com.badlogic.gdx.math.Vector2 point)
    Adds a point to the end of this polygon
    scl(float scale)
    Uniformly scales all of the vertices of this polygon.
    scl(com.badlogic.gdx.math.Vector2 scale)
    Nonuniformly scales all of the vertices of this polygon.
    set(float[] vertices)
    Sets the polygon to have the given vertices
    set(float[] vertices, int voffset, int vlength)
    Sets the polygon to have the given vertices.
    set(float x, float y, float w, float h)
    Sets the polygon to represent the given rectangle.
    set(com.badlogic.gdx.math.Rectangle rect)
    Sets the polygon to represent the given rectangle.
    set(Poly2 poly)
    Sets this polygon to be a copy of the given one.
    int
    Returns the number of vertices in the polygon.
    sub(float offset)
    Uniformly translates all of the vertices of this polygon.
    sub(com.badlogic.gdx.math.Vector2 offset)
    Non-uniformly translates all of the vertices of this polygon.
    Returns a string representation of this rectangle for debugging purposes.
    toString(boolean verbose)
    Returns a string representation of this rectangle for debugging purposes.
    transform(com.badlogic.gdx.math.Affine2 transform)
    Transforms all of the vertices of this polygon.
    com.badlogic.gdx.math.Vector2
    vertexAt(int index)
    Returns the point at given index
    com.badlogic.gdx.math.Vector2
    vertexAt(int index, com.badlogic.gdx.math.Vector2 point)
    Returns the point at given index

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • vertices

      public com.badlogic.gdx.utils.FloatArray vertices
      The vector of vertices in this polygon
    • indices

      public com.badlogic.gdx.utils.ShortArray indices
      The vector of indices in the triangulation
  • Constructor Details

    • Poly2

      public Poly2()
      Creates an empty polygon.

      The created polygon has no vertices and no triangulation. The bounding box is trivial.

    • Poly2

      public Poly2(float[] vertices)
      Creates a polygon with the given vertices

      The resulting polygon has no indices triangulating the vertices.

      Parameters:
      vertices - The vector of vertices (as float in pairs) in this polygon
    • Poly2

      public Poly2(float[] vertices, int voffset, int vlength)
      Creates a the polygon with the given vertices

      The resulting polygon has no indices triangulating the vertices.

      Parameters:
      vertices - The vector of vertices (as float in pairs) in this polygon
      voffset - The offset of the first array vertex
      vlength - The array size to use
    • Poly2

      public Poly2(float[] vertices, short[] indices)
      Creates a polygon with the given vertices and indices
      Parameters:
      vertices - The float array of vertices (as float in pairs) in this polygon
      indices - The array of indices in this polygon
    • Poly2

      public Poly2(Poly2 poly)
      Creates a polygon that is a copy of the given one.

      All of the contents are copied, so that this polygon does not hold any references to elements of the other polygon.

      Parameters:
      poly - The polygon to copy
    • Poly2

      public Poly2(com.badlogic.gdx.math.Rectangle rect)
      Creates a polygon for the given rectangle.

      The polygon will have four vertices, one for each corner of the rectangle. The indices will define two triangles on these vertices. This method is faster than using one of the more heavy-weight triangulators.

      Parameters:
      rect - The rectangle to copy
    • Poly2

      public Poly2(float x, float y, float w, float h)
      Creates a polygon for the given rectangle.

      The polygon will have four vertices, one for each corner of the rectangle. The indices will define two triangles on these vertices. This method is faster than using one of the more heavy-weight triangulators.

      Parameters:
      x - The x-coordinate of the bottom left corner
      y - The y-coordinate of the bottom left corner
      w - The rectangle width
      h - The rectangle height
  • Method Details

    • set

      public Poly2 set(float[] vertices)
      Sets the polygon to have the given vertices

      The resulting polygon has no indices triangulating the vertices.

      This method returns a reference to this polygon for chaining.

      Parameters:
      vertices - The vector of vertices (as Vec2) in this polygon
      Returns:
      This polygon, returned for chaining
    • set

      public Poly2 set(float[] vertices, int voffset, int vlength)
      Sets the polygon to have the given vertices.

      The resulting polygon has no indices triangulating the vertices.

      This method returns a reference to this polygon for chaining.

      Parameters:
      vertices - The array of vertices (as Vec2) in this polygon
      voffset - The offset of the first array vertex
      vlength - The array size to use
      Returns:
      This polygon, returned for chaining
    • set

      public Poly2 set(Poly2 poly)
      Sets this polygon to be a copy of the given one.

      All of the contents are copied, so that this polygon does not hold any references to elements of the other polygon.

      This method returns a reference to this polygon for chaining.

      Parameters:
      poly - The polygon to copy
      Returns:
      This polygon, returned for chaining
    • set

      public Poly2 set(com.badlogic.gdx.math.Rectangle rect)
      Sets the polygon to represent the given rectangle.

      The polygon will have four vertices, one for each corner of the rectangle. The indices will define two triangles on these vertices. This method is faster than using one of the more heavy-weight triangulators.

      Parameters:
      rect - The rectangle to copy
      Returns:
      This polygon, returned for chaining
    • set

      public Poly2 set(float x, float y, float w, float h)
      Sets the polygon to represent the given rectangle.

      The polygon will have four vertices, one for each corner of the rectangle. The indices will define two triangles on these vertices. This method is faster than using one of the more heavy-weight triangulators.

      Parameters:
      x - The x-coordinate of the bottom left corner
      y - The y-coordinate of the bottom left corner
      w - The rectangle width
      h - The rectangle height
      Returns:
      This polygon, returned for chaining
    • push

      public void push(com.badlogic.gdx.math.Vector2 point)
      Adds a point to the end of this polygon

      This method does not update the indices, and so does not include the new point in any triangles.

      Parameters:
      point - The point to add
    • push

      public void push(float x, float y)
      Adds a point to the end of this polygon

      This method does not update the indices, and so does not include the new point in any triangles.

      Parameters:
      x - The x-coordinate to add
      y - The y-coordinate to add
    • clear

      public void clear()
      Clears the contents of this polygon (both vertices and indices)
    • makePolyRegion

      public com.badlogic.gdx.graphics.g2d.PolygonRegion makePolyRegion(com.badlogic.gdx.graphics.g2d.TextureRegion region)
      Converts the current Poly2 object to PolygonRegion.

      This method is useful for using Poly2 objects with legacy LibGDX graphics pipelines.

      Parameters:
      region - The TextureRegion used for PolygonRegion
      Returns:
      The PolygonRegion after conversion.
    • scl

      public Poly2 scl(float scale)
      Uniformly scales all of the vertices of this polygon.

      The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.

      Parameters:
      scale - The uniform scaling factor
      Returns:
      This polygon, scaled uniformly.
    • scl

      public Poly2 scl(com.badlogic.gdx.math.Vector2 scale)
      Nonuniformly scales all of the vertices of this polygon.

      The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.

      Parameters:
      scale - The non-uniform scaling factor
      Returns:
      This polygon, scaled non-uniformly.
    • div

      public Poly2 div(float scale)
      Uniformly scales all of the vertices of this polygon.

      The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.

      Parameters:
      scale - The inverse of the uniform scaling factor
      Returns:
      This polygon, scaled uniformly.
    • div

      public Poly2 div(com.badlogic.gdx.math.Vector2 scale)
      Nonuniformly scales all of the vertices of this polygon.

      The vertices are scaled from the origin of the coordinate space. This means that if the origin is not in the interior of this polygon, the polygon will be effectively translated by the scaling.

      Parameters:
      scale - The inverse of the non-uniform scaling factor
      Returns:
      This polygon, scaled non-uniformly.
    • add

      public Poly2 add(float offset)
      Uniformly translates all of the vertices of this polygon.
      Parameters:
      offset - The uniform translation amount
      Returns:
      This polygon, translated uniformly.
    • add

      public Poly2 add(com.badlogic.gdx.math.Vector2 offset)
      Non-uniformly translates all of the vertices of this polygon.
      Parameters:
      offset - The non-uniform translation amount
      Returns:
      This polygon, translated non-uniformly.
    • sub

      public Poly2 sub(float offset)
      Uniformly translates all of the vertices of this polygon.
      Parameters:
      offset - The inverse of the uniform translation amount
      Returns:
      This polygon, translated uniformly.
    • sub

      public Poly2 sub(com.badlogic.gdx.math.Vector2 offset)
      Non-uniformly translates all of the vertices of this polygon.
      Parameters:
      offset - The inverse of the non-uniform translation amount
      Returns:
      This polygon, translated non-uniformly.
    • transform

      public Poly2 transform(com.badlogic.gdx.math.Affine2 transform)
      Transforms all of the vertices of this polygon.
      Parameters:
      transform - The transform to apply to each vertex
      Returns:
      This polygon, transformed at each vertex.
    • size

      public int size()
      Returns the number of vertices in the polygon.
      Returns:
      the number of vertices in the polygon.
    • indexSize

      public int indexSize()
      Returns the number of indices in the polygon.
      Returns:
      the number of indices in the polygon.
    • vertexAt

      public com.badlogic.gdx.math.Vector2 vertexAt(int index)
      Returns the point at given index

      This accessor will not permit any changes to the vertex array. To change the array, you must change the polygon via a set() method.

      Parameters:
      index - the index of the point
      Returns:
      the former end point in the path
    • vertexAt

      public com.badlogic.gdx.math.Vector2 vertexAt(int index, com.badlogic.gdx.math.Vector2 point)
      Returns the point at given index

      The vector object returned is the one provided, unless it is null.

      This accessor will not permit any changes to the vertex array. To change the array, you must change the polygon via a set() method.

      Parameters:
      index - the index of the point
      point - The object to store the result
      Returns:
      the former end point in the path
    • getVertices

      public com.badlogic.gdx.math.Vector2[] getVertices()
      Returns the list of vertices

      This accessor will not permit any changes to the vertex array. To change the array, you must change the polygon via a set() method.

      Returns:
      a reference to the vertex array
    • getBounds

      public com.badlogic.gdx.math.Rectangle getBounds()
      Returns the bounding box for the polygon

      The bounding box is the minimal rectangle that contains all of the vertices in this polygon. It is recomputed whenever the vertices are set.

      Returns:
      the bounding box for the polygon
    • getBounds

      public com.badlogic.gdx.math.Rectangle getBounds(com.badlogic.gdx.math.Rectangle rect)
      Sets given rectangle to be the bounding box for the polygon

      The bounding box is the minimal rectangle that contains all of the vertices in this polygon. It is recomputed whenever the vertices are set.

      If the provided rectangle is null, this method will create a new rectangle.

      Parameters:
      rect - the rectangle to store the result
      Returns:
      the rectangle storing the result
    • contains

      public boolean contains(float x, float y)
      Returns true if this polygon contains the given point.

      Unlike Path2, this method does not use an even-odd rule. Instead, it checks for containment within the associated triangle mesh.

      Containment is not strict. Points on the boundary are contained within this polygon.

      Parameters:
      x - x-pos of the point
      y - y-pos of the point
      Returns:
      true if this polygon contains the given point.
    • contains

      public boolean contains(com.badlogic.gdx.math.Vector2 point)
      Returns true if this polygon contains the given point.

      Unlike Path2, this method does not use an even-odd rule. Instead, it checks for containment within the associated triangle mesh.

      Containment is not strict. Points on the boundary are contained within this polygon.

      Parameters:
      point - The point to test
      Returns:
      true if this polygon contains the given point.
    • incident

      public boolean incident(float x, float y, float err)
      Returns true if this polygon contains the given point.

      Unlike Path2, this method does not use an even-odd rule. Instead, it checks for containment within the associated triangle mesh.

      Containment is not strict. Points on the boundary are contained within this polygon.

      Parameters:
      x - The x-coordinate to test
      y - The y-coordinate to test
      err - The error tolerance
      Returns:
      true if this polygon contains the given point.
    • incident

      public boolean incident(com.badlogic.gdx.math.Vector2 point, float err)
      Returns true if the given point is on the boundary of this polygon.

      This method generates uses boundaries() to determine the boundaries. It returns true if the point is within margin of error of a line segment on one of the boundaries.

      Parameters:
      point - The point to check
      err - The distance tolerance
      Returns:
      true if the given point is on the boundary of this polygon.
    • exterior

      public short[] exterior()
      Returns the setx of indices that are on a boundary of this polygon

      This method can identify the outer hull using the graph properties of the triangle mesh. An internal node if the number of neighbors is the same as the number of attached triangles. An index that is not internal is external.

      Unlike boundaries(), this method does not order the boundary indices or decompose them into connected components.

      Returns:
      the set of indices that are on a boundary of this polygon
    • boundaries

      public short[][] boundaries()
      Returns the connected boundary components for this polygon.

      This method allows us to reconstruct the exterior boundary of a solid shape, or to compose a pathwise connected curve into components.

      This method detriangulates the polygon mesh, returning the outer hull, discarding any interior points. This hull need not be convex. If the mesh represents a simple polygon, only one boundary will be returned. If the mesh is not continuous, the outer array will contain the boundary of each disjoint polygon. If the mesh has holes, each hole will be returned as a separate boundary. There is no guarantee on the order of boundaries returned.

      Returns:
      the connected boundary components for this polygon.
    • toString

      public String toString(boolean verbose)
      Returns a string representation of this rectangle for debugging purposes.

      If verbose is true, the string will include class information. This allows us to unambiguously identify the class.

      Parameters:
      verbose - Whether to include class information
      Returns:
      a string representation of this rectangle for debuggging purposes.
    • toString

      public String toString()
      Returns a string representation of this rectangle for debugging purposes.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this rectangle for debuggging purposes.