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,DirectedPoint2d,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-2024 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:
  • Constructor Details

    • Polygon2d

      public Polygon2d(double[] x, double[] y) throws DrawRuntimeException
      Construct a new Polygon2d.
      Parameters:
      x - double[]; the x coordinates of the points
      y - 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 null
      DrawRuntimeException - 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 Polygon2d
      point2 - Point2d; the second point of the new Polygon2d
      otherPoints - Point2d[]; all remaining points of the new Polygon2d (may be null)
      Throws:
      NullPointerException - when point1 or point2 is null
      DrawRuntimeException - 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 null
      DrawRuntimeException - 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 filter
      points - 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 filter
      pointList - 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
    • Polygon2d

      public Polygon2d(Polygon2d polygon)
      Construct a new Polygon2d from an existing one. This constructor is primarily intended for use in extending classes.
      Parameters:
      polygon - Polygon2d; the existing Polygon2d
  • 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:
      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 point
      y - 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()
      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 interface PolyLine<PolyLine2d,Point2d,Ray2d,DirectedPoint2d,LineSegment2d>
      Overrides:
      getLength in class PolyLine2d
      Returns:
      double; the length of this line
    • getSegment

      public LineSegment2d getSegment(int index)
      Description copied from interface: PolyLine
      Extract one LineSegment of this PolyLine, or Polygon.
      Specified by:
      getSegment in interface PolyLine<PolyLine2d,Point2d,Ray2d,DirectedPoint2d,LineSegment2d>
      Overrides:
      getSegment in class PolyLine2d
      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()
      Description copied from interface: PolyLine
      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 class PolyLine2d
      Returns:
      Path2D; newly construct Path2D consisting solely of straight segments.
    • toString

      public final String 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 interface Drawable<Point2d>
      Overrides:
      toString in class PolyLine2d
      Returns:
      String; a string describing the Drawable
    • toString

      public String toString(String doubleFormat, boolean doNotIncludeClassName)
      Description copied from interface: Drawable
      Produce a String describing the Drawable.
      Specified by:
      toString in interface Drawable<Point2d>
      Overrides:
      toString in class PolyLine2d
      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