math.geom2d.polygon
Class SimplePolygon2D
- java.lang.Object
-
- 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.
-
-
Constructor Summary
Constructors Constructor and Description 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(int nVertices)Ensure the polygon has enough memory for storing the required number of vertices.SimplePolygon2D(LinearRing2D ring)Creates a simple polygon with the given linear ring representing its boundary.SimplePolygon2D(Point2D... vertices)Constructor from an array of pointsSimplePolygon2D(SimplePolygon2D poly)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description voidaddVertex(Point2D point)Adds a point as the last vertex.booleanalmostEquals(GeometricObject2D obj, double eps)Checks if the two objects are similar up to a given threshold value.doublearea()Computes the signed area of the polygon.Polygon2DasPolygon(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.Box2DboundingBox()Returns the bounding box of the polygon.CirculinearDomain2Dbuffer(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.Point2Dcentroid()Computes the centroid (center of mass) of the polygon.voidclearVertices()Removes all the vertices of the polygon.Polygon2Dclip(Box2D box)Returns the shape formed by the polygon clipped by the given box.SimplePolygon2Dclone()Deprecated.use copy constructor instead (0.11.2)intclosestVertexIndex(Point2D point)Computes the index of the closest vertex to the input point.SimplePolygon2Dcomplement()Returns the polygon created by reversing the order of the vertices.booleancontains(double x, double y)Returns true if the point (x, y) lies inside the polygon, with precision given by Shape2D.ACCURACY.booleancontains(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 SimplePolygon2Dcreate(java.util.Collection<? extends Point2D> points)Static factory for creating a new SimplePolygon2D from a collection of points.static SimplePolygon2Dcreate(Point2D... points)Static factory for creating a new SimplePolygon2D from an array of points.doubledistance(double x, double y)Returns the distance of the point to the polygon.doubledistance(Point2D p)Returns the distance of the point to the polygon.voiddraw(java.awt.Graphics2D g2)Draws the shape on the given graphics.intedgeNumber()Returns the number of edges.java.util.Collection<LineSegment2D>edges()Returns the set of edges, as a collection of LineSegment2D.booleanequals(java.lang.Object obj)Tests if the two polygons are equal.voidfill(java.awt.Graphics2D g)Fills the interior of the domain, using the Graphics current Paint.java.awt.geom.GeneralPathgetGeneralPath()Returns a general path iterator.LinearRing2DgetRing()Returns the linear ring that composes the boundary of this polygon.intgetWindingNumber(double x, double y)Computes the winding number of the polygon.voidinsertVertex(int index, Point2D point)Adds a point as the last vertex.booleanisBounded()Returns true if polygon is oriented counter-clockwise, false otherwise.booleanisEmpty()Returns true if the shape does not contain any point.voidremoveVertex(int index)Removes a vertex of the polygon specified by its index.booleanremoveVertex(Point2D point)Removes a vertex of the polygon.voidsetVertex(int index, Point2D position)Changes the position of the i-th vertex.SimplePolygon2Dsimplify(double distMax)Returns a simplified version of this polygon, by using Douglas-Peucker algorithm.SimplePolygon2Dtransform(AffineTransform2D trans)Returns the new Polygon created by an affine transform of this polygon.CirculinearDomain2Dtransform(CircleInversion2D inv)Transforms the shape by a circle inversion.Point2Dvertex(int i)Returns the i-th vertex of the polygon.intvertexNumber()Returns the number of vertices of the polygon.java.util.Collection<Point2D>vertices()Returns the points of the polygon.
-
-
-
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 vertexycoords- the y coordinate of each vertex
-
SimplePolygon2D
public SimplePolygon2D(java.util.Collection<? extends Point2D> points)
-
SimplePolygon2D
public SimplePolygon2D(int nVertices)
Ensure the polygon has enough memory for storing the required number of vertices.
-
SimplePolygon2D
public SimplePolygon2D(LinearRing2D ring)
Creates a simple polygon with the given linear ring representing its boundary.- Parameters:
ring- the boundary of the polygon
-
SimplePolygon2D
public SimplePolygon2D(SimplePolygon2D poly)
-
-
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 pointy- the y-coordinate of the point- Returns:
- the number of windings of the curve around the point
-
simplify
public SimplePolygon2D simplify(double distMax)
Returns a simplified version of this polygon, by using Douglas-Peucker algorithm.
-
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.
-
insertVertex
public void insertVertex(int index, Point2D point)Adds a point as the last vertex.- Specified by:
insertVertexin interfacePolygon2D- Parameters:
index- index at which the specified vertex is to be insertedpoint- 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.
-
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:
removeVertexin interfacePolygon2D- 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:
closestVertexIndexin interfacePolygon2D
-
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.
-
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/.
-
vertices
public java.util.Collection<Point2D> vertices()
Returns the points of the polygon. The result is a pointer to the inner collection of vertices.
-
vertex
public Point2D vertex(int i)
Returns the i-th vertex of the polygon.
-
vertexNumber
public int vertexNumber()
Returns the number of vertices of the polygon.- Specified by:
vertexNumberin interfacePolygon2D- Since:
- 0.6.3
-
edges
public java.util.Collection<LineSegment2D> edges()
Returns the set of edges, as a collection of LineSegment2D.
-
edgeNumber
public int edgeNumber()
Returns the number of edges. For a simple polygon, this equals the number of vertices.- Specified by:
edgeNumberin interfacePolygon2D
-
transform
public CirculinearDomain2D transform(CircleInversion2D inv)
Description copied from interface:CirculinearShape2DTransforms the shape by a circle inversion. The result is still an instance a CirculinearShape2D.- Specified by:
transformin interfaceCirculinearDomain2D- Specified by:
transformin interfaceCirculinearShape2D- Parameters:
inv- the circle inversion- Returns:
- the transformed shape
-
buffer
public CirculinearDomain2D buffer(double dist)
Description copied from interface:CirculinearShape2DComputes 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:
bufferin interfaceCirculinearShape2D- 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:Domain2DReturns an approximation of the domain as a polygon, or a MultiPolygon.
-
boundary
public CirculinearContourArray2D<LinearRing2D> boundary()
Returns a set of one LinearRing2D, which encloses the polygon.
-
contours
public java.util.Collection<LinearRing2D> contours()
Description copied from interface:Domain2DReturns the set of contours that enclose this domain. The result is a collection of shapes that implement the Contour2D interface.
-
complement
public SimplePolygon2D complement()
Returns the polygon created by reversing the order of the vertices.- Specified by:
complementin interfaceCirculinearDomain2D- Specified by:
complementin interfaceDomain2D- Specified by:
complementin interfacePolygon2D- 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.
-
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.
-
clip
public Polygon2D clip(Box2D box)
Returns the shape formed by the polygon clipped by the given box.
-
boundingBox
public Box2D boundingBox()
Returns the bounding box of the polygon.- Specified by:
boundingBoxin interfaceShape2D- Returns:
- the bounding box of the shape.
-
isBounded
public boolean isBounded()
Returns true if polygon is oriented counter-clockwise, false otherwise.
-
isEmpty
public boolean isEmpty()
Description copied from interface:Shape2DReturns true if the shape does not contain any point. This is the case for example for PointSet2D without 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.
-
contains
public boolean contains(Point2D p)
Returns true if the point p lies inside the polygon, with precision given by Shape2D.ACCURACY.
-
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.
-
getGeneralPath
public java.awt.geom.GeneralPath getGeneralPath()
Returns a general path iterator.
-
draw
public void draw(java.awt.Graphics2D g2)
Description copied from interface:Shape2DDraws the shape on the given graphics. If the shape is empty, nothing is drawn. If the shape is unbounded, an exception is thrown.
-
fill
public void fill(java.awt.Graphics2D g)
Description copied from interface:Domain2DFills the interior of the domain, using the Graphics current Paint.
-
almostEquals
public boolean almostEquals(GeometricObject2D obj, double eps)
Description copied from interface:GeometricObject2DChecks 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:
almostEqualsin interfaceGeometricObject2D- Parameters:
obj- the object to compareeps- 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:
equalsin classjava.lang.Object
-
clone
@Deprecated public SimplePolygon2D clone()
Deprecated. use copy constructor instead (0.11.2)- Overrides:
clonein classjava.lang.Object
-
-
DMelt 3.0 © DataMelt by jWork.ORG