A Polygon Clipping and Offsetting library — a complete, pure Rust port of the Clipper2 C++ library by Angus Johnson.
This port is feature-complete — all core algorithms have been ported and verified against the original C++ implementation.
- 444 tests (392 unit + 52 integration), all passing, 0 ignored
- Exact behavioral match with C++ on all test cases, including edge cases from 20+ GitHub issues
- 4 examples demonstrating clipping, offsetting, rectangle clipping, and benchmarking
- 6 Criterion benchmarks covering boolean ops, offsetting, rect clipping, and simplification
The Clipper2 library performs intersection, union, difference and XOR boolean operations on both simple and complex polygons. It also performs polygon offsetting/inflating.
This is a pure Rust port of the original Clipper2 C++ library, with identical functionality, algorithmic behavior, and precision while leveraging Rust's memory safety guarantees.
This port was created by MatterHackers using Claude by Anthropic.
- Boolean Operations: Intersection, Union, Difference, XOR on both simple and complex polygons
- Polygon Offsetting: Inflate/deflate polygons with Miter, Square, Bevel, and Round join types
- Rectangle Clipping: High-performance rectangular clipping
- Minkowski Sum/Difference: Geometric Minkowski operations on polygons
- Path Simplification: Reduce polygon complexity while preserving shape (Ramer-Douglas-Peucker)
- Multiple Precision: Integer (
i64) and floating-point (f64) coordinate support - PolyTree Structure: Hierarchical representation of polygon parent/child/hole relationships
- SVG Output: Built-in SVG writer for visualization and debugging
- Memory Safe: All the benefits of Rust's ownership system with zero-cost abstractions
Clipping
Inflating (aka Offsetting)
Comprehensive documentation for the Clipper2 algorithms and API is available at:
The Rust API follows the same structure and naming conventions (adapted to Rust idioms) as the original C++ library, so the upstream documentation serves as an excellent reference.
Add this to your Cargo.toml:
[dependencies]
clipper2 = "0.1.0"use clipper2::core::FillRule;
let subject = vec![clipper2::make_path64(&[100, 100, 300, 100, 300, 300, 100, 300])];
let clip = vec![clipper2::make_path64(&[200, 200, 400, 200, 400, 400, 200, 400])];
let intersection = clipper2::intersect_64(&subject, &clip, FillRule::NonZero);
let union = clipper2::union_64(&subject, &clip, FillRule::NonZero);
let difference = clipper2::difference_64(&subject, &clip, FillRule::NonZero);
let xor = clipper2::xor_64(&subject, &clip, FillRule::NonZero);use clipper2::offset::{JoinType, EndType};
let paths = vec![clipper2::make_path64(&[0, 0, 100, 0, 100, 100, 0, 100])];
let inflated = clipper2::inflate_paths_64(&paths, 10.0, JoinType::Round, EndType::Polygon, 2.0, 0.0);use clipper2::core::Rect64;
let rect = Rect64::new(100, 100, 300, 300);
let paths = vec![clipper2::make_path64(&[50, 50, 350, 50, 350, 350, 50, 350])];
let clipped = clipper2::rect_clip_64(&rect, &paths);The library supports two coordinate systems:
Path64/Paths64: Integer coordinates usingi64— recommended for precisionPathD/PathsD: Floating-point coordinates usingf64— convenient for external data
// Points
pub struct Point64 { pub x: i64, pub y: i64 }
pub struct PointD { pub x: f64, pub y: f64 }
// Paths (polygons)
pub type Path64 = Vec<Point64>;
pub type PathD = Vec<PointD>;
pub type Paths64 = Vec<Path64>;
pub type PathsD = Vec<PathD>;
// Rectangles
pub struct Rect64 { pub left: i64, pub top: i64, pub right: i64, pub bottom: i64 }
pub struct RectD { pub left: f64, pub top: f64, pub right: f64, pub bottom: f64 }Run the included examples:
cargo run --example simple_clipping # Boolean intersection with SVG output
cargo run --example inflate_paths # Path offsetting demo
cargo run --example rect_clipping # Rectangle clipping demo
cargo run --example benchmark_cli # Performance benchmark- Rust 1.70+ (2021 edition)
cargo buildcargo test # All 444 tests
cargo test --lib core_tests # Specific module
cargo test test_name -- --exact # Specific testcargo benchContributions are welcome! Please note:
- All functions must be complete — no
todo!()or stubs - Tests required for all new functionality
- Must match C++ behavioral semantics for ported code
- Run
cargo testandcargo clippybefore submitting
This project is licensed under the Boost Software License 1.0, the same license as the original Clipper2 C++ library.
- Angus Johnson — Author of the original Clipper2 library
- MatterHackers — Developed this Rust port
- Claude by Anthropic — AI assistant used to perform the port