Expand description
clipper2 is a path/polygon clipping and offsetting library that supports operations like difference, inflate, intersect, point-in-polygon, union, xor, simplify.
The create uses the clipper2c-sys crate that in turn is a Rust wrapper around the C++ version of Clipper2.
The crate exposes the Clipper API in two alternative ways until the best version has been figured out.
- Through the
Path
/Paths
struct methods:Path::inflate
Path::simplify
Path::is_point_inside
Paths::inflate
Paths::simplify
Paths::to_clipper_subject
returns aClipper
builder struct with the current set of paths as the first subject, and allowing to make boolean operations on several sets of paths in one go.Paths::to_clipper_open_subject
similar but adds the current set of paths as an open “line” rather than a closed path/polygon.
- Via the plain functions:
The Path
/Paths
structs also thas some transformation methods such
as:
Path::translate
/Paths::translate
for moving a path in by a x/y offsetPath::rotate
/Paths::rotate
for rotating a path in by x radiansPath::scale
/Paths::scale
for scaling a path by multiplier
§Examples
use clipper2::*;
let path_a: Paths = vec![(0.2, 0.2), (6.0, 0.2), (6.0, 6.0), (0.2, 6.0)].into();
let path_b: Paths = vec![(5.0, 5.0), (8.0, 5.0), (8.0, 8.0), (5.0, 8.0)].into();
let output: Vec<Vec<(f64, f64)>> = path_a
.to_clipper_subject()
.add_clip(path_b)
.difference(FillRule::default())
.expect("Failed difference operation")
.into();
dbg!(output);
More examples can be found in the examples directory.
Structs§
- Bounds
- Represents an area from one min and one max Point.
- Centi
- Scale by 100. This is the default.
- Clipper
- The Clipper struct used as a builder for applying boolean operations to paths.
- Deci
- Scale by 10.
- Milli
- Scale by 1000.
- NoSubjects
- A state indicating no subjects and no clips.
- One
- No scaling.
- Path
- A collection of points.
- Paths
- A collection of paths.
- Point
- XY Point with custom scaler.
- With
Clips - A state indicating one or more subjects and one or more clips.
- With
Subjects - A state indicating one or more subjects and no clips.
Enums§
- Clipper
Error - Errors that can occur during clipper operations.
- EndType
- The EndType enumerator is only needed when offsetting (inflating/shrinking). It isn’t needed for polygon clipping.
- Fill
Rule - The Clipper Library supports 4 filling rules: Even-Odd, Non-Zero, Positive and Negative. These rules are base on the winding numbers (see below) of each polygon sub-region, which in turn are based on the orientation of each path. Orientation is determined by the order in which vertices are declared during path construction, and whether these vertices progress roughly clockwise or counter-clockwise.
- Join
Type - The JoinType enumeration is only needed when offsetting (inflating/shrinking). It isn’t needed for polygon clipping. It specifies how to manage offsetting at convex angled joins. Concave joins will always be offset with a mitered join.
- Path
Error - Path related errors
- Point
InPolygon Result - The result indicates whether the point is inside, or outside, or on one of the specified polygon’s edges.
Traits§
- Clipper
State - The state of the Clipper struct.
- Point
Scaler - The point scaling trait allows to choose a multiplier for the point values at compile time.
Functions§
- difference
- This function differences closed subject paths from clip paths.
- inflate
- This function performs both closed path and open path offsetting.
- intersect
- This function intersects closed subject paths with clip paths.
- point_
in_ polygon - The function result indicates whether the point is inside, or outside, or on one of the specified polygon’s edges.
- simplify
- This function removes points that are less than the specified epsilon distance from an imaginary line that passes through its 2 adjacent points. Logically, smaller epsilon values will be less aggressive in removing points than larger epsilon values.
- union
- This function joins a set of closed subject paths, with and without clip paths.
- xor
- This function ‘XORs’ closed subject paths and clip paths.