math.geom2d.polygon
Class Polygon2D

java.lang.Object
  extended by math.geom2d.polygon.Polygon2D
All Implemented Interfaces:
java.awt.Shape, Domain2D, PolygonalShape2D, Shape2D

public class Polygon2D
extends java.lang.Object
implements PolygonalShape2D

Represent a polygonal domain whose boundary is a single closed polyline.


Field Summary
protected  java.util.ArrayList<Point2D> points
          The inner ordered list of vertices.
 
Fields inherited from interface math.geom2d.Shape2D
ACCURACY, defaultClipWindow, EMPTY_SET
 
Constructor Summary
Polygon2D()
          Empty constructor: no vertex.
Polygon2D(java.util.Collection<? extends Point2D> points)
           
Polygon2D(double[] xcoords, double[] ycoords)
          Constructor from two arrays, one for each coordinate.
Polygon2D(Point2D[] tab)
          Constructor from an array of points
 
Method Summary
 void addPoint(Point2D point)
          Add a point as the last vertex.
 void clearPoints()
          remove all the vertices of the polygon.
 Shape2D clip(Box2D box)
          Return the shape formed by the polygon clipped by the given box.
 boolean contains(double x, double y)
          Return true if the point (x, y) lies inside the polygon, with precision given by Shape2D.ACCURACY.
 boolean contains(double x, double y, double w, double h)
          return false, because a line cannot contain a rectangle.
 boolean contains(java.awt.geom.Point2D p)
          Return true if the point p lies inside the polygon, with precision given by Shape2D.ACCURACY.
 boolean contains(java.awt.geom.Rectangle2D r)
          return false, because a line cannot contain a rectangle.
 boolean equals(java.lang.Object obj)
          Test if the two polygons are equal.
 double getArea()
          Computes area of the polygon, by returning the absolute value of the signed area.
 Boundary2D getBoundary()
          Returns a closed polyline, which encloses the polygon.
 Box2D getBoundingBox()
          Return the bounding box of the polygon.
 java.awt.Rectangle getBounds()
          Return bounding box of the shape.
 java.awt.geom.Rectangle2D getBounds2D()
          Return more precise bounds for the shape.
 Point2D getCentroid()
          Computes the centroid (center of mass) of the polygon.
 Shape2D getClippedShape(Box2D box)
          Return the shape formed by the polygon clipped by the given box.
 double getDistance(double x, double y)
          Get the distance of the point to the polygon.
 double getDistance(java.awt.geom.Point2D p)
          Get the distance of the point to the polygon.
 java.util.Collection<LineSegment2D> getEdges()
          Return the set of edges, as a collection of LineSegment2D.
 java.awt.geom.GeneralPath getGeneralPath()
          Return a general path iterator.
 java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform trans)
          Return pathiterator for this polygon.
 java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform trans, double flatness)
          Return pathiterator for this polygon.
 java.util.Iterator<Point2D> getPoints()
          Deprecated. use getVertices() instead.
 double getSignedArea()
          Computes the signed area of the polygon.
 double getSignedDistance(double x, double y)
          Get the signed distance of the shape to the given point : this distance is positive if the point lies outside the shape, and is negative if the point lies inside the shape.
 double getSignedDistance(java.awt.geom.Point2D p)
          Get the signed distance of the shape to the given point : this distance is positive if the point lies outside the shape, and is negative if the point lies inside the shape.
 java.util.Collection<Point2D> getVertices()
          Returns the points of the polygon.
 int getVerticesNumber()
          Return the number of vertices.
 int getWindingNumber(double x, double y)
          Computes the winding number of the polygon.
 boolean intersects(double x, double y, double w, double h)
          Tests if the Polygon intersects the interior of a specified rectangular area.
 boolean intersects(java.awt.geom.Rectangle2D r)
          Tests if the Polygon intersects the interior of a specified rectangle2D.
 boolean isBounded()
          Always returns true if polygon is oriented counter-clockwise, false otherwise.
 void removePoint(Point2D point)
          Remove a vertex of the polygon.
 Polygon2D transform(AffineTransform2D trans)
          Return the new Polygon created by an affine transform of this polygon.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

points

protected java.util.ArrayList<Point2D> points
The inner ordered list of vertices. The last point is connected to the first one.

Constructor Detail

Polygon2D

public Polygon2D()
Empty constructor: no vertex.


Polygon2D

public Polygon2D(Point2D[] tab)
Constructor from an array of points

Parameters:
tab - the vertices stored in an array of Point2D

Polygon2D

public Polygon2D(double[] xcoords,
                 double[] ycoords)
Constructor from two arrays, one for each coordinate.

Parameters:
xcoords - the x coordinate of each vertex
ycoords - the y coordinate of each vertex

Polygon2D

public Polygon2D(java.util.Collection<? extends Point2D> points)
Method Detail

addPoint

public void addPoint(Point2D point)
Add a point as the last vertex.


removePoint

public void removePoint(Point2D point)
Remove a vertex of the polygon.

Parameters:
point - the vertex to be removed.

getArea

public double getArea()
Computes area of the polygon, by returning the absolute value of the signed area.


getSignedArea

public double getSignedArea()
Computes the signed area of the polygon. Algorithm is taken from page: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/. Signed are is positive if polygon is oriented counter-clockwise, and negative otherwise. Result is wrong if polygon is self-intersecting.

Returns:
the signed area of the polygon.

getCentroid

public Point2D getCentroid()
Computes the centroid (center of mass) of the polygon. Algorithm is taken from page: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/.

Returns:
the centroid of the polygon

getWindingNumber

public int getWindingNumber(double x,
                            double y)
Computes the winding number of the polygon. Algorithm adapted from http://www.geometryalgorithms.com/Archive/algorithm_0103/algorithm_0103.htm

Parameters:
x - the x-coordinate of the point
y - the y-coordinate of the point
Returns:
the number of windings of the curve around the point

getPoints

public java.util.Iterator<Point2D> getPoints()
Deprecated. use getVertices() instead.

Return an iterator on the vertices.


getVertices

public java.util.Collection<Point2D> getVertices()
Returns the points of the polygon. The result is a pointer to the inner collection of vertices.

Specified by:
getVertices in interface PolygonalShape2D

getVerticesNumber

public int getVerticesNumber()
Return the number of vertices.

Specified by:
getVerticesNumber in interface PolygonalShape2D

getEdges

public java.util.Collection<LineSegment2D> getEdges()
Return the set of edges, as a collection of LineSegment2D.

Specified by:
getEdges in interface PolygonalShape2D

clearPoints

public void clearPoints()
remove all the vertices of the polygon.


getBoundary

public Boundary2D getBoundary()
Returns a closed polyline, which encloses the polygon.

Specified by:
getBoundary in interface Domain2D
Returns:
the boundary of the domain

getDistance

public double getDistance(java.awt.geom.Point2D p)
Get the distance of the point to the polygon. This is actually the minimal distance computed for each edge if the polygon, or ZERO if the point belong to the polygon.

Specified by:
getDistance in interface Shape2D

getDistance

public double getDistance(double x,
                          double y)
Get the distance of the point to the polygon. This is actually the minimal distance computed for each edge if the polygon, or ZERO if the point belong to the polygon.

Specified by:
getDistance in interface Shape2D

getSignedDistance

public double getSignedDistance(java.awt.geom.Point2D p)
Get the signed distance of the shape to the given point : this distance is positive if the point lies outside the shape, and is negative if the point lies inside the shape. In this case, absolute value of distance is equals to the distance to the border of the shape.


getSignedDistance

public double getSignedDistance(double x,
                                double y)
Get the signed distance of the shape to the given point : this distance is positive if the point lies outside the shape, and is negative if the point lies inside the shape. In this case, absolute value of distance is equals to the distance to the border of the shape.


getClippedShape

public Shape2D getClippedShape(Box2D box)
Return the shape formed by the polygon clipped by the given box.


clip

public Shape2D clip(Box2D box)
Return the shape formed by the polygon clipped by the given box.

Specified by:
clip in interface Shape2D
Parameters:
box - the clipping box
Returns:
the clipped shape

getBoundingBox

public Box2D getBoundingBox()
Return the bounding box of the polygon.

Specified by:
getBoundingBox in interface Shape2D
Returns:
the bounding box of the shape.

isBounded

public boolean isBounded()
Always returns true if polygon is oriented counter-clockwise, false otherwise.

Specified by:
isBounded in interface Shape2D

transform

public Polygon2D transform(AffineTransform2D trans)
Return the new Polygon created by an affine transform of this polygon.

Specified by:
transform in interface PolygonalShape2D
Specified by:
transform in interface Shape2D
Parameters:
trans - an affine transform
Returns:
the transformed shape

contains

public boolean contains(double x,
                        double y,
                        double w,
                        double h)
return false, because a line cannot contain a rectangle.

Specified by:
contains in interface java.awt.Shape

contains

public boolean contains(java.awt.geom.Rectangle2D r)
return false, because a line cannot contain a rectangle.

Specified by:
contains in interface java.awt.Shape

contains

public boolean contains(java.awt.geom.Point2D p)
Return true if the point p lies inside the polygon, with precision given by Shape2D.ACCURACY.

Specified by:
contains in interface java.awt.Shape

contains

public boolean contains(double x,
                        double y)
Return true if the point (x, y) lies inside the polygon, with precision given by Shape2D.ACCURACY.

Specified by:
contains in interface java.awt.Shape

getBounds

public java.awt.Rectangle getBounds()
Return bounding box of the shape.

Specified by:
getBounds in interface java.awt.Shape

getBounds2D

public java.awt.geom.Rectangle2D getBounds2D()
Return more precise bounds for the shape.

Specified by:
getBounds2D in interface java.awt.Shape

intersects

public boolean intersects(double x,
                          double y,
                          double w,
                          double h)
Tests if the Polygon intersects the interior of a specified rectangular area.

Specified by:
intersects in interface java.awt.Shape

intersects

public boolean intersects(java.awt.geom.Rectangle2D r)
Tests if the Polygon intersects the interior of a specified rectangle2D.

Specified by:
intersects in interface java.awt.Shape

getGeneralPath

public java.awt.geom.GeneralPath getGeneralPath()
Return a general path iterator.


getPathIterator

public java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform trans)
Return pathiterator for this polygon.

Specified by:
getPathIterator in interface java.awt.Shape

getPathIterator

public java.awt.geom.PathIterator getPathIterator(java.awt.geom.AffineTransform trans,
                                                  double flatness)
Return pathiterator for this polygon.

Specified by:
getPathIterator in interface java.awt.Shape

equals

public boolean equals(java.lang.Object obj)
Test if the two polygons are equal. Test first the number of vertices, then the bounding boxes, then if each vertex of the polygon is contained in the vertices array of this polygon.

Overrides:
equals in class java.lang.Object