Package inra.ijpb.geometry
Class Polygon2D
java.lang.Object
inra.ijpb.geometry.Polygon2D
- All Implemented Interfaces:
java.lang.Iterable<java.awt.geom.Point2D>
public class Polygon2D
extends java.lang.Object
implements java.lang.Iterable<java.awt.geom.Point2D>
A polygon shape in the plane.
Polygon is assumed to be simple: only one connected component, no hole.
- Author:
- dlegland
-
Constructor Summary
Constructors Constructor Description Polygon2D()
Creates a new empty polygon.Polygon2D(double[] xCoords, double[] yCoords)
Creates a new polygon from the coordinates of its vertices.Polygon2D(int n)
Creates a new empty polygon by allocating enough space for storing vertex coordinates.Polygon2D(java.util.Collection<java.awt.geom.Point2D> vertices)
Creates a new polygon from a collection of vertices. -
Method Summary
Modifier and Type Method Description int
addVertex(java.awt.geom.Point2D position)
Adds a new vertex in this polygondouble
area()
Computes the area of this polygonBox2D
boundingBox()
Returns the bounding box of this polygon.java.awt.geom.Point2D
centroid()
Computes the centroid of this polygonboolean
contains(java.awt.geom.Point2D point)
Checks if this polygon contains the query point.ij.gui.PolygonRoi
createRoi()
Converts this polygon into an ImageJ Polygon ROI.java.awt.geom.Point2D
getVertex(int index)
Returns index at the specific indexPolygon2D
invert()
Computes the complementary polygon, whose interior is the exterior of this polygon.java.util.Iterator<java.awt.geom.Point2D>
iterator()
void
setVertex(int i, java.awt.geom.Point2D pos)
Changes vertex coordinate at the specified indexdouble
signedArea()
Computes the signed area of this polygonint
vertexNumber()
Returns the number of vertices within this polygonjava.util.ArrayList<java.awt.geom.Point2D>
vertices()
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Polygon2D
public Polygon2D()Creates a new empty polygon. -
Polygon2D
public Polygon2D(int n)Creates a new empty polygon by allocating enough space for storing vertex coordinates.- Parameters:
n
- then number of vertices
-
Polygon2D
public Polygon2D(double[] xCoords, double[] yCoords)Creates a new polygon from the coordinates of its vertices.- Parameters:
xCoords
- the x-coordinates of the verticesyCoords
- the y-coordinates of the vertices
-
Polygon2D
public Polygon2D(java.util.Collection<java.awt.geom.Point2D> vertices)Creates a new polygon from a collection of vertices. A new collection is created.- Parameters:
vertices
- the polygon vertices
-
-
Method Details
-
boundingBox
Returns the bounding box of this polygon.- Returns:
- the bounding box of this polygon.
-
area
public double area()Computes the area of this polygon- Returns:
- the area of this polygon
- See Also:
signedArea()
-
signedArea
public double signedArea()Computes the signed area of this polygon- Returns:
- the signed area of this polygon
- See Also:
area()
-
centroid
public java.awt.geom.Point2D centroid()Computes the centroid of this polygon- Returns:
- the centroid position of the polygon.
-
contains
public boolean contains(java.awt.geom.Point2D point)Checks if this polygon contains the query point. Uses winding number algorithm.- Parameters:
point
- the polygon- Returns:
- true if the polygon contains the query point
-
invert
Computes the complementary polygon, whose interior is the exterior of this polygon. Keeps the same vertex as initial vertex.- Returns:
- the inverted polygon
-
createRoi
public ij.gui.PolygonRoi createRoi()Converts this polygon into an ImageJ Polygon ROI.- Returns:
- the corresponding PolygonRoi
-
vertexNumber
public int vertexNumber()Returns the number of vertices within this polygon- Returns:
- the number of vertices within this polygon
-
addVertex
public int addVertex(java.awt.geom.Point2D position)Adds a new vertex in this polygon- Parameters:
position
- the position of the new vertex- Returns:
- the index of the newly created vertex
-
getVertex
public java.awt.geom.Point2D getVertex(int index)Returns index at the specific index- Parameters:
index
- vertex index- Returns:
- the vertex at the specified index
-
setVertex
public void setVertex(int i, java.awt.geom.Point2D pos)Changes vertex coordinate at the specified index- Parameters:
i
- vertex indexpos
- the position of the new vertex
-
vertices
public java.util.ArrayList<java.awt.geom.Point2D> vertices()- Returns:
- a reference to the inner array of vertices
-
iterator
public java.util.Iterator<java.awt.geom.Point2D> iterator()- Specified by:
iterator
in interfacejava.lang.Iterable<java.awt.geom.Point2D>
- Returns:
- an iterator over the vertices of this polygon
-