toxi.geom
Class Polygon2D

java.lang.Object
  extended by toxi.geom.Polygon2D
All Implemented Interfaces:
Shape2D

public class Polygon2D
extends java.lang.Object
implements Shape2D

Container type for convex polygons. Implements Shape2D.


Field Summary
 java.util.List<Vec2D> vertices
           
 
Constructor Summary
Polygon2D()
           
Polygon2D(java.util.List<Vec2D> points)
           
Polygon2D(Vec2D... points)
           
 
Method Summary
 Polygon2D add(Vec2D p)
          Adds a new vertex to the polygon (builder pattern).
 boolean containsPoint(ReadonlyVec2D p)
          Checks if the point is within the given shape.
 Polygon2D flipVertexOrder()
          Flips the ordering of the polygon's vertices.
 float getArea()
          Computes the area of the polygon, provided it isn't self intersecting.
 Vec2D getCentroid()
          Computes the polygon's centre of mass.
 float getCircumference()
          Computes the shape's circumference.
 int getNumPoints()
          Returns the number of polygon vertices.
 boolean isClockwise()
          Checks if the vertices of this polygon are in clockwise ordering by examining the first 3.
 Polygon2D reverseOrientation()
          Deprecated. 
 Polygon2D smooth(float amount, float baseWeight)
          Applies a laplacian-style smooth operation to all polygon vertices, causing sharp corners/angles to widen and results in a general smoother shape.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

vertices

public java.util.List<Vec2D> vertices
Constructor Detail

Polygon2D

public Polygon2D()

Polygon2D

public Polygon2D(java.util.List<Vec2D> points)

Polygon2D

public Polygon2D(Vec2D... points)
Method Detail

add

public Polygon2D add(Vec2D p)
Adds a new vertex to the polygon (builder pattern).

Parameters:
p - vertex point to add
Returns:
itself

containsPoint

public boolean containsPoint(ReadonlyVec2D p)
Description copied from interface: Shape2D
Checks if the point is within the given shape.

Specified by:
containsPoint in interface Shape2D
Returns:
true, if inside

flipVertexOrder

public Polygon2D flipVertexOrder()
Flips the ordering of the polygon's vertices.

Returns:
itself

getArea

public float getArea()
Computes the area of the polygon, provided it isn't self intersecting. Code ported from: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/

Specified by:
getArea in interface Shape2D
Returns:
polygon area

getCentroid

public Vec2D getCentroid()
Computes the polygon's centre of mass. Code ported from: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/

Returns:
centroid point

getCircumference

public float getCircumference()
Description copied from interface: Shape2D
Computes the shape's circumference.

Specified by:
getCircumference in interface Shape2D
Returns:
circumference

getNumPoints

public int getNumPoints()
Returns the number of polygon vertices.

Returns:
vertex count

isClockwise

public boolean isClockwise()
Checks if the vertices of this polygon are in clockwise ordering by examining the first 3.

Returns:
true, if clockwise

reverseOrientation

@Deprecated
public Polygon2D reverseOrientation()
Deprecated. 


smooth

public Polygon2D smooth(float amount,
                        float baseWeight)
Applies a laplacian-style smooth operation to all polygon vertices, causing sharp corners/angles to widen and results in a general smoother shape. Let the current vertex be A and its neighbours P and Q, then A will be moved by a specified amount into the direction given by (P-A)+(Q-A). Additionally, and to avoid shrinking of the shape through repeated iteration of this procedure, the vector A - C (Polygon centroid) is added as counter force and a weight for its impact can be specified. To keep the average size of the polygon stable, this weight value should be ~1/2 of the smooth amount.

Parameters:
amount - smooth amount (between 0 < x < 0.5)
baseWeight - counter weight (0 <= x < 1/2 * smooth amount)
Returns:
itself

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object