Class ShapeClipper


  • public final class ShapeClipper
    extends Object
    Tools to clip a shape based on the Sutherland-Hodgman algorithm. See https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm
    Since:
    14583
    • Method Detail

      • clipShape

        public static Path2D.Double clipShape​(Shape shape,
                                              Rectangle2D clippingRect)
        Clip a given (closed) shape with a given rectangle.
        Parameters:
        shape - the subject shape to clip
        clippingRect - the clipping rectangle
        Returns:
        the intersection of the shape and the rectangle or null if they don't intersect or the shape is not closed. The intersection may contain dangling edges.
      • addToResult

        private static boolean addToResult​(Path2D.Double result,
                                           double[] points,
                                           int num,
                                           Rectangle2D bbox,
                                           Rectangle2D clippingRect)
        Clip extracted segment if needed and add it to result if not completely outside of clipping rectangle.
        Parameters:
        result - the path that will describe the clipped shape (modified)
        points - array of x/y pairs
        num - the number of valid values in points
        bbox - the bounding box of the path
        clippingRect - the clipping rectangle
        Returns:
        true if data was added to result
      • pointsToPath2D

        private static Path2D.Double pointsToPath2D​(double[] points,
                                                    int num)
        Convert a list of points to a Path2D.Double
        Parameters:
        points - array of x/y pairs
        num - the number of valid values in points
        Returns:
        the path or null if the path describes a point or line.
      • clipSinglePathWithSutherlandHodgman

        private static Path2D.Double clipSinglePathWithSutherlandHodgman​(double[] points,
                                                                         int num,
                                                                         Rectangle2D bbox,
                                                                         Rectangle2D clippingRect)
        Clip a single path with a given rectangle using the Sutherland-Hodgman algorithm. This is much faster compared to the area.intersect method, but may create dangling edges.
        Parameters:
        points - array of x/y pairs
        num - the number of valid values in points
        bbox - the bounding box of the path
        clippingRect - the clipping rectangle
        Returns:
        the clipped path as a Path2D.Double or null if the result is empty