public class Polygon extends PointList implements Shape
PointList
,
so the vertices are the underlying PointList.points
, and they are
considered to be joined in order.Constructor and Description |
---|
Polygon()
Constructs an empty polygon to which vertices may be added.
|
Polygon(boolean representsHole)
Constructs an empty polygon to which vertices may be added.
|
Polygon(Collection<? extends Point2d> vertices)
Construct a Polygon from vertices
|
Polygon(Collection<? extends Point2d> vertices,
boolean copy)
Construct a Polygon from the vertices, possibly copying the vertices
first
|
Polygon(Point2d... vertices)
Construct a Polygon from vertices
|
Modifier and Type | Method and Description |
---|---|
void |
addInnerPolygon(Polygon p)
Add an inner polygon to this polygon.
|
void |
addInnerPolygon(Polygon p,
boolean inferOuter)
Add an inner polygon to this polygon.
|
void |
addVertex(float x,
float y)
Add a vertex to the polygon
|
void |
addVertex(Point2d pt)
Add a vertex to the polygon
|
Polygon |
asPolygon()
Convert the shape to a polygon representation
|
double |
calculateArea()
Calculate the area of the shape
|
Point2d |
calculateCentroid()
Calculate the centroid of the polygon.
|
Polygon |
calculateConvexHullMelkman()
Calculate convex hull using Melkman's algorithm.
|
double |
calculateDirection()
Calculates the principle direction of the connected component.
|
double[] |
calculateFirstMoment()
Treating the polygon as a continuous piecewise function, calculate
exactly the first moment.
|
double |
calculatePerimeter()
Calculate the perimeter of the shape
|
Rectangle |
calculateRegularBoundingBox()
Compute the regular (oriented to the axes) bounding box of the polygon.
|
double[] |
calculateSecondMoment()
Treating the polygon as a continuous piecewise function, calculate
exactly the second moment.
|
double[] |
calculateSecondMomentCentralised()
Treating the polygon as a continuous piecewise function, calculate
exactly the centralised second moment.
|
double |
calculateSignedArea()
Calculate the area of the polygon.
|
Polygon |
clone() |
void |
close()
Close the polygon if it's not already closed
|
Point2d |
closestPoint(Point2d pt)
Find the closest point on the polygon to the given point
|
Polygon |
difference(Polygon p)
Calculates the difference between two polygons and returns a new polygon.
|
boolean |
equals(Object obj) |
boolean |
equals(Polygon p)
Specific equals method for polygons where the polgyons are tested for the
vertices being in the same order.
|
Polygon |
getInnerPoly(int index)
Get the inner polygon at the given index.
|
List<Polygon> |
getInnerPolys()
Returns the list of inner polygons.
|
int |
getNumInnerPoly()
Returns the number of inner polygons in this polygon including this
polygon.
|
List<Point2d> |
getVertices()
Get the vertices of the polygon
|
int |
hashCode() |
boolean |
hasNoInnerPolygons()
Test if this has no inner polygons or sub-parts
|
Polygon |
intersect(Polygon p2)
Returns the intersection of this polygon and the given polygon.
|
double |
intersectionArea(Shape that)
Calls
intersectionArea(Shape, int) with 1 step per pixel
dimension. |
double |
intersectionArea(Shape that,
int nStepsPerDimension)
Return an estimate for the area of the intersection of this polygon and
another polygon.
|
boolean |
isClockwise()
Test (outer) polygon path direction
|
boolean |
isClosed()
Is the polygon closed (i.e.
|
boolean |
isConvex()
Test if the outer polygon is convex.
|
boolean |
isEmpty()
Return whether this polygon has no vertices or not.
|
boolean |
isHole()
Returns whether this polygon is representing a hole in another polygon.
|
boolean |
isInside(Point2d point)
Test whether the point p is inside the polygon using the winding rule
algorithm.
|
RotatedRectangle |
minimumBoundingRectangle()
Compute the minimum size rotated bounding rectangle that contains this
shape using the rotating calipers approach.
|
RotatedRectangle |
minimumBoundingRectangle(boolean assumeSimple)
Compute the minimum size rotated bounding rectangle that contains this
shape using the rotating calipers approach.
|
int |
nVertices()
Get the number of vertices
|
void |
open()
Open the polygon if it's closed
|
Polygon |
reduceVertices(double eps)
Reduce the number of vertices in a polygon.
|
Polygon |
roundVertices()
Iterates through the vertices and rounds all vertices to round integers.
|
void |
scale(float sc)
Scale the polygon by the given amount about (0,0).
|
void |
scale(Point2d centre,
float sc)
Scale the polygon by the given amount about the given point.
|
Polygon |
scaleX(float sc)
Scale the polygon only in the x-direction by the given amount about
(0,0).
|
Polygon |
scaleXY(float scx,
float scy)
Scale the polygon by the given amount about (0,0).
|
Polygon |
scaleY(float sc)
Scale the polygon only in the y-direction by the given amount about
(0,0).
|
void |
setIsHole(boolean isHole)
Set whether this polygon represents a hole in another polygon.
|
Ellipse |
toEllipse()
Using
EllipseUtilities.ellipseFromCovariance(float, float, Matrix, float)
and the calculateSecondMomentCentralised() return the Ellipse
best fitting the shape of this polygon. |
String |
toString()
Displays the complete list of vertices unless the number of vertices is
greater than 10 - then a sublist is shown of 5 from the start and 5 from
the end separated by ellipses.
|
Polygon |
transform(Jama.Matrix transform)
Apply a 3x3 transform matrix to a copy of the polygon and return it
|
void |
translate(float x,
float y)
Translate the polygons position
|
Polygon |
union(Polygon p2)
Returns the union of this polygon and the given polygon.
|
Polygon |
xor(Polygon p2)
Returns the XOR of this polygon and the given polygon.
|
calculateConvexHull, computeIntrinsicScale, computeMean, get, getHeight, getLines, getLines, getWidth, iterator, maxX, maxY, minX, minY, rotate, rotate, scaleCentroid, size
finalize, getClass, notify, notifyAll, wait, wait, wait
getHeight, getWidth, maxX, maxY, minX, minY, scaleCentroid
forEach, spliterator
public Polygon()
public Polygon(boolean representsHole)
representsHole
- Whether the polygon will represent a hole.public Polygon(Point2d... vertices)
vertices
- the verticespublic Polygon(Collection<? extends Point2d> vertices)
vertices
- the verticespublic Polygon(Collection<? extends Point2d> vertices, boolean copy)
vertices
- the verticescopy
- should the vertices be copiedpublic List<Point2d> getVertices()
public int nVertices()
public boolean isClosed()
public void close()
public void open()
public boolean isInside(Point2d point)
public Polygon difference(Polygon p)
p
- the polygon to subtract.public double calculateArea()
Shape
calculateArea
in interface Shape
public double calculateSignedArea()
public double intersectionArea(Shape that)
intersectionArea(Shape, int)
with 1 step per pixel
dimension. Subsequently this function returns the shared whole pixels of
this polygon and that.intersectionArea
in interface Shape
that
- public double intersectionArea(Shape that, int nStepsPerDimension)
intersectionArea
in interface Shape
that
- public Polygon asPolygon()
asPolygon
in interface Shape
Shape.asPolygon()
public void addVertex(float x, float y)
x
- x-coordinate of the vertexy
- y-coordinate of the vertexpublic void addVertex(Point2d pt)
pt
- coordinate of the vertexpublic Polygon roundVertices()
public boolean isEmpty()
public int getNumInnerPoly()
public Polygon getInnerPoly(int index)
index
- the index of the polygon to getpublic void addInnerPolygon(Polygon p, boolean inferOuter)
inferOuter
boolean is true. If this
variable is false, the inner polygon will be added to the list of inner
polygons for this polygon regardless of whether a main polygon exists.
When the main polygon is inferred from the given polygon, the vertices
are copied into this polygon's vertex list.p
- The inner polygon to addinferOuter
- Whether to infer the outer polygon from this inner onepublic void addInnerPolygon(Polygon p)
p
- The inner polygon to addpublic List<Polygon> getInnerPolys()
public void setIsHole(boolean isHole)
isHole
- Whether this polygon is a whole.public boolean isHole()
public boolean equals(Object obj)
equals
in class Object
Object.equals(java.lang.Object)
public boolean equals(Polygon p)
p
- The polygon to test againstpublic int hashCode()
hashCode
in class Object
Object.hashCode()
public String toString()
toString
in class PointList
Object.toString()
public Polygon intersect(Polygon p2)
p2
- The polygon to intersect with.public Polygon union(Polygon p2)
p2
- The polygon to union with.public Polygon xor(Polygon p2)
p2
- The polygon to XOR with.public Polygon reduceVertices(double eps)
eps
- distance value below which a vertex can be ignoredpublic Polygon transform(Jama.Matrix transform)
public Rectangle calculateRegularBoundingBox()
calculateRegularBoundingBox
in interface GeometricObject2d
calculateRegularBoundingBox
in class PointList
public void translate(float x, float y)
translate
in interface GeometricObject2d
translate
in class PointList
x
- x-translationy
- y-translationpublic void scale(float sc)
scale
in interface GeometricObject2d
scale
in class PointList
sc
- the scale factor.public Polygon scaleX(float sc)
public Polygon scaleY(float sc)
public Polygon scaleXY(float scx, float scy)
public void scale(Point2d centre, float sc)
scale
in interface GeometricObject2d
scale
in class PointList
centre
- the centre of the scaling operationsc
- the scale factorpublic Point2d calculateCentroid()
calculateCentroid
in interface GeometricObject2d
calculateCentroid
in class PointList
calculateFirstMoment()
;@Reference(author="Carsten Steger", title="On the Calculation of Moments of Polygons", type=Techreport, month="August", year="1996", url="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.29.8765&rep=rep1&type=pdf") public double[] calculateFirstMoment()
@Reference(author="Carsten Steger", title="On the Calculation of Moments of Polygons", type=Techreport, month="August", year="1996", url="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.29.8765&rep=rep1&type=pdf") public double[] calculateSecondMoment()
@Reference(author="Carsten Steger", title="On the Calculation of Moments of Polygons", type=Techreport, month="August", year="1996", url="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.29.8765&rep=rep1&type=pdf") public double[] calculateSecondMomentCentralised()
public double calculateDirection()
0.5 * atan( (M20-M02) / 2 * M11 )
so results in an angle between -PI and +PI.public Ellipse toEllipse()
EllipseUtilities.ellipseFromCovariance(float, float, Matrix, float)
and the calculateSecondMomentCentralised()
return the Ellipse
best fitting the shape of this polygon.Ellipse
defining the shape of the polygonpublic boolean isConvex()
public boolean hasNoInnerPolygons()
getInnerPolys()
public double calculatePerimeter()
Shape
calculatePerimeter
in interface Shape
public boolean isClockwise()
public Polygon calculateConvexHullMelkman()
PointList.calculateConvexHull()
method, but will only work for simple
polygons (those that don't self-intersect)
Based on http://softsurfer.com/Archive/algorithm_0203/algorithm_0203.htm, but modified (fixed) to deal with the problem of the initialisation points potentially being co-linear.
Copyright 2001, softSurfer (www.softsurfer.com) This code may be freely used and modified for any purpose providing that this copyright notice is included with it. SoftSurfer makes no warranty for this code, and cannot be held liable for any real or imagined damage resulting from its use. Users of this code must verify correctness for their application.
public RotatedRectangle minimumBoundingRectangle()
minimumBoundingRectangle
in interface Shape
Shape.minimumBoundingRectangle()
,
RotatingCalipers.getMinimumBoundingRectangle(Polygon, boolean)
public RotatedRectangle minimumBoundingRectangle(boolean assumeSimple)
assumeSimple
- can the algorithm assume the polygon is simple and use an
optimised (Melkman's) convex hull?RotatingCalipers.getMinimumBoundingRectangle(Polygon, boolean)
public Point2d closestPoint(Point2d pt)
pt
- the point