Package org.djutils.draw.line
Class Polygon2d
java.lang.Object
org.djutils.draw.line.PolyLine2d
org.djutils.draw.line.Polygon2d
- All Implemented Interfaces:
Serializable
,Iterable<Point2d>
,Drawable<Point2d>
,Drawable2d
,PolyLine<PolyLine2d,
,Point2d, Ray2d, DirectedPoint2d, LineSegment2d> Project<Point2d>
Closed PolyLine2d. The actual closing point (which is the same as the starting point) is NOT included in the super
PolyLine2d. The constructors automatically remove the last point if it is a at the same location as the first point.
Copyright (c) 2020-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.djutils.draw.line.PolyLine
PolyLine.TransitionFunction
-
Field Summary
Fields inherited from interface org.djutils.draw.line.PolyLine
DEFAULT_CIRCLE_PRECISION, DEFAULT_OFFSET_FILTER_RATIO, DEFAULT_OFFSET_MAXIMUM_FILTER_VALUE, DEFAULT_OFFSET_MINIMUM_FILTER_VALUE, DEFAULT_OFFSET_PRECISION, NO_FILTER
-
Constructor Summary
ConstructorsConstructorDescriptionPolygon2d
(double[] x, double[] y) Construct a new Polygon2d.Polygon2d
(double epsilon, double[] x, double[] y) Construct a new Polygon2d.Construct a new Polygon2d from an iterator that yields Point2d.Construct a new Polygon2d from a list of Point2d objects.Construct a new Polygon2d.Construct a new Polygon2d.Construct a new Polygon2d from an iterator that yields Point2d.Construct a new Polygon2d from a list of Point2d objects.Construct a new Polygon2d from an existing one.Construct a new Polygon2d.Construct a new Polygon2d. -
Method Summary
Modifier and TypeMethodDescriptionboolean
contains
(double x, double y) Determine if a point is inside this Polygon.boolean
Determine if this Polygon completely contains a Bounds2d object.boolean
Determine if a point is inside this Polygon.double
Return the length of this line.getSegment
(int index) Extract one LineSegment of this PolyLine, or Polygon.boolean
intersects
(Polygon2d other) Determine if this Polygon2d intersects another Polygon2d.final boolean
isConvex()
Determine if this Polygon is convex.reverse()
Construct a new PolyLine with all points of this PolyLine in reverse order.double
surface()
Compute the surface of this Polygon2d.toPath2D()
Construct a Path2D from this PolyLine2d.final String
toString()
Produce a string describing the Drawable using default conversion for the (double) coordinate values.Produce a String describing the Drawable.Methods inherited from class org.djutils.draw.line.PolyLine2d
closestPointOnPolyLine, concatenate, concatenate, concatenate, equals, extract, get, getBounds, getLocation, getLocationExtended, getX, getY, hashCode, instantiate, iterator, iteratorToList, lengthAtIndex, makeArray, noiseFilteredLine, offsetLine, offsetLine, offsetLine, projectOrthogonal, projectOrthogonalExtended, projectOrthogonalFractional, projectOrthogonalFractionalExtended, projectRay, size, transitionLine, truncate
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.djutils.draw.Drawable
getPointList, toString, toString
Methods inherited from interface org.djutils.draw.Drawable2d
getDimensions
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface org.djutils.draw.line.PolyLine
extractFractional, filterNearDuplicates, find, getFirst, getLast, getLocationFraction, getLocationFraction, getLocationFractionExtended, instantiate, offsetLine, offsetLine
-
Constructor Details
-
Polygon2d
public Polygon2d(double[] x, double[] y) Construct a new Polygon2d.- Parameters:
x
- the x coordinates of the pointsy
- the y coordinates of the points- Throws:
NullPointerException
- whenx
, ory
isnull
IllegalArgumentException
- when any two successive points are equal, or when there are too few points, or when the lengths of the coordinate arrays are not equal
-
Polygon2d
public Polygon2d(double epsilon, double[] x, double[] y) Construct a new Polygon2d.- Parameters:
epsilon
- minimum distance between points to be considered different (these will not be filtered out)x
- the x coordinates of the pointsy
- the y coordinates of the points- Throws:
NullPointerException
- whenx
, ory
isnull
IllegalArgumentException
- when any two successive points are equal, or when there are too few points, or when the lengths of the coordinate arrays are not equal
-
Polygon2d
Construct a new Polygon2d.- Parameters:
points
- array of Point2d objects.- Throws:
NullPointerException
- whenpoints
isnull
IllegalArgumentException
- whenpoints
is too short, or contains successive duplicate points
-
Polygon2d
Construct a new Polygon2d.- Parameters:
epsilon
- minimum distance between points to be considered different (these will not be filtered out)points
- array of Point2d objects.- Throws:
NullPointerException
- whenpoints
isnull
IllegalArgumentException
- whenpoints
is too short, or contains successive duplicate points
-
Polygon2d
Construct a new Polygon2d.- Parameters:
point1
- the first point of the new Polygon2dpoint2
- the second point of the new Polygon2dotherPoints
- all remaining points of the new Polygon2d (may benull
)- Throws:
NullPointerException
- whenpoint1
orpoint2
isnull
, orotherPoints
contains anull
valueIllegalArgumentException
- whenpoint1
is equal to the last entry ofotherPoints
, or any two successive points are equal
-
Polygon2d
Construct a new Polygon2d.- Parameters:
epsilon
- minimum distance between points to be considered different (these will not be filtered out)point1
- the first point of the new Polygon2dpoint2
- the second point of the new Polygon2dotherPoints
- all remaining points of the new Polygon2d (may benull
)- Throws:
NullPointerException
- whenpoint1
orpoint2
isnull
, orotherPoints
contains anull
valueIllegalArgumentException
- whenpoint1
is equal to the last entry ofotherPoints
, or any two successive points are equal
-
Polygon2d
Construct a new Polygon2d from a list of Point2d objects.- Parameters:
points
- the list of points- Throws:
NullPointerException
- whenpoints
isnull
, or contains anull
valueIllegalArgumentException
- whenpoints
is too short, or the last two points are at the same location
-
Polygon2d
Construct a new Polygon2d from a list of Point2d objects.- Parameters:
epsilon
- minimum distance between points to be considered different (these will not be filtered out)points
- the list of points- Throws:
NullPointerException
- whenpoints
isnull
, or contains anull
valueIllegalArgumentException
- whenpoints
is too short, or the last two points are at the same location
-
Polygon2d
Construct a new Polygon2d from an iterator that yields Point2d.- Parameters:
iterator
- the iterator- Throws:
NullPointerException
- wheniterator
isnull
, or the iterator returns anull
valueIllegalArgumentException
- when theiterator
yields too few points, or the before last and last point have the same coordinates
-
Polygon2d
Construct a new Polygon2d from an iterator that yields Point2d.- Parameters:
epsilon
- minimum distance between points to be considered different (these will not be filtered out)iterator
- the iterator- Throws:
NullPointerException
- wheniterator
isnull
, or the iterator returns anull
valueIllegalArgumentException
- when theiterator
yields too few points, or the before last and last point have the same coordinates
-
Polygon2d
Construct a new Polygon2d from an existing one. This constructor is primarily intended for use in extending classes.- Parameters:
polygon
- the existing Polygon2d- Throws:
NullPointerException
- whenpolygon
isnull
-
-
Method Details
-
isConvex
public final boolean isConvex()Determine if this Polygon is convex. Returns bogus result for self-intersecting polygons. Derived from Convex by Paul Bourke- Returns:
true
if thisPolygon2d
is convex;false
if thisPolygon2d
is concave
-
contains
Determine if a point is inside this Polygon. Returns bogus results for self-intersecting polygons.- Parameters:
point
- the point- Returns:
true
if the point is inside thisPolygon2d
,false
if the point is outside thisPolygon2d
. Results are ill-defined for points on the edges of thisPolygon2d
.
-
contains
public boolean contains(double x, double y) Determine if a point is inside this Polygon. Returns bogus results for self-intersecting polygons. Derived from Polygons and meshes by Paul Bourke- Parameters:
x
- the x-coordinate of the pointy
- the y-coordinate of the point- Returns:
true
if the point is inside thisPolygon2d
,false
if the point is outside thisPolygon2d
. Results are ill-defined for points on the edges of thisPolygon2d
.
-
contains
Determine if this Polygon completely contains a Bounds2d object. If this Polygon self-intersects, the results is bogus.- Parameters:
bounds
- the Bounds2d object- Returns:
true
if theBounds2d
object is completely contained in thisPolygon2d
;false
if any part (or all) of the Bounds2d object is outside thisPolygon2d
. If the Bounds2d object touches thisPolygon2d
the results are ill-defined.
-
intersects
Determine if this Polygon2d intersects another Polygon2d.- Parameters:
other
- the other Polygon2d- Returns:
true
if the polygons intersect;false
if the polygons are disjunct. Ill-defined if the polygons touch.
-
surface
public double surface()Compute the surface of this Polygon2d. Sign of the result reflects the winding-ness of this this Polygon2d. If this Polygon2d self-intersects, the result is bogus.- Returns:
- the surface of this
Polygon2d
-
getLength
public double getLength()Description copied from interface:PolyLine
Return the length of this line. This is not the number of points; it is the sum of the lengths of the segments.- Specified by:
getLength
in interfacePolyLine<PolyLine2d,
Point2d, Ray2d, DirectedPoint2d, LineSegment2d> - Overrides:
getLength
in classPolyLine2d
- Returns:
- the length of this line
-
getSegment
Description copied from interface:PolyLine
Extract one LineSegment of this PolyLine, or Polygon.- Specified by:
getSegment
in interfacePolyLine<PolyLine2d,
Point2d, Ray2d, DirectedPoint2d, LineSegment2d> - Overrides:
getSegment
in classPolyLine2d
- Parameters:
index
- the rank number of the segment; must be in range 0..Size() - 2 for PolyLine, or 0.. Size() - 1 for Polygon.- Returns:
- the LineSegment that connects point index to point index + 1
-
reverse
Description copied from interface:PolyLine
Construct a new PolyLine with all points of this PolyLine in reverse order.- Returns:
- the new
PolyLine
-
toPath2D
Construct a Path2D from this PolyLine2d. The result is NOT cached (in the current implementation).- Overrides:
toPath2D
in classPolyLine2d
- Returns:
- newly construct Path2D consisting solely of straight segments.
-
toString
Description copied from interface:Drawable
Produce a string describing the Drawable using default conversion for the (double) coordinate values. Regrettably, it is not allowed to provide a default implementation here.- Specified by:
toString
in interfaceDrawable<Point2d>
- Overrides:
toString
in classPolyLine2d
- Returns:
- a string describing the Drawable
-
toString
Description copied from interface:Drawable
Produce a String describing the Drawable.- Specified by:
toString
in interfaceDrawable<Point2d>
- Overrides:
toString
in classPolyLine2d
- Parameters:
doubleFormat
- a format string (something like "%6.3f") which will be used to render every coordinate value)doNotIncludeClassName
- iftrue
; the output oftoString
is not prefixed by the class name. This is useful for concatenating the textual representation of lots of Drawables (e.g. an array, or a List).- Returns:
- textual representation of the Drawable
-