Class Polygon

  • All Implemented Interfaces:
    java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<Shape>

    public final class Polygon
    extends AbstractPolygon
    Defines a polygon.

    Given a list of points, the polygon is the area inside the figure created by joining each pair of adjacent points by a line segment. The first and last points are also joined; you do not need to repeat the first point in order to close the polygon.

    A polygon whose edges cross each other may have unpredictable behavior. A point exactly on the edge of a polygon may fall inside or outside the polygon unpredictably. A point very close to an edge may seem to fall in the wrong area because of implications of floating-point arithmetic.

    WARNING: this API is experimental and will likely change in the near future.

    See Also:
    Serialized Form
    • Constructor Detail

      • Polygon

        public Polygon()
        Constructs a polygon with default vertex capacity.
      • Polygon

        public Polygon​(Point... points)
      • Polygon

        public Polygon​(java.util.List<Point> points)
      • Polygon

        public Polygon​(int capacity)
        Constructs a polygon with specified capacity.
        Parameters:
        capacity - the vertex capacity
    • Method Detail

      • getTypeName

        public java.lang.String getTypeName()
        Specified by:
        getTypeName in class Shape
      • size

        public int size()
        Return the number of vertices in this polygon.
        Specified by:
        size in class AbstractPolygon
      • clear

        public void clear()
        Remove all points from this polygon.
      • getPoints

        public java.util.List<Point> getPoints()
      • setPoints

        public void setPoints​(java.util.List<Point> points)
      • fastGetX

        public double fastGetX​(int index)
        Get the X coordinate of the Point at index.

        WARNING: this method assumes that index is positive and less than AbstractPolygon.size().

        Specified by:
        fastGetX in class AbstractPolygon
      • fastGetY

        public double fastGetY​(int index)
        Get the Y coordinate of the Point at index.

        WARNING: this method assumes that index is positive and less than AbstractPolygon.size().

        Specified by:
        fastGetY in class AbstractPolygon
      • addPoint

        public void addPoint​(Point point)
        Add a point to this polygon.
      • addPoint

        public void addPoint​(double x,
                             double y)
        Add a point to this polygon.
      • getWidth

        public double getWidth()
        Get the width of this shape's bounding box.
        Specified by:
        getWidth in class Shape
      • getHeight

        public double getHeight()
        Get the height of this shape's bounding box.
        Specified by:
        getHeight in class Shape
      • getBoundingBox

        public Rectangle getBoundingBox()
        Get the rectangle that represents the bounding box that fully contains this shape.
        Specified by:
        getBoundingBox in class Shape
      • fastIntersects

        public boolean fastIntersects​(Rectangle box)
        Check if this shape's bounding box intersects with box.
        Specified by:
        fastIntersects in class Shape
      • contains

        public final boolean contains​(double x,
                                      double y)
        Return true if a point is contained inside this filter's polygon.
        Specified by:
        contains in class Shape
      • lineIntersects

        public boolean lineIntersects​(double x1,
                                      double y1,
                                      double x2,
                                      double y2)
        Check if the line (x1, y1), (x2, y2) intersects with this polygon.
        Specified by:
        lineIntersects in class AbstractPolygon
      • addArguments

        public void addArguments​(java.lang.StringBuilder buffer)
        Specified by:
        addArguments in class Shape
      • toString

        public void toString​(java.lang.StringBuilder buffer,
                             int i)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Shape
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object
      • writeExternal

        public void writeExternal​(java.io.DataOutput out)
                           throws java.io.IOException
        Specified by:
        writeExternal in class Shape
        Throws:
        java.io.IOException
      • readExternal

        public Polygon readExternal​(java.io.DataInput in)
                             throws java.io.IOException
        Specified by:
        readExternal in class Shape
        Throws:
        java.io.IOException