Class Polygon2D

java.lang.Object
com.irurueta.geometry.Polygon2D
All Implemented Interfaces:
Serializable

public class Polygon2D extends Object implements Serializable
This class defines a polygon in 2D space.
See Also:
  • Field Details

    • DEFAULT_THRESHOLD

      public static final double DEFAULT_THRESHOLD
      Default threshold value. Thresholds are used to determine whether a point lies inside the polygon or not, or if it's locus or not, etc.
      See Also:
    • MIN_THRESHOLD

      public static final double MIN_THRESHOLD
      Minimum allowed threshold value.
      See Also:
    • MIN_VERTICES

      public static final int MIN_VERTICES
      Minimum number of vertices that a polygon is allowed to have.
      See Also:
    • DEFAULT_TRIANGULATOR_METHOD

      public static final TriangulatorMethod DEFAULT_TRIANGULATOR_METHOD
      Default method for triangulation.
    • vertices

      private List<Point2D> vertices
      List containing vertices of this polygon. Each vertex is a 2D point.
    • triangulated

      private boolean triangulated
      Boolean indicating whether polygon has already been triangulated.
    • triangles

      private List<Triangle2D> triangles
      List containing triangles found after triangulating this polygon. Initially this list will be null until triangulation is done.
    • triangulatorMethod

      private TriangulatorMethod triangulatorMethod
      Method to do triangulation.
  • Constructor Details

  • Method Details

    • getTriangulatorMethod

      public TriangulatorMethod getTriangulatorMethod()
      Returns triangulator method. Triangulator method determines the way a polygon is divided into triangles. If none has been provided DEFAULT_TRIANGULATOR_METHOD will be returned.
      Returns:
      Triangulator method.
    • setTriangulatorMethod

      public void setTriangulatorMethod(TriangulatorMethod triangulatorMethod)
      Sets triangulator method. A triangulator method determines the way a polygon is divided into triangles.
      Parameters:
      triangulatorMethod - A triangulator method.
    • getVertices

      public List<Point2D> getVertices()
      Returns the list of vertices forming this polygon.
      Returns:
      List of vertices.
    • setVertices

      public final void setVertices(List<Point2D> vertices) throws NotEnoughVerticesException
      Sets list of vertices forming this polygon.
      Parameters:
      vertices - List of vertices.
      Throws:
      NotEnoughVerticesException - Raised if provided list does not have enough vertices.
      See Also:
    • isTriangulated

      public boolean isTriangulated()
      Determines whether this polygon has already been triangulated. A polygon will only need to be triangulated once, unless the list of vertices is reset.
      Returns:
      True if polygon has already been triangulated, false otherwise.
    • getTriangles

      public List<Triangle2D> getTriangles() throws TriangulatorException
      Returns a list of triangles forming this polygon. This method checks whether this polygon has already been triangulated, if not, it performs triangulation first.
      Returns:
      A list of triangles forming this polygon.
      Throws:
      TriangulatorException - Raised if triangulation was needed and failed.
    • getSignedArea

      public double getSignedArea()
      Returns signed area of this polygon. The sign of the area determines whether vertices of the polygon are provided in clockwise (negative sign) or clockwise (positive sign) order.
      Returns:
      Signed area of this polygon.
    • getArea

      public double getArea()
      Returns area of this polygon.
      Returns:
      Area of this polygon.
    • areVerticesClockwise

      public boolean areVerticesClockwise(double threshold)
      Determines whether vertices of this polygon are in clockwise order or in counterclockwise order.
      Parameters:
      threshold - threshold to determine if vertices are in clockwise order. Usually this value is zero.
      Returns:
      True if vertices are in clockwise order, false otherwise.
    • areVerticesClockwise

      public boolean areVerticesClockwise()
      Determines whether vertices of this polygon are in clockwise order or in counterclockwise order.
      Returns:
      True if vertices are in clockwise order, false otherwise.
    • getPerimeter

      public double getPerimeter()
      Returns perimeter of this polygon. The perimeter is computed as the sum of the distances between consecutive pairs of vertices.
      Returns:
      Perimeter of this polygon.
    • isInside

      public boolean isInside(Point2D point) throws TriangulatorException
      Determines if provided point lies within the region defined by this polygon. Notice that this method is only ensured to work for polygons having no holes or crossing borders. It will safely work on any other polygon, no matter if it is regular, non-regular, convex or concave.
      Parameters:
      point - Point to be checked.
      Returns:
      True if point lies within the area defined by this polygon, false otherwise.
      Throws:
      TriangulatorException - Raised if triangulation was required but failed.
    • isInside

      public boolean isInside(Point2D point, double threshold) throws TriangulatorException
      Determines if provided point lies within the region defined by this polygon. Notice that this method is only ensured to work for polygons having no holes or crossing borders. It will safely work on any other polygon, no matter if it is regular, non-regular, convex or concave.
      Parameters:
      point - Point to be checked.
      threshold - Threshold to determine whether point lies inside this polygon. Usually this value should be small.
      Returns:
      True if point lies within the area defined by this polygon, false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
      TriangulatorException - Raised if triangulation was required but failed.
    • getCenter

      public Point2D getCenter()
      Returns the center of this polygon. The center is the average point among all the vertices of this polygon. The center is not ensure to lie within the area formed by this polygon.
      Returns:
      Center of this polygon.
    • center

      public void center(Point2D result)
      Computes the center of this polygon. The center is the average point among all the vertices of this polygon. The center is not ensured to lie within the area formed by this polygon.
      Parameters:
      result - Instance where the computed center will be stored.
    • isLocus

      public boolean isLocus(Point2D point, double threshold)
      Determines whether provided point is locus of the borders defined by the vertices of this polygon. A point will be locus if it lies in the line defined by two consecutive vertices up to a certain threshold of error.
      Parameters:
      point - Point to be checked.
      threshold - Threshold of allowed error. This should usually be a small value.
      Returns:
      True if provided point lies in a border of this polygon, false otherwise.
      Throws:
      IllegalArgumentException - Raised if provided threshold is negative.
    • isLocus

      public boolean isLocus(Point2D point)
      Determines whether provided point is locus of the borders defined by the vertices of this polygon. A point will be locus if it lies in the line defined by two consecutive vertices.
      Parameters:
      point - Point to be checked.
      Returns:
      True if provided point lies in a border of this polygon, false otherwise.
    • getShortestDistance

      public double getShortestDistance(Point2D point)
      Returns the shortest distance from provided point to a border of this polygon. Note that borders are segments defined by consecutive vertices.
      Parameters:
      point - Point to be checked.
      Returns:
      Shortest distance from provided point to this polygon.
    • getClosestPoint

      public Point2D getClosestPoint(Point2D point)
      Returns the closest point to provided point that is locus of this polygon (i.e. lies on a border of this polygon).
      Parameters:
      point - Point to be checked.
      Returns:
      Closest point being locus of this polygon.
    • closestPoint

      public void closestPoint(Point2D point, Point2D result)
      Computes the closes point to provided point that is locus of this polygon (i.e. lies on a border of this polygon).
      Parameters:
      point - Point to be checked.
      result - Instance where the closest point will be stored.
    • triangulate

      public void triangulate() throws TriangulatorException
      Triangulates this polygon using this polygon's triangulator method. A polygon only will be triangulated once when required or this method is called. This method will make no action if a polygon is already triangulated unless it's vertices are reset.
      Throws:
      TriangulatorException - Raised if triangulation failed
      See Also: