Package org.djutils.draw.line
Class Polygon2d
- java.lang.Object
-
- org.djutils.draw.line.PolyLine2d
-
- org.djutils.draw.line.Polygon2d
-
- All Implemented Interfaces:
Serializable
,Drawable<Point2d>
,Drawable2d
,PolyLine<PolyLine2d,Point2d,Ray2d,LineSegment2d>
,Project<Point2d>
public class Polygon2d extends PolyLine2d
Polygon2d.java. 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-2023 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
- See Also:
- Serialized Form
-
-
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
-
-
Constructor Summary
Constructors Constructor Description Polygon2d(boolean filterDuplicates, List<Point2d> pointList)
Create a new Polygon2d, optionally filtering out repeating successive points.Polygon2d(boolean filterDuplicates, Point2d... points)
Create a new Polygon2d, optionally filtering out repeating successive points.Polygon2d(double[] x, double[] y)
Construct a new Polygon2d.Polygon2d(Iterator<Point2d> iterator)
Construct a new Polygon2d from an iterator that yields Point2d.Polygon2d(List<Point2d> points)
Construct a new Polygon2d from a list of Point2d objects.Polygon2d(Point2d[] points)
Construct a new Polygon2d.Polygon2d(Point2d point1, Point2d point2, Point2d... otherPoints)
Construct a new Polygon2d.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(double x, double y)
Determine if a point is inside this Polygon.boolean
contains(Bounds2d bounds)
Determine if this Polygon completely contains a Bounds2d object.boolean
contains(Point2d point)
Determine if a point is inside this Polygon.double
getLength()
Return the length of this line.LineSegment2d
getSegment(int index)
Extract one LineSegment of this PolyLine, or Polygon.boolean
intersects(Polygon2d other)
Determine if this Polygon intersects another Polygon.boolean
isConvex()
Determine if this Polygon is convex.Polygon2d
reverse()
Construct a new PolyLine with all points of this PolyLine in reverse order.double
surface()
Compute the surface of this Polygon2d.String
toExcel()
Convert this PolyLine to something that MS-Excel can plot.Path2D
toPath2D()
Construct a Path2D from this PolyLine2d.String
toPlot()
Convert this PolyLine2D to Peter's plot format.String
toString()
Produce a string describing the Drawable using default conversion for the (double) coordinate values.String
toString(String doubleFormat, boolean doNotIncludeClassName)
Produce a String describing the Drawable.-
Methods inherited from class org.djutils.draw.line.PolyLine2d
cleanPoints, closestPointOnPolyLine, concatenate, concatenate, concatenate, equals, extract, get, getBounds, getLocation, getLocationExtended, getPoints, getX, getY, hashCode, instantiate, iteratorToList, lengthAtIndex, makeArray, noiseFilteredLine, 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 org.djutils.draw.line.PolyLine
extractFractional, find, getFirst, getLast, getLocationFraction, getLocationFraction, getLocationFractionExtended, offsetLine, offsetLine
-
-
-
-
Constructor Detail
-
Polygon2d
public Polygon2d(double[] x, double[] y) throws DrawRuntimeException
Construct a new Polygon2d.- Parameters:
x
- double[]; the x coordinates of the pointsy
- double[]; the y coordinates of the points- Throws:
DrawRuntimeException
- when any two successive points are equal, or when there are too few points
-
Polygon2d
public Polygon2d(Point2d[] points) throws NullPointerException, DrawRuntimeException
Construct a new Polygon2d.- Parameters:
points
- Point2d[]; array of Point2d objects.- Throws:
NullPointerException
- when points is nullDrawRuntimeException
- when points is too short, or contains successive duplicate points
-
Polygon2d
public Polygon2d(Point2d point1, Point2d point2, Point2d... otherPoints) throws NullPointerException, DrawRuntimeException
Construct a new Polygon2d.- Parameters:
point1
- Point2d; the first point of the new Polygon2dpoint2
- Point2d; the second point of the new Polygon2dotherPoints
- Point2d[]; all remaining points of the new Polygon2d (may be null)- Throws:
NullPointerException
- when point1 or point2 is nullDrawRuntimeException
- when point1 is equal to the last point of otherPoints, or any two successive points are equal
-
Polygon2d
public Polygon2d(List<Point2d> points) throws NullPointerException, DrawRuntimeException
Construct a new Polygon2d from a list of Point2d objects.- Parameters:
points
- List<Point2d>; the list of points- Throws:
NullPointerException
- when points is nullDrawRuntimeException
- when points is too short, or the last two points are at the same location
-
Polygon2d
public Polygon2d(Iterator<Point2d> iterator)
Construct a new Polygon2d from an iterator that yields Point2d.- Parameters:
iterator
- Iterator<Point2d>; the iterator
-
Polygon2d
public Polygon2d(boolean filterDuplicates, Point2d... points) throws DrawRuntimeException
Create a new Polygon2d, optionally filtering out repeating successive points.- Parameters:
filterDuplicates
- boolean; if true; filter out successive repeated points; otherwise do not filterpoints
- Point2d...; the coordinates of the polygon as Point2d- Throws:
DrawRuntimeException
- when number of points < 2
-
Polygon2d
public Polygon2d(boolean filterDuplicates, List<Point2d> pointList) throws DrawRuntimeException
Create a new Polygon2d, optionally filtering out repeating successive points.- Parameters:
filterDuplicates
- boolean; if true; filter out successive repeated points; otherwise do not filterpointList
- List<Point2d>; list of the coordinates of the line as Point3d; any duplicate points in this list are removed (this method may modify the provided list)- Throws:
DrawRuntimeException
- when number of non-equal points < 2
-
-
Method Detail
-
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:
- boolean; true if this Polygon2d is convex; false if this Polygon2d is concave
-
contains
public boolean contains(Point2d point)
Determine if a point is inside this Polygon. Returns bogus results for self-intersecting polygons.- Parameters:
point
- Point2d; the point- Returns:
- boolean; true if the point is inside this polygon, false if the point is outside this polygon. Results are ill-defined for points on the edges of this Polygon.
-
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
- double; the x-coordinate of the pointy
- double; the y-coordinate of the point- Returns:
- boolean; true if the point is inside this polygon, false if the point is outside this polygon. Results are ill-defined for points on the edges of this Polygon.
-
contains
public boolean contains(Bounds2d bounds)
Determine if this Polygon completely contains a Bounds2d object. If this Polygon self-intersects, the results is bogus.- Parameters:
bounds
- Bounds2d; the Bounds2d object- Returns:
- boolean; true if the Bounds2d object is completely contained in this Polygon; false if any part (or all) of the Bounds2d object is outside this Polygon. If the Bounds2d object touches this Polygon the results are ill-defined.
-
intersects
public boolean intersects(Polygon2d other)
Determine if this Polygon intersects another Polygon.- Parameters:
other
- Polygon2d; the other Polygon- Returns:
- boolean; 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:
- double; the surface of this Polygon2d
-
getLength
public double getLength()
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,LineSegment2d>
- Overrides:
getLength
in classPolyLine2d
- Returns:
- double; the length of this line
-
getSegment
public LineSegment2d getSegment(int index)
Extract one LineSegment of this PolyLine, or Polygon.- Specified by:
getSegment
in interfacePolyLine<PolyLine2d,Point2d,Ray2d,LineSegment2d>
- Overrides:
getSegment
in classPolyLine2d
- Parameters:
index
- int; the rank number of the segment; must be in range 0..Size() - 2 for PolyLine, or 0.. Size() - 1 for Polygon.- Returns:
- LS; the LineSegment that connects point index to point index + 1
-
reverse
public Polygon2d reverse()
Construct a new PolyLine with all points of this PolyLine in reverse order.- Returns:
- L; the new PolyLine
-
toPath2D
public Path2D toPath2D()
Construct a Path2D from this PolyLine2d. The result is NOT cached (in the current implementation).- Overrides:
toPath2D
in classPolyLine2d
- Returns:
- Path2D; newly construct Path2D consisting solely of straight segments.
-
toExcel
public final String toExcel()
Convert this PolyLine to something that MS-Excel can plot.- Specified by:
toExcel
in interfacePolyLine<PolyLine2d,Point2d,Ray2d,LineSegment2d>
- Overrides:
toExcel
in classPolyLine2d
- Returns:
- String MS-excel XY, or XYZ plottable output
-
toPlot
public final String toPlot()
Convert this PolyLine2D to Peter's plot format.- Overrides:
toPlot
in classPolyLine2d
- Returns:
- Peter's format plot output
-
toString
public final String toString()
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:
- String; a string describing the Drawable
-
toString
public String toString(String doubleFormat, boolean doNotIncludeClassName)
Produce a String describing the Drawable.- Specified by:
toString
in interfaceDrawable<Point2d>
- Overrides:
toString
in classPolyLine2d
- Parameters:
doubleFormat
- String; a format string (something like "%6.3f") which will be used to render every coordinate value)doNotIncludeClassName
- boolean; if true; the output of toString 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:
- String; textual representation of the Drawable
-
-