Package com.irurueta.geometry
Class Polygon2D
java.lang.Object
com.irurueta.geometry.Polygon2D
- All Implemented Interfaces:
Serializable
This class defines a polygon in 2D space.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDefault threshold value.static final TriangulatorMethodDefault method for triangulation.static final doubleMinimum allowed threshold value.static final intMinimum number of vertices that a polygon is allowed to have.private List<Triangle2D> List containing triangles found after triangulating this polygon.private booleanBoolean indicating whether polygon has already been triangulated.private TriangulatorMethodMethod to do triangulation.List containing vertices of this polygon. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanDetermines whether vertices of this polygon are in clockwise order or in counterclockwise order.booleanareVerticesClockwise(double threshold) Determines whether vertices of this polygon are in clockwise order or in counterclockwise order.voidComputes the center of this polygon.voidclosestPoint(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).doublegetArea()Returns area of this polygon.Returns the center of this polygon.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).doubleReturns perimeter of this polygon.doublegetShortestDistance(Point2D point) Returns the shortest distance from provided point to a border of this polygon.doubleReturns signed area of this polygon.Returns a list of triangles forming this polygon.Returns triangulator method.Returns the list of vertices forming this polygon.booleanDetermines if provided point lies within the region defined by this polygon.booleanDetermines if provided point lies within the region defined by this polygon.booleanDetermines whether provided point is locus of the borders defined by the vertices of this polygon.booleanDetermines whether provided point is locus of the borders defined by the vertices of this polygon.booleanDetermines whether this polygon has already been triangulated.voidsetTriangulatorMethod(TriangulatorMethod triangulatorMethod) Sets triangulator method.final voidsetVertices(List<Point2D> vertices) Sets list of vertices forming this polygon.voidTriangulates this polygon using this polygon's triangulator method.
-
Field Details
-
DEFAULT_THRESHOLD
public static final double DEFAULT_THRESHOLDDefault 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_THRESHOLDMinimum allowed threshold value.- See Also:
-
MIN_VERTICES
public static final int MIN_VERTICESMinimum number of vertices that a polygon is allowed to have.- See Also:
-
DEFAULT_TRIANGULATOR_METHOD
Default method for triangulation. -
vertices
List containing vertices of this polygon. Each vertex is a 2D point. -
triangulated
private boolean triangulatedBoolean indicating whether polygon has already been triangulated. -
triangles
List containing triangles found after triangulating this polygon. Initially this list will be null until triangulation is done. -
triangulatorMethod
Method to do triangulation.
-
-
Constructor Details
-
Polygon2D
Constructor.- Parameters:
vertices- List of vertices forming this polygon.- Throws:
NotEnoughVerticesException- Raised if list does not contain enough vertices.- See Also:
-
-
Method Details
-
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
Sets triangulator method. A triangulator method determines the way a polygon is divided into triangles.- Parameters:
triangulatorMethod- A triangulator method.
-
getVertices
Returns the list of vertices forming this polygon.- Returns:
- List of vertices.
-
setVertices
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
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
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
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
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
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
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
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
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
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
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
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:
-