Class ShapeClipper


  • public final class ShapeClipper
    extends java.lang.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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int BOTTOM  
      private static int LEFT  
      private static int RIGHT  
      private static int TOP  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ShapeClipper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean addToResult​(java.awt.geom.Path2D.Double result, double[] points, int num, java.awt.geom.Rectangle2D bbox, java.awt.geom.Rectangle2D clippingRect)
      Clip extracted segment if needed and add it to result if not completely outside of clipping rectangle.
      static java.awt.geom.Path2D.Double clipShape​(java.awt.Shape shape, java.awt.geom.Rectangle2D clippingRect)
      Clip a given (closed) shape with a given rectangle.
      private static java.awt.geom.Path2D.Double clipSinglePathWithSutherlandHodgman​(double[] points, int num, java.awt.geom.Rectangle2D bbox, java.awt.geom.Rectangle2D clippingRect)
      Clip a single path with a given rectangle using the Sutherland-Hodgman algorithm.
      private static java.awt.geom.Path2D.Double pointsToPath2D​(double[] points, int num)
      Convert a list of points to a Path2D.Double
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • clipShape

        public static java.awt.geom.Path2D.Double clipShape​(java.awt.Shape shape,
                                                            java.awt.geom.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​(java.awt.geom.Path2D.Double result,
                                           double[] points,
                                           int num,
                                           java.awt.geom.Rectangle2D bbox,
                                           java.awt.geom.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 java.awt.geom.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 java.awt.geom.Path2D.Double clipSinglePathWithSutherlandHodgman​(double[] points,
                                                                                       int num,
                                                                                       java.awt.geom.Rectangle2D bbox,
                                                                                       java.awt.geom.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