math.geom2d.polygon
Class SimplePolygon2D

java.lang.Object
  extended by math.geom2d.polygon.SimplePolygon2D
All Implemented Interfaces:
CirculinearDomain2D, CirculinearShape2D, Domain2D, GeometricObject2D, Polygon2D, Shape2D

public class SimplePolygon2D
extends java.lang.Object
implements Polygon2D

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


Field Summary
 
Fields inherited from interface math.geom2d.Shape2D
ACCURACY
 
Constructor Summary
SimplePolygon2D()
          Empty constructor: no vertex.
SimplePolygon2D(java.util.Collection<? extends Point2D> points)
           
SimplePolygon2D(double[] xcoords, double[] ycoords)
          Constructor from two arrays, one for each coordinate.
SimplePolygon2D(LinearRing2D ring)
          Creates a simple polygon with the given linear ring representing its boundary.
SimplePolygon2D(Point2D... vertices)
          Constructor from an array of points
 
Method Summary
 void addVertex(Point2D point)
          Adds a point as the last vertex.
 boolean almostEquals(GeometricObject2D obj, double eps)
          Checks if the two objects are similar up to a given threshold value.
 double area()
          Computes the signed area of the polygon.
 Polygon2D asPolygon(int n)
          Returns an approximation of the domain as a polygon, or a MultiPolygon.
 CirculinearContourArray2D<LinearRing2D> boundary()
          Returns a set of one LinearRing2D, which encloses the polygon.
 Box2D boundingBox()
          Returns the bounding box of the polygon.
 CirculinearDomain2D buffer(double dist)
          Computes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.
 Point2D centroid()
          Computes the centroid (center of mass) of the polygon.
 void clearVertices()
          Removes all the vertices of the polygon.
 Polygon2D clip(Box2D box)
          Returns the shape formed by the polygon clipped by the given box.
 SimplePolygon2D clone()
           
 int closestVertexIndex(Point2D point)
          Computes the index of the closest vertex to the input point.
 SimplePolygon2D complement()
          Returns the polygon created by reversing the order of the vertices.
 boolean contains(double x, double y)
          Returns true if the point (x, y) lies inside the polygon, with precision given by Shape2D.ACCURACY.
 boolean contains(Point2D p)
          Returns true if the point p lies inside the polygon, with precision given by Shape2D.ACCURACY.
 java.util.Collection<LinearRing2D> contours()
          Returns the set of contours that enclose this domain.
static SimplePolygon2D create(java.util.Collection<? extends Point2D> points)
          Static factory for creating a new SimplePolygon2D from a collection of points.
static SimplePolygon2D create(Point2D... points)
          Static factory for creating a new SimplePolygon2D from an array of points.
 double distance(double x, double y)
          Returns the distance of the point to the polygon.
 double distance(Point2D p)
          Returns the distance of the point to the polygon.
 void draw(java.awt.Graphics2D g2)
          Draws the shape on the given graphics.
 int edgeNumber()
          Returns the number of edges.
 java.util.Collection<LineSegment2D> edges()
          Returns the set of edges, as a collection of LineSegment2D.
 boolean equals(java.lang.Object obj)
          Tests if the two polygons are equal.
 void fill(java.awt.Graphics2D g)
          Fills the interior of the domain, using the Graphics current Paint.
 java.awt.geom.GeneralPath getGeneralPath()
          Returns a general path iterator.
 LinearRing2D getRing()
          Returns the linear ring that composes the boundary of this polygon.
 int getWindingNumber(double x, double y)
          Computes the winding number of the polygon.
 void insertVertex(int index, Point2D point)
          Adds a point as the last vertex.
 boolean isBounded()
          Returns true if polygon is oriented counter-clockwise, false otherwise.
 boolean isEmpty()
          Returns true if the shape does not contain any point.
 void removeVertex(int index)
          Removes a vertex of the polygon specified by its index.
 boolean removeVertex(Point2D point)
          Removes a vertex of the polygon.
 void setVertex(int index, Point2D position)
          Changes the position of the i-th vertex.
 SimplePolygon2D transform(AffineTransform2D trans)
          Returns the new Polygon created by an affine transform of this polygon.
 CirculinearDomain2D transform(CircleInversion2D inv)
          Transforms the shape by a circle inversion.
 Point2D vertex(int i)
          Returns the i-th vertex of the polygon.
 int vertexNumber()
          Returns the number of vertices of the polygon.
 java.util.Collection<Point2D> vertices()
          Returns the points of the polygon.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimplePolygon2D

public SimplePolygon2D()
Empty constructor: no vertex.


SimplePolygon2D

public SimplePolygon2D(Point2D... vertices)
Constructor from an array of points

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

SimplePolygon2D

public SimplePolygon2D(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

SimplePolygon2D

public SimplePolygon2D(java.util.Collection<? extends Point2D> points)

SimplePolygon2D

public SimplePolygon2D(LinearRing2D ring)
Creates a simple polygon with the given linear ring representing its boundary.

Parameters:
ring - the boundary of the polygon
Method Detail

create

public static SimplePolygon2D create(java.util.Collection<? extends Point2D> points)
Static factory for creating a new SimplePolygon2D from a collection of points.

Since:
0.8.1

create

public static SimplePolygon2D create(Point2D... points)
Static factory for creating a new SimplePolygon2D from an array of points.

Since:
0.8.1

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

getRing

public LinearRing2D getRing()
Returns the linear ring that composes the boundary of this polygon.

Since:
0.9.3

addVertex

public void addVertex(Point2D point)
Adds a point as the last vertex.

Specified by:
addVertex in interface Polygon2D
Parameters:
point - the position of the new vertex

insertVertex

public void insertVertex(int index,
                         Point2D point)
Adds a point as the last vertex.

Specified by:
insertVertex in interface Polygon2D
Parameters:
index - index at which the specified vertex is to be inserted
point - the position of the new vertex
Since:
0.9.3

setVertex

public void setVertex(int index,
                      Point2D position)
Changes the position of the i-th vertex.

Specified by:
setVertex in interface Polygon2D
Parameters:
index - the vertex index
position - the new position of the vertex

removeVertex

public boolean removeVertex(Point2D point)
Removes a vertex of the polygon.

Parameters:
point - the vertex to be removed.

removeVertex

public void removeVertex(int index)
Removes a vertex of the polygon specified by its index.

Specified by:
removeVertex in interface Polygon2D
Parameters:
index - index of the vertex to remove
Since:
0.9.3

clearVertices

public void clearVertices()
Removes all the vertices of the polygon.


closestVertexIndex

public int closestVertexIndex(Point2D point)
Computes the index of the closest vertex to the input point.

Specified by:
closestVertexIndex in interface Polygon2D

area

public double area()
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.

Specified by:
area in interface Polygon2D
Returns:
the signed area of the polygon.

centroid

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

Specified by:
centroid in interface Polygon2D
Returns:
the centroid of the polygon

vertices

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

Specified by:
vertices in interface Polygon2D

vertex

public Point2D vertex(int i)
Returns the i-th vertex of the polygon.

Specified by:
vertex in interface Polygon2D
Parameters:
i - index of the vertex, between 0 and the number of vertices

vertexNumber

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

Specified by:
vertexNumber in interface Polygon2D
Since:
0.6.3

edges

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

Specified by:
edges in interface Polygon2D

edgeNumber

public int edgeNumber()
Returns the number of edges. For a simple polygon, this equals the number of vertices.

Specified by:
edgeNumber in interface Polygon2D

transform

public CirculinearDomain2D transform(CircleInversion2D inv)
Description copied from interface: CirculinearShape2D
Transforms the shape by a circle inversion. The result is still an instance a CirculinearShape2D.

Specified by:
transform in interface CirculinearDomain2D
Specified by:
transform in interface CirculinearShape2D
Parameters:
inv - the circle inversion
Returns:
the transformed shape

buffer

public CirculinearDomain2D buffer(double dist)
Description copied from interface: CirculinearShape2D
Computes the buffer of the shape, formed by the set of points located at a distance from the shape that is lower or equal to d.

Specified by:
buffer in interface CirculinearShape2D
Parameters:
dist - the maximal distance between a point of the buffer and the shape
Returns:
the buffer of the shape

asPolygon

public Polygon2D asPolygon(int n)
Description copied from interface: Domain2D
Returns an approximation of the domain as a polygon, or a MultiPolygon.

Specified by:
asPolygon in interface Domain2D
Returns:
a polygon

boundary

public CirculinearContourArray2D<LinearRing2D> boundary()
Returns a set of one LinearRing2D, which encloses the polygon.

Specified by:
boundary in interface CirculinearDomain2D
Specified by:
boundary in interface Domain2D
Specified by:
boundary in interface Polygon2D
Returns:
the boundary of the domain

contours

public java.util.Collection<LinearRing2D> contours()
Description copied from interface: Domain2D
Returns the set of contours that enclose this domain. The result is a collection of shapes that implement the Contour2D interface.

Specified by:
contours in interface CirculinearDomain2D
Specified by:
contours in interface Domain2D
Specified by:
contours in interface Polygon2D
See Also:
Contour2D

complement

public SimplePolygon2D complement()
Returns the polygon created by reversing the order of the vertices.

Specified by:
complement in interface CirculinearDomain2D
Specified by:
complement in interface Domain2D
Specified by:
complement in interface Polygon2D
Returns:
the polygon complementary to this

distance

public double distance(Point2D p)
Returns the distance of the point to the polygon. The result is the minimal distance computed for each edge if the polygon, or ZERO if the point lies inside the polygon.

Specified by:
distance in interface Shape2D

distance

public double distance(double x,
                       double y)
Returns the distance of the point to the polygon. The result is the minimal distance computed for each edge if the polygon, or ZERO if the point lies inside the polygon.

Specified by:
distance in interface Shape2D

clip

public Polygon2D clip(Box2D box)
Returns the shape formed by the polygon clipped by the given box.

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

boundingBox

public Box2D boundingBox()
Returns the bounding box of the polygon.

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

isBounded

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

Specified by:
isBounded in interface Shape2D

isEmpty

public boolean isEmpty()
Description copied from interface: Shape2D
Returns true if the shape does not contain any point. This is the case for example for PointSet2D without any point.

Specified by:
isEmpty in interface Shape2D
Returns:
true if the shape does not contain any point.

transform

public SimplePolygon2D transform(AffineTransform2D trans)
Returns the new Polygon created by an affine transform of this polygon. If the transform is not direct, the order of vertices is reversed.

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

contains

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

Specified by:
contains in interface Shape2D

contains

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

Specified by:
contains in interface Shape2D

getGeneralPath

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


draw

public void draw(java.awt.Graphics2D g2)
Description copied from interface: Shape2D
Draws the shape on the given graphics. If the shape is empty, nothing is drawn. If the shape is unbounded, an exception is thrown.

Specified by:
draw in interface Domain2D
Specified by:
draw in interface Shape2D
Parameters:
g2 - the Graphics to draw on

fill

public void fill(java.awt.Graphics2D g)
Description copied from interface: Domain2D
Fills the interior of the domain, using the Graphics current Paint.

Specified by:
fill in interface Domain2D
Parameters:
g - the Graphics to fill on

almostEquals

public boolean almostEquals(GeometricObject2D obj,
                            double eps)
Description copied from interface: GeometricObject2D
Checks if the two objects are similar up to a given threshold value. This method can be used to compare the results of geometric computations, that introduce errors due to numerical computations.

Specified by:
almostEquals in interface GeometricObject2D
Parameters:
obj - the object to compare
eps - a threshold value, for example the minimal coordinate difference
Returns:
true if both object have the same value up to the threshold

equals

public boolean equals(java.lang.Object obj)
Tests 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

clone

public SimplePolygon2D clone()
Overrides:
clone in class java.lang.Object