Class Poly2
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 -
Constructor Summary
ConstructorsConstructorDescriptionPoly2()
Creates an empty polygon.Poly2
(float[] vertices) Creates a polygon with the given verticesPoly2
(float[] vertices, int voffset, int vlength) Creates a the polygon with the given verticesPoly2
(float[] vertices, short[] indices) Creates a polygon with the given vertices and indicesPoly2
(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.Creates a polygon that is a copy of the given one. -
Method Summary
Modifier and TypeMethodDescriptionadd
(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
clear()
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[]
exterior()
Returns the setx of indices that are on a boundary of this polygoncom.badlogic.gdx.math.Rectangle
Returns the bounding box for the polygoncom.badlogic.gdx.math.Rectangle
getBounds
(com.badlogic.gdx.math.Rectangle rect) Sets given rectangle to be the bounding box for the polygoncom.badlogic.gdx.math.Vector2[]
Returns the list of verticesboolean
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 polygonvoid
push
(com.badlogic.gdx.math.Vector2 point) Adds a point to the end of this polygonscl
(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 verticesset
(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.Sets this polygon to be a copy of the given one.int
size()
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.toString()
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 indexcom.badlogic.gdx.math.Vector2
vertexAt
(int index, com.badlogic.gdx.math.Vector2 point) Returns the point at given index
-
Field Details
-
vertices
public com.badlogic.gdx.utils.FloatArray verticesThe vector of vertices in this polygon -
indices
public com.badlogic.gdx.utils.ShortArray indicesThe 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 verticesThe 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 verticesThe resulting polygon has no indices triangulating the vertices.
- Parameters:
vertices
- The vector of vertices (as float in pairs) in this polygonvoffset
- The offset of the first array vertexvlength
- 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 polygonindices
- The array of indices in this polygon
-
Poly2
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 cornery
- The y-coordinate of the bottom left cornerw
- The rectangle widthh
- The rectangle height
-
-
Method Details
-
set
Sets the polygon to have the given verticesThe 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
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 polygonvoffset
- The offset of the first array vertexvlength
- The array size to use- Returns:
- This polygon, returned for chaining
-
set
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
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
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 cornery
- The y-coordinate of the bottom left cornerw
- The rectangle widthh
- 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 polygonThis 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 polygonThis method does not update the indices, and so does not include the new point in any triangles.
- Parameters:
x
- The x-coordinate to addy
- 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
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
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
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
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
Uniformly translates all of the vertices of this polygon.- Parameters:
offset
- The uniform translation amount- Returns:
- This polygon, translated uniformly.
-
add
Non-uniformly translates all of the vertices of this polygon.- Parameters:
offset
- The non-uniform translation amount- Returns:
- This polygon, translated non-uniformly.
-
sub
Uniformly translates all of the vertices of this polygon.- Parameters:
offset
- The inverse of the uniform translation amount- Returns:
- This polygon, translated uniformly.
-
sub
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
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 indexThis 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 indexThe 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 pointpoint
- 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 verticesThis 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 polygonThe 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 polygonThe 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 pointy
- 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 testy
- The y-coordinate to testerr
- 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 checkerr
- 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 polygonThis 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
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
Returns a string representation of this rectangle for debugging purposes.
-