net.phys2d.raw.shapes
Class Polygon

java.lang.Object
  extended bynet.phys2d.raw.shapes.AbstractShape
      extended bynet.phys2d.raw.shapes.Polygon
All Implemented Interfaces:
DynamicShape, Shape
Direct Known Subclasses:
ConvexPolygon

public class Polygon
extends AbstractShape
implements DynamicShape

A polygon represented by a list of its vertices in counterclockwise ordering. Note that the term 'counterclockwise' depends on the orientation of the axes: if x points to the right and y points up, the vertices are counter clockwise. This means that on many displays the ordering of vertices will be clockwise because the y axis is pointing down. TODO: the polygon is immutable but that could be changed


Field Summary
protected  float area
          The total area of this polygon
protected  Vector2f centroid
          The center of mass of this polygon
protected  Vector2f[] vertices
          The vertices of this polygon in counterclockwise order
 
Fields inherited from class net.phys2d.raw.shapes.AbstractShape
bounds
 
Constructor Summary
protected Polygon()
          A constructor that allows for overloading without using the public constructor.
  Polygon(ROVector2f[] vertices)
          Construct the polygon with a list of vertices sorted in counterclockwise order.
 
Method Summary
protected  float computeArea()
          Computes the area as described by Paul Borke.
protected  float computeBoundingCircleRadius()
          Computes the radius of an approximation of a minimal bounding circle which has its origin at (0,0) and sets this.bounds.
protected  Vector2f computeCentroid()
          Compute the centroid (center of mass) as described by Paul Borke.
 boolean contains(ROVector2f p)
          Test whether or not the point p is in this polygon in O(n), where n is the number of vertices in this polygon.
 float getArea()
          Get the area of this polygon
 Vector2f getCentroid()
          Get the center of mass (aka centroid) for this polygon.
 Vector2f getCentroid(ROVector2f displacement, float rotation)
          Returns a translated and rotated copy of this poly's centroid.
 ROVector2f getNearestPoint(ROVector2f p)
          Get point on this polygon's hull that is closest to p.
 float getSurfaceFactor()
          Some factor based on the edges length of the shape
 ROVector2f[] getVertices()
          Returns a copy of the list of vertices.
 Vector2f[] getVertices(ROVector2f displacement, float rotation)
          Returns a translated and rotated copy of this poly's vertices.
 boolean isConvex()
          Check wether or not the polygon is convex.
 
Methods inherited from class net.phys2d.raw.shapes.AbstractShape
getBounds
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.phys2d.raw.shapes.Shape
getBounds
 

Field Detail

vertices

protected Vector2f[] vertices
The vertices of this polygon in counterclockwise order


area

protected float area
The total area of this polygon


centroid

protected Vector2f centroid
The center of mass of this polygon

Constructor Detail

Polygon

public Polygon(ROVector2f[] vertices)
Construct the polygon with a list of vertices sorted in counterclockwise order. Note that all the vector values will be copied. Throws an exception when too few vertices (<3) are supplied. TODO: throw an exception when the vertices arent counterclockwise?

Parameters:
vertices - Vertices sorted in counterclockwise order

Polygon

protected Polygon()
A constructor that allows for overloading without using the public constructor. Does absolutely nothing.

Method Detail

computeArea

protected float computeArea()
Computes the area as described by Paul Borke. See: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/

Returns:
this polygon's computed area

computeCentroid

protected Vector2f computeCentroid()
Compute the centroid (center of mass) as described by Paul Borke. See: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/ Make sure you have computed the area before calling this!

Returns:
the computed centroid

computeBoundingCircleRadius

protected float computeBoundingCircleRadius()
Computes the radius of an approximation of a minimal bounding circle which has its origin at (0,0) and sets this.bounds. TODO: this can be done much better

Returns:
The

getArea

public float getArea()
Get the area of this polygon

Returns:
the area of this polygon

getCentroid

public Vector2f getCentroid()
Get the center of mass (aka centroid) for this polygon.

Returns:
the center of mass

getVertices

public ROVector2f[] getVertices()
Returns a copy of the list of vertices. The vertices are sorted counterclockwise.

Returns:
this polygons vertices

isConvex

public boolean isConvex()
Check wether or not the polygon is convex.

Returns:
true iff this polygon is convex

getVertices

public Vector2f[] getVertices(ROVector2f displacement,
                              float rotation)
Returns a translated and rotated copy of this poly's vertices. The vertices are rotated before they are translated, i.e. they are rotated around the origin (0,0). The vertices are sorted counterclockwise. This function is typically used to get the vertices for a specific body, for example to collide it with another body or draw it.

Parameters:
displacement - The displacement with wich all the
rotation -
Returns:
this polygon's vertices translated and rotated

getCentroid

public Vector2f getCentroid(ROVector2f displacement,
                            float rotation)
Returns a translated and rotated copy of this poly's centroid. The centroid is rotated before it is translated, i.e. it is rotated around the origin (0,0).

Parameters:
displacement - The displacement with wich all the
rotation -
Returns:
this polygon's vertices translated and rotated

contains

public boolean contains(ROVector2f p)
Test whether or not the point p is in this polygon in O(n), where n is the number of vertices in this polygon.

Parameters:
p - The point to be tested for inclusion in this polygon
Returns:
true iff the p is in this polygon (not on a border)

getNearestPoint

public ROVector2f getNearestPoint(ROVector2f p)
Get point on this polygon's hull that is closest to p. TODO: make this thing return a negative value when it is contained in the polygon

Parameters:
p - The point to search the closest point for
Returns:
the nearest point on this vertex' hull

getSurfaceFactor

public float getSurfaceFactor()
Description copied from interface: Shape
Some factor based on the edges length of the shape

Specified by:
getSurfaceFactor in interface Shape
Returns:
The factor result - from the original demo
See Also:
Shape.getSurfaceFactor()