Class Polygon2D

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Polygon2D.Double
      The concrete Polygon class that stores coordinates internally as doubles.
      static class  Polygon2D.Float
      The concrete Polygon class that stores coordinates internally as floats.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean _closed
      The flag that says that the polygon has been closed
      protected int _coordCount
      The current number of coordinates
    • Constructor Summary

      Constructors 
      Constructor Description
      Polygon2D()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static double area​(Polygon2D poly)
      Calculates the double area of the given polygon.
      void closePath()
      Close the polygon.
      boolean contains​(double x, double y)
      Return true if the given point is inside the polygon.
      boolean contains​(double x1, double y1, double w, double h)
      Return true if the given rectangle is entirely inside the polygon.
      boolean contains​(Point2D p)
      Return true if the given point is inside the polygon.
      boolean contains​(Rectangle2D r)
      Return true if the given rectangle is entirely inside the polygon.
      boolean contains​(GeoRectangle r)
      Return true if the given rectangle is entirely inside the polygon.
      boolean contains​(Polygon2D p)
      Determines if the given polygon is fully contained within this polygon.

      This method can be fooled by suppling a polygon whose points are completly contained in this polygon but has some intersecting lines.
      abstract Polygon2D copy()
      Creates a copy of this polygon with the same coordinates.
      double distance​(Point2D p)
      Calculates the square of the distance from the point p to the nearest line segment specified by connecting the vertices of this Polygon2D
      double distanceSq​(Point2D p)
      Calculates the square of the distance from the point p to the nearest line segment specified by connecting the vertices of this Polygon2D
      double getArea()
      Calculates the area of this Polygon2D
      Rectangle getBounds()
      Get the integer Rectangle bounds of the polygon.
      abstract Rectangle2D getBounds2D()
      Get the floating-point bounds of the polygon.
      abstract GeoRectangle getBoundsGeo()
      Get the bound box of this poly as a GeoRectangle2D
      abstract Point2D getCentroid()
      Calculates the center point of this Polygon2D
      double getMaxX()
      Determines the highest x value within the points of this polygon
      double getMaxY()
      Determines the highest x value within the points of this polygon
      double getMinX()
      Determines the lowest x value within the points of this polygon
      double getMinY()
      Determines the lowest y value within the points of this polygon
      PathIterator getPathIterator​(AffineTransform at)
      Get a path iterator over the object.
      PathIterator getPathIterator​(AffineTransform at, double flatness)
      Get a path iterator over the object.
      int getVertexCount()
      Get the number of vertices
      abstract List<Point2D> getVertices()
      Gets a list of Point2D vertices of this polygon.
      abstract double getX​(int index)
      Get the given X-coordinate
      abstract double[] getXCoords()
      Gets a double array containing the X coordinates of the polygon.
      abstract double getY​(int index)
      Get the given Y-coordinate
      abstract double[] getYCoords()
      Gets a double array containing the Y coordinates of the polygon.
      boolean intersects​(double x1, double y1, double w, double h)
      Test if the polygon is intersected by the given rectangle.
      boolean intersects​(Point2D p)
      Inclusively checks if the given point is contained within this polygon or intersected by any of its line-segments
      boolean intersects​(Point2D e0, Point2D e1)
      Checks if the line segment given by the extremes e1 and e2 intersect this polygon, by checking segment to segment intersection for each vector of this polygon.
      boolean intersects​(Rectangle2D r)
      Test if the polygon is intersected by the given rectangle.
      boolean intersects​(GeoRectangle r)
      Test if the polygon is intersected by the given GeoRectangle.
      boolean intersects​(Polygon2D poly)
      Checks if this polygon intersects the given polygon.
      static boolean lineIntersectsPoint​(double e0x, double e0y, double e1x, double e1y, double p1x, double p1y)
      Checks if the point given by p1x, p1y is intersected by the line segment given by e0, e1
      static boolean linesIntersect​(double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double p3x, double p3y)
      Checks if the line segment drawn from p0 to p1 intersects the line drawn from p2 to p3.
      abstract void lineTo​(double x, double y)
      Add a new vertex to the end of the polygon.
      void lineTo​(Point2D p)  
      abstract void moveTo​(double x, double y)
      Move the start point of the vertex to the given position.
      void moveTo​(Point2D p)  
      void reset()
      Reset the polygon back to empty.
      abstract void setX​(int index, double x)
      Set the given X-coordinate.
      abstract void setY​(int index, double y)
      Set the given Y-coordinate
      String toString()
      Return a string representation of the polygon.
      abstract void transform​(AffineTransform at)
      Transform the polygon with the given transform.
      abstract void translate​(double x, double y)
      Translate the polygon the given distance.
    • Field Detail

      • _coordCount

        protected int _coordCount
        The current number of coordinates
      • _closed

        protected boolean _closed
        The flag that says that the polygon has been closed
    • Constructor Detail

      • Polygon2D

        public Polygon2D()
    • Method Detail

      • area

        public static double area​(Polygon2D poly)
        Calculates the double area of the given polygon.
        Parameters:
        poly - a Polygon2D object
        Returns:
        the double area
      • linesIntersect

        public static boolean linesIntersect​(double p0x,
                                             double p0y,
                                             double p1x,
                                             double p1y,
                                             double p2x,
                                             double p2y,
                                             double p3x,
                                             double p3y)
        Checks if the line segment drawn from p0 to p1 intersects the line drawn from p2 to p3. This is an optimized variation of Line2D.linesIntersect(double, double, double, double, double, double, double, double)
        Parameters:
        p0x - the first points x value
        p0y - the first points y value
        p1x - the second points x value
        p1y - the second point's y value
        p2x - the third point's x value
        p2y - the third point's y value
        p3x - the fourth point's x value
        p3y - the fourth point's y value
        Returns:
        true if the lines created from the given points intersect, otherwise false
      • lineIntersectsPoint

        public static boolean lineIntersectsPoint​(double e0x,
                                                  double e0y,
                                                  double e1x,
                                                  double e1y,
                                                  double p1x,
                                                  double p1y)
        Checks if the point given by p1x, p1y is intersected by the line segment given by e0, e1
        Parameters:
        e0x - the line segment's first endpoint's x value
        e0y - the line segment's first endpoint's y value
        e1x - the line segment's second endpoint's x value
        e1y - the line segment's second endpoint's y value
        p1x - the point's x value
        p1y - the point's y value
        Returns:
        true if the point is intersected by the given line segment, otherwise false
      • closePath

        public void closePath()
        Close the polygon. No further segments can be added. If this method not called, then the path iterators will treat the polygon as thought it were closed, and implicitly join the most recently added vertex to the first one. However, this method should generally be called, as if the last vertex is the same as the first vertex, then it merges them.
      • contains

        public boolean contains​(double x,
                                double y)
        Return true if the given point is inside the polygon. This method uses a straight-forward algorithm, where a point is taken to be inside the polygon if a horizontal line extended from the point to infinity intersects an odd number of segments.
        Specified by:
        contains in interface Shape
      • contains

        public boolean contains​(Point2D p)
        Return true if the given point is inside the polygon.
        Specified by:
        contains in interface Shape
      • contains

        public boolean contains​(Rectangle2D r)
        Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)
        Specified by:
        contains in interface Shape
      • contains

        public boolean contains​(GeoRectangle r)
        Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)
      • contains

        public boolean contains​(Polygon2D p)
        Determines if the given polygon is fully contained within this polygon.

        This method can be fooled by suppling a polygon whose points are completly contained in this polygon but has some intersecting lines. In this instance, although the polygons intersect the given polygon is not "wholely" contained within this polygon. This method would still return true.
        Parameters:
        p - a Polygon2D object
        Returns:
        true if all the points of the given polygon are contained within this Polygon2D, otherwise false
      • copy

        public abstract Polygon2D copy()
        Creates a copy of this polygon with the same coordinates. The original polygon remains unchanged.
        Returns:
        a copy of the this polygon
      • contains

        public boolean contains​(double x1,
                                double y1,
                                double w,
                                double h)
        Return true if the given rectangle is entirely inside the polygon. (Currently, this algorithm can be fooled by supplying a rectangle that has all four corners inside the polygon, but which intersects some edges.)
        Specified by:
        contains in interface Shape
      • getArea

        public double getArea()
        Calculates the area of this Polygon2D
        Returns:
        the double area of this polygon
      • getBounds2D

        public abstract Rectangle2D getBounds2D()
        Get the floating-point bounds of the polygon.
        Specified by:
        getBounds2D in interface Shape
      • getBoundsGeo

        public abstract GeoRectangle getBoundsGeo()
        Get the bound box of this poly as a GeoRectangle2D
      • getCentroid

        public abstract Point2D getCentroid()
        Calculates the center point of this Polygon2D
        Returns:
        the center point of this Polygon2D
      • distanceSq

        public double distanceSq​(Point2D p)
        Calculates the square of the distance from the point p to the nearest line segment specified by connecting the vertices of this Polygon2D
        Parameters:
        p - a Point2D
        Returns:
        the square of the distance from the given point to this Polygon2D
      • distance

        public double distance​(Point2D p)
        Calculates the square of the distance from the point p to the nearest line segment specified by connecting the vertices of this Polygon2D
        Parameters:
        p - a Point2D
        Returns:
        the square of the distance from the given point to this Polygon2D
      • getXCoords

        public abstract double[] getXCoords()
        Gets a double array containing the X coordinates of the polygon.
        Returns:
        the X coordinates
      • getYCoords

        public abstract double[] getYCoords()
        Gets a double array containing the Y coordinates of the polygon.
        Returns:
        the Y coordinates
      • getVertices

        public abstract List<Point2D> getVertices()
        Gets a list of Point2D vertices of this polygon.
        Returns:
        a List{Point2D} of vertices
      • getMinX

        public double getMinX()
        Determines the lowest x value within the points of this polygon
        Returns:
        the lowest x value
      • getMinY

        public double getMinY()
        Determines the lowest y value within the points of this polygon
        Returns:
        the lowest y value
      • getMaxX

        public double getMaxX()
        Determines the highest x value within the points of this polygon
        Returns:
        the highest x value
      • getMaxY

        public double getMaxY()
        Determines the highest x value within the points of this polygon
        Returns:
        the highest x value
      • getVertexCount

        public int getVertexCount()
        Get the number of vertices
      • getX

        public abstract double getX​(int index)
        Get the given X-coordinate
        Throws:
        IndexOutOfBoundsException - The index is out of bounds.
      • getY

        public abstract double getY​(int index)
        Get the given Y-coordinate
        Throws:
        IndexOutOfBoundsException - The index is out of bounds.
      • intersects

        public boolean intersects​(Point2D e0,
                                  Point2D e1)
        Checks if the line segment given by the extremes e1 and e2 intersect this polygon, by checking segment to segment intersection for each vector of this polygon. [TPL]
      • intersects

        public boolean intersects​(Polygon2D poly)
        Checks if this polygon intersects the given polygon. This algorithm uses the 2D Polygon-Polygon Intersection test as proposed in the webaddress below
        Returns:
        true if there is an intersection, otherwise false
        See Also:
        Efficient Collision Detection between 2D Polygons
      • intersects

        public boolean intersects​(Rectangle2D r)
        Test if the polygon is intersected by the given rectangle. (Currently, this algorithm can be fooled by supplying a rectangle that has no corners inside the polygon, and does not contain any vertex of the polygon, but which intersects some edges.)
        Specified by:
        intersects in interface Shape
      • intersects

        public boolean intersects​(GeoRectangle r)
        Test if the polygon is intersected by the given GeoRectangle. (Currently, this algorithm can be fooled by supplying a rectangle that has no corners inside the polygon, and does not contain any vertex of the polygon, but which intersects some edges.)
      • intersects

        public boolean intersects​(double x1,
                                  double y1,
                                  double w,
                                  double h)
        Test if the polygon is intersected by the given rectangle.
        Specified by:
        intersects in interface Shape
      • intersects

        public boolean intersects​(Point2D p)
        Inclusively checks if the given point is contained within this polygon or intersected by any of its line-segments
        Parameters:
        p - a Point2D
        Returns:
        true if the given point is intersected by this Polygon2D, otherwise false
      • lineTo

        public void lineTo​(Point2D p)
      • lineTo

        public abstract void lineTo​(double x,
                                    double y)
        Add a new vertex to the end of the polygon. Throw an exception of the polygon has already been closed.
      • moveTo

        public void moveTo​(Point2D p)
      • moveTo

        public abstract void moveTo​(double x,
                                    double y)
        Move the start point of the vertex to the given position. Throw an exception if the line already contains any vertices.
      • reset

        public void reset()
        Reset the polygon back to empty.
      • setX

        public abstract void setX​(int index,
                                  double x)
        Set the given X-coordinate.
        Throws:
        IndexOutOfBoundsException - The index is out of bounds.
      • setY

        public abstract void setY​(int index,
                                  double y)
        Set the given Y-coordinate
        Throws:
        IndexOutOfBoundsException - The index is out of bounds.
      • transform

        public abstract void transform​(AffineTransform at)
        Transform the polygon with the given transform.
      • translate

        public abstract void translate​(double x,
                                       double y)
        Translate the polygon the given distance.
      • toString

        public String toString()
        Return a string representation of the polygon.
        Overrides:
        toString in class Object