Class Polygon


public class Polygon extends Polyline
Represents a closed polygon in a 2D space, extending the Polyline class. This class provides additional methods such as area calculation, point inclusion test, and polygons intersection.

A polygon is defined by a sequence of points where the first and last points are equal to ensure closure.

Author:
Andrey Malykhanov
  • Constructor Details

    • Polygon

      public Polygon(Polyline polyline)
      Constructs a Polygon from a given Polyline. Ensures that the polygon is closed by appending the first point if necessary.

      If the specified polyline has less than 2 points or all its points lie on the same line, a polygon instance with zero area is still created.

      Parameters:
      polyline - the polyline used to construct the polygon
    • Polygon

      public Polygon(List<Point> points)
      Constructs a Polygon from a given list of Points. Ensures that the polygon is closed by appending the first point if necessary.

      If there are less than 2 points in the specified list, or all its points lie on the same, a polygon instance with zero area is still created.

      Parameters:
      points - the list of points used to construct the polygon
  • Method Details

    • getArea

      public double getArea()
      Returns the area of the polygon. Area of an empty polygon is considered zero.
      Returns:
      the area of the polygon
    • getCenterPoint

      public Point getCenterPoint()
      Returns the center point of the polygon.
    • isPointInside

      public boolean isPointInside(Point point)
      Checks whether a given point is inside the polygon or on its boundary.

      A point lying exactly on the polygon's boundary is considered inside.

      To check if the point is strictly lying on the polygon's boundary, use Polyline.containsPoint(Point, boolean) method.

      Parameters:
      point - the point to be checked
      Returns:
      true if the point is inside the polygon or on its boundary, false otherwise
      See Also:
    • getSimplePolygons

      public List<Polygon> getSimplePolygons()
      Returns a list of closed polygons without self-intersections each having non-zero area, that fully cover the area of this polygon. For empty polygons or polygons with zero area, returns an empty list.

      If this polygon has a non-zero area and does not have self-intersections or zero-area fragments, returns a list containing single element - a copy of this polygon.

      The order of the returned polygons is not guaranteed but is always the same for the same arguments.

      Returns:
      list of closed polygons without self-intersections each having non-zero area
      See Also:
    • getIntersectionPolygons

      public List<Polygon> getIntersectionPolygons(Polygon other)
      Returns a list of closed polygons without self-intersections each having non-zero area, that fully cover the area of intersection of this polygon and the specified other polygon.

      If the two polygons do not intersect, or their intersection has zero area, an empty list is returned.

      The order of the returned polygons is not guaranteed but is always the same for the same arguments.

      Parameters:
      other - the other polygon to compute the intersection with
      Returns:
      list of closed polygons without self-intersections each having non-zero area
      See Also:
    • getExclusionPolygons

      public List<Polygon> getExclusionPolygons(Polygon other)
      Returns a list of closed polygons without self-intersections each having non-zero area, that fully cover the area of this polygon with the specified other polygon excluded from this one.

      If the specified other polygon does not intersect with this one, the result of this call is identical to the result of getSimplePolygons() call on this polygon.

      Parameters:
      other - the other polygon to be excluded from the this one
      Returns:
      list of closed polygons without self-intersections each having non-zero area
      See Also:
    • getIntersectionPolygon

      @Deprecated public Polygon getIntersectionPolygon(Polygon other)
      Deprecated.
      Returns the intersection polygon between this polygon and another. If the two polygons do not intersect or their boundaries just touch each other, an empty polygon is returned. In other words, if the intersection of the two polygons is a polygon with zero area, an empty polygon is returned.
      Parameters:
      other - the other polygon to compute the intersection with
      Returns:
      a new Polygon representing the intersection of the two polygons
      See Also:
    • isEquivalent

      public boolean isEquivalent(Polygon other)
      Checks whether this polygon is equivalent to the other polygon, meaning they contain the same points in the same order but possibly with a different starting point.

      Two polygons are considered equivalent if they have the same number of points, are closed (first and last points are the same), and their point sequences can be cyclically shifted to match each other.

      Special cases:

      • Two empty polygons are considered equivalent.
      • Two polygons with a single identical point are considered equivalent.
      Parameters:
      other - the polygon to compare with
      Returns:
      true if the polygons are equivalent, false otherwise
    • containsPoints

      public boolean containsPoints(List<Point> points)
      Checks whether all points in the given list are on the boundary of the polygon .
      Parameters:
      points - the list of points to check (may be empty, but must not be null)
      Returns:
      true if all points in the list are contained on the boundary of the polygon, false if at least one point is outside or inside or if the input list is null
      See Also:
    • isOtherPolygonInside

      public boolean isOtherPolygonInside(Polygon other)
      Checks whether the other polygon is completely inside this polygon.
      Returns:
      true if all points of the other polygon are inside this polygon, false otherwise
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Polyline