- Object
-
- AbstractShape
-
- Polygon
-
- All Implemented Interfaces:
DataContainer
,Convex
,Rotatable
,Shape
,Transformable
,Translatable
,Wound
public class Polygon extends AbstractShape implements Convex, Wound, Shape, Transformable, DataContainer
Implementation of an arbitrary polygonConvex
Shape
.A
Polygon
must have at least 3 vertices where one of which is not colinear with the other two. APolygon
must also beConvex
and have counter-clockwise winding of points.A polygon cannot have coincident vertices.
- Since:
- 1.0.0
- Version:
- 3.4.0
- Author:
- William Bittle
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
computeAABB(Transform transform, AABB aabb)
boolean
contains(Vector2 point, Transform transform)
Returns true if the given point is inside thisShape
.Mass
createMass(double density)
Vector2[]
getAxes(Vector2[] foci, Transform transform)
Returns an array of separating axes to test for thisShape
.EdgeFeature
getFarthestFeature(Vector2 vector, Transform transform)
Returns theFeature
farthest in the direction of the given vector.Vector2
getFarthestPoint(Vector2 vector, Transform transform)
Returns the point farthest in the direction of the given vector.Vector2[]
getFoci(Transform transform)
Returns an array of world space foci points for circular curved edges.Iterator<Vector2>
getNormalIterator()
Returns an iterator for the normals.Vector2[]
getNormals()
Returns the array of edge normals in local coordinates.double
getRadius(Vector2 center)
Returns the radius of the shape if the given point was the center for this shape.Iterator<Vector2>
getVertexIterator()
Returns an iterator for the vertices.Vector2[]
getVertices()
Returns the array of vertices in local coordinates.Interval
project(Vector2 vector, Transform transform)
void
rotate(Rotation rotation, double x, double y)
Rotates the object about the given point.String
toString()
void
translate(double x, double y)
Translates the object the given amounts in the respective directions.-
Methods inherited from class AbstractShape
computeAABB, contains, createAABB, createAABB, getCenter, getRadius, getUserData, project, rotate, rotate, rotate, rotate, rotate, rotateAboutCenter, setUserData, translate
-
Methods inherited from interface DataContainer
getUserData, setUserData
-
Methods inherited from interface Shape
computeAABB, contains, createAABB, createAABB, getCenter, getRadius, project, rotateAboutCenter
-
Methods inherited from interface Translatable
translate
-
-
-
-
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 elementIllegalArgumentException
- if vertices contains less than 3 points, contains coincident points, is not convex, or has clockwise winding
-
-
Method Detail
-
toString
public String toString()
- Overrides:
toString
in classAbstractShape
-
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 interfaceWound
- 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 interfaceWound
- 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 interfaceWound
- 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 interfaceWound
- 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.
-
getAxes
public Vector2[] getAxes(Vector2[] foci, Transform transform)
Description copied from interface:Convex
Returns an array of separating axes to test for thisShape
.The
foci
parameter is an array of circular focal points of the otherShape
.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.
-
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.
-
contains
public boolean contains(Vector2 point, Transform transform)
Description copied from interface:Shape
-
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 interfaceRotatable
- Overrides:
rotate
in classAbstractShape
- Parameters:
rotation
- theRotation
representing the rotation amountx
- the x coordinate to rotate abouty
- 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 interfaceTranslatable
- Overrides:
translate
in classAbstractShape
- Parameters:
x
- the translation in the x directiony
- the translation in the y direction
-
project
public Interval project(Vector2 vector, Transform transform)
Description copied from interface:Shape
-
getFarthestFeature
public EdgeFeature getFarthestFeature(Vector2 vector, Transform transform)
Description copied from interface:Convex
Returns theFeature
farthest in the direction of the given vector.The returned feature is in world space.
-
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 givenVector2
the first one is used.The returned point is in world space.
-
createMass
public Mass createMass(double density)
Creates aMass
object using the geometric properties of thisPolygon
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 interfaceShape
- Parameters:
density
- the density in kg/m2- Returns:
Mass
theMass
of thisPolygon
-
computeAABB
public void computeAABB(Transform transform, AABB aabb)
Description copied from interface:Shape
Computes theAABB
from thisShape
after applying the given transformation and places the result in the givenAABB
.- Specified by:
computeAABB
in interfaceShape
- Parameters:
transform
- theTransform
for thisShape
aabb
- the destinationAABB
-
-