Package org.openstreetmap.josm.tools
Class ShapeClipper
- java.lang.Object
-
- org.openstreetmap.josm.tools.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
-
-
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
-
-
-
Field Detail
-
LEFT
private static final int LEFT
- See Also:
- Constant Field Values
-
TOP
private static final int TOP
- See Also:
- Constant Field Values
-
RIGHT
private static final int RIGHT
- See Also:
- Constant Field Values
-
BOTTOM
private static final int BOTTOM
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ShapeClipper
private ShapeClipper()
-
-
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 clipclippingRect
- 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 pairsnum
- the number of valid values in pointsbbox
- the bounding box of the pathclippingRect
- 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 pairsnum
- 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 pairsnum
- the number of valid values in pointsbbox
- the bounding box of the pathclippingRect
- the clipping rectangle- Returns:
- the clipped path as a Path2D.Double or null if the result is empty
-
-