math.geom2d.polygon
Class SimplePolygon2D

Package class diagram package SimplePolygon2D
java.lang.Object
  extended by math.geom2d.polygon.SimplePolygon2D
All Implemented Interfaces:
Serializable, CirculinearDomain2D, CirculinearShape2D, Domain2D, Polygon2D, Shape2D

public class SimplePolygon2D
extends Object
implements Polygon2D

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

See Also:
Serialized Form

Field Summary
protected  ArrayList<Point2D> points
          The inner ordered list of vertices.
 
Fields inherited from interface math.geom2d.Shape2D
ACCURACY
 
Constructor Summary
SimplePolygon2D()
          Empty constructor: no vertex.
SimplePolygon2D(Collection<? extends Point2D> points)
           
SimplePolygon2D(double[] xcoords, double[] ycoords)
          Constructor from two arrays, one for each coordinate.
SimplePolygon2D(Point2D[] tab)
          Constructor from an array of points
 
Method Summary
 void addPoint(Point2D point)
          Deprecated. replaced by addVertex() (0.7.1)
 void addVertex(Point2D point)
          Adds a point as the last vertex.
 void clearPoints()
          Deprecated. use clearVertices instead
 void clearVertices()
          Removes all the vertices of the polygon.
 Domain2D clip(Box2D box)
          Returns the shape formed by the polygon clipped by the given box.
 SimplePolygon2D clone()
           
 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)
          Return true if the point p lies inside the polygon, with precision given by Shape2D.ACCURACY.
static SimplePolygon2D create(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.
 void draw(Graphics2D g2)
          Draw the shape on the given graphics.
 boolean equals(Object obj)
          Tests if the two polygons are equal.
 void fill(Graphics2D g)
          Fills the interior of the domain, using the Graphics current Paint.
 double getArea()
          Computes area of the polygon, by returning the absolute value of the signed area.
 CirculinearBoundarySet2D<LinearRing2D> getBoundary()
          Returns a set of one LinearRing2D, which encloses the polygon.
 Box2D getBoundingBox()
          Returns the bounding box of the polygon.
 CirculinearDomain2D getBuffer(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 getCentroid()
          Computes the centroid (center of mass) of the polygon.
 double getDistance(double x, double y)
          Returns the distance of the point to the polygon.
 double getDistance(Point2D p)
          Returns the distance of the point to the polygon.
 int getEdgeNumber()
          Returns the number of edges.
 Collection<LineSegment2D> getEdges()
          Returns the set of edges, as a collection of LineSegment2D.
 GeneralPath getGeneralPath()
          Returns a general path iterator.
 Collection<LinearRing2D> getRings()
          Returns the set of rings comprising the boundary of this polygon.
 double getSignedArea()
          Computes the signed area of the polygon.
 double getSignedDistance(double x, double y)
          Returns 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(Point2D p)
          Returns 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.
 Point2D getVertex(int i)
          Returns the i-th vertex of the polygon.
 int getVertexNumber()
          Returns the number of vertices of the polygon.
 Collection<Point2D> getVertices()
          Returns the points of the polygon.
 int getWindingNumber(double x, double y)
          Computes the winding number of the polygon.
 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 removePoint(Point2D point)
          Deprecated. replaced by removeVertex() (0.7.1)
 void 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.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

points

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

Constructor Detail

SimplePolygon2D

public SimplePolygon2D()
Empty constructor: no vertex.


SimplePolygon2D

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

Parameters:
tab - 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(Collection<? extends Point2D> points)
Method Detail

create

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

addVertex

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


removeVertex

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

Parameters:
point - the vertex to be removed.

addPoint

@Deprecated
public void addPoint(Point2D point)
Deprecated. replaced by addVertex() (0.7.1)

Adds a point as the last vertex.


removePoint

@Deprecated
public void removePoint(Point2D point)
Deprecated. replaced by removeVertex() (0.7.1)

Removes 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

clearPoints

@Deprecated
public void clearPoints()
Deprecated. use clearVertices instead

Removes all the vertices of the polygon.


clearVertices

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


setVertex

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


getVertices

public 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 Polygon2D

getVertex

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

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

getVertexNumber

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

Specified by:
getVertexNumber in interface Polygon2D
Since:
0.6.3

getEdges

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

Specified by:
getEdges in interface Polygon2D

getEdgeNumber

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

Specified by:
getEdgeNumber in interface Polygon2D

getRings

public Collection<LinearRing2D> getRings()
Description copied from interface: Polygon2D
Returns the set of rings comprising the boundary of this polygon.

Specified by:
getRings in interface Polygon2D
Returns:
the set of boundary rings.

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

getBuffer

public CirculinearDomain2D getBuffer(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:
getBuffer in interface CirculinearShape2D
Parameters:
dist - the maximal distance between a point of the buffer and the shape
Returns:
the buffer of the shape

getBoundary

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

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

complement

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

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

getDistance

public double getDistance(Point2D p)
Returns 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)
Returns 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(Point2D p)
Returns 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)
Returns 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.


clip

public Domain2D 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 Shape2D
Parameters:
box - the clipping box
Returns:
the clipped shape

getBoundingBox

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

Specified by:
getBoundingBox 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)
Return 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 GeneralPath getGeneralPath()
Returns a general path iterator.


draw

public void draw(Graphics2D g2)
Description copied from interface: Shape2D
Draw 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(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

equals

public boolean equals(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 Object

clone

public SimplePolygon2D clone()
Overrides:
clone in class Object


Copyright © 2013 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.