edu.eside.flingbox.math
Class PolygonUtils

java.lang.Object
  extended by edu.eside.flingbox.math.PolygonUtils

public final class PolygonUtils
extends java.lang.Object

Implements some utilities for polygons.


Method Summary
static Vector2D[] computePolygonNormals(Vector2D[] contour)
          Computes the polygon's normals.
static float distanceFromLineToPoint(Vector2D p0, Vector2D p1, Vector2D p)
          Computes minimum distance from line to point
static Vector2D[] douglasPeuckerReducer(Vector2D[] points, float epsilon)
          The Douglas-Peucker algorithm is an algorithm for reducing the number of points in a curve that is approximated by a series of points.
static float polygonArea(Vector2D[] Vector2Ds)
          Computes area of polygon.
static Vector2D polygonCentroid(Vector2D[] contour)
          Computes the polygon's centroid
static boolean polygonConatinsPoint(Vector2D[] polygon, Vector2D Vector2D)
          Checks if a Vector2D is contained by a polygon.
static short[] triangulatePolygon(Vector2D[] Vector2Ds)
          Computes the triangulation of a polygon(tesellation) with ear-clipping algorithm.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

douglasPeuckerReducer

public static Vector2D[] douglasPeuckerReducer(Vector2D[] points,
                                               float epsilon)
The Douglas-Peucker algorithm is an algorithm for reducing the number of points in a curve that is approximated by a series of points. The end of this function is a good moment to call the GarbageCollector

Parameters:
points - Array of the polygon's points
epsilon - Max distance to ignore a point
Returns:
New array with optimized points

triangulatePolygon

public static short[] triangulatePolygon(Vector2D[] Vector2Ds)
Computes the triangulation of a polygon(tesellation) with ear-clipping algorithm.

Parameters:
Vector2Ds - Array of polygon's points
Returns:
Will return n-2 group of 3 points, for n sides polygon or null if not enough points

polygonConatinsPoint

public static boolean polygonConatinsPoint(Vector2D[] polygon,
                                           Vector2D Vector2D)
Checks if a Vector2D is contained by a polygon. It's based on Winding number algorithm. More info at http://en.wikipedia.org/wiki/Winding_number

Parameters:
polygon - polygon's Vector2Ds
Vector2D - Vector2D to be checked

polygonArea

public static float polygonArea(Vector2D[] Vector2Ds)
Computes area of polygon.

Parameters:
Vector2Ds - Polygon's Vector2Ds
Returns:
Polygon's area. if Vector2Ds are counter-clockwise the result will be positive, else it'll be negative

polygonCentroid

public static Vector2D polygonCentroid(Vector2D[] contour)
Computes the polygon's centroid

Parameters:
Vector2Ds - Polygon's Vector2Ds
Returns:
centroid

computePolygonNormals

public static Vector2D[] computePolygonNormals(Vector2D[] contour)
Computes the polygon's normals.

Parameters:
contour - Counterclockwise polygon points
Returns:
Polygon's normals

distanceFromLineToPoint

public static float distanceFromLineToPoint(Vector2D p0,
                                            Vector2D p1,
                                            Vector2D p)
Computes minimum distance from line to point