Module org.dyn4j

## Class Polygon

• All Implemented Interfaces:
`DataContainer`, `Convex`, `Rotatable`, `Shape`, `Transformable`, `Translatable`, `Wound`
Direct Known Subclasses:
`Rectangle`, `Triangle`

```public class Polygon
extends AbstractShape
implements Convex, Wound, Shape, Transformable, DataContainer```
Implementation of an arbitrary polygon `Convex` `Shape`.

A `Polygon` must have at least 3 vertices where one of which is not colinear with the other two. A `Polygon` must also be `Convex` and have counter-clockwise winding of points.

A polygon cannot have coincident vertices.

Since:
1.0.0
Version:
3.4.0
Author:
William Bittle
• ### Field Summary

Fields
Modifier and Type Field Description
`(package private) Vector2[]` `normals`
The polygon normals
`(package private) Vector2[]` `vertices`
The polygon vertices
• ### Fields inherited from class org.dyn4j.geometry.AbstractShape

`center, id, radius, userData`
• ### Constructor Summary

Constructors
Constructor Description
`Polygon​(Vector2... vertices)`
Full constructor.
```Polygon​(Vector2 center, double radius, Vector2[] vertices, Vector2[] normals)```
Full constructor for sub classes.
• ### Method Summary

All Methods
Modifier and Type Method Description
`boolean` ```contains​(Vector2 point, Transform transform)```
Returns true if the given point is inside this `Shape`.
`AABB` `createAABB​(Transform transform)`
Creates an `AABB` from this `Shape` after applying the given transformation to the shape.
`Mass` `createMass​(double density)`
Creates a `Mass` object using the geometric properties of this `Polygon` and the given density.
`Vector2[]` ```getAxes​(Vector2[] foci, Transform transform)```
Returns an array of separating axes to test for this `Shape`.
`EdgeFeature` ```getFarthestFeature​(Vector2 vector, Transform transform)```
Returns the `Feature` 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.
`(package private) int` `getFarthestVertexIndex​(Vector2 vector)`
Internal helper method that returns the index of the point that is farthest in direction of a vector.
`Vector2[]` `getFoci​(Transform transform)`
Returns an array of world space foci points for circular curved edges.
`java.util.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.
`java.util.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)```
Returns the `Interval` of this `Shape` projected onto the given `Vector2` given the `Transform`.
`void` ```rotate​(Rotation rotation, double x, double y)```
Rotates the object about the given point.
`java.lang.String` `toString()`
`void` ```translate​(double x, double y)```
Translates the object the given amounts in the respective directions.
• ### Methods inherited from class org.dyn4j.geometry.AbstractShape

`contains, createAABB, getCenter, getId, getRadius, getUserData, project, rotate, rotate, rotate, rotate, rotate, rotateAboutCenter, setUserData, translate`
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
• ### Methods inherited from interface org.dyn4j.DataContainer

`getUserData, setUserData`
• ### Methods inherited from interface org.dyn4j.geometry.Rotatable

`rotate, rotate, rotate, rotate, rotate`
• ### Methods inherited from interface org.dyn4j.geometry.Shape

`contains, createAABB, getCenter, getId, getRadius, project, rotateAboutCenter`
• ### Methods inherited from interface org.dyn4j.geometry.Translatable

`translate`
• ### Field Detail

• #### vertices

`final Vector2[] vertices`
The polygon vertices
• #### normals

`final Vector2[] normals`
The polygon normals
• ### Constructor Detail

• #### Polygon

```Polygon​(Vector2 center,
Vector2[] vertices,
Vector2[] normals)```
Full constructor for sub classes.
Parameters:
`center` - the center
`radius` - the rotation radius
`vertices` - the vertices
`normals` - the normals
• #### 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:
`java.lang.NullPointerException` - if vertices is null or contains a null element
`java.lang.IllegalArgumentException` - if vertices contains less than 3 points, contains coincident points, is not convex, or has clockwise winding
• ### Method Detail

• #### toString

`public java.lang.String toString()`
Overrides:
`toString` in class `AbstractShape`
• #### 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 interface `Wound`
Returns:
`Vector2`[]
`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 interface `Wound`
Returns:
`Vector2`[]
`Wound.getNormalIterator()`
• #### getVertexIterator

`public java.util.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 interface `Wound`
Returns:
Iterator<`Vector2`>
• #### getNormalIterator

`public java.util.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 interface `Wound`
Returns:
Iterator<`Vector2`>

`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.
Specified by:
`getRadius` in interface `Shape`
Parameters:
`center` - the center point
Returns:
double
• #### getAxes

```public Vector2[] getAxes​(Vector2[] foci,
Transform transform)```
Description copied from interface: `Convex`
Returns an array of separating axes to test for this `Shape`.

The `foci` parameter is an array of circular focal points of the other `Shape`.

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.

Specified by:
`getAxes` in interface `Convex`
Parameters:
`foci` - the world space points representing foci of curved `Shape`s; can be null
`transform` - the local to world space `Transform` of this `Convex` `Shape`
Returns:
`Vector2`[]
• #### 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.

Specified by:
`getFoci` in interface `Convex`
Parameters:
`transform` - the local to world space `Transform` of this `Convex` `Shape`
Returns:
null
• #### contains

```public boolean contains​(Vector2 point,
Transform transform)```
Description copied from interface: `Shape`
Returns true if the given point is inside this `Shape`.

If the given point lies on an edge the point is considered to be inside the `Shape`.

The given point is assumed to be in world space.

Specified by:
`contains` in interface `Shape`
Parameters:
`point` - world space point
`transform` - `Transform` for this `Shape`
Returns:
boolean
• #### 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 interface `Rotatable`
Overrides:
`rotate` in class `AbstractShape`
Parameters:
`rotation` - the `Rotation` representing the rotation amount
`x` - the x coordinate to rotate about
`y` - 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 interface `Translatable`
Overrides:
`translate` in class `AbstractShape`
Parameters:
`x` - the translation in the x direction
`y` - the translation in the y direction
• #### getFarthestFeature

```public EdgeFeature getFarthestFeature​(Vector2 vector,
Transform transform)```
Description copied from interface: `Convex`
Returns the `Feature` farthest in the direction of the given vector.

The returned feature is in world space.

Specified by:
`getFarthestFeature` in interface `Convex`
Parameters:
`vector` - the direction
`transform` - the local to world space `Transform` of this `Convex` `Shape`
Returns:
`Feature`
• #### 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 given `Vector2` the first one is used.

The returned point is in world space.

Specified by:
`getFarthestPoint` in interface `Convex`
Parameters:
`vector` - the direction
`transform` - the local to world space `Transform` of this `Convex` `Shape`
Returns:
`Vector2`
• #### getFarthestVertexIndex

`int getFarthestVertexIndex​(Vector2 vector)`
Internal helper method that returns the index of the point that is farthest in direction of a vector.
Parameters:
`vector` - the direction
Returns:
the index of the farthest vertex in that direction
Since:
3.4.0
• #### createMass

`public Mass createMass​(double density)`
Creates a `Mass` object using the geometric properties of this `Polygon` 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 interface `Shape`
Parameters:
`density` - the density in kg/m2
Returns:
`Mass` the `Mass` of this `Polygon`