Clipper2 Rust
A Polygon Clipping and Offsetting library — a complete, pure Rust port of the Clipper2 C++ library by Angus Johnson.
Interactive Demo
Try it in your browser — no installation required
8 interactive pages running entirely via WebAssembly: boolean operations, fill rules, path offsetting, rectangle clipping, Minkowski operations, path simplification, PolyTree visualization, and utilities. All shapes are draggable for real-time exploration.
Status: Complete
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
Overview
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.
Features
- 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
Visual Examples
Clipping
Inflating (aka Offsetting)
Documentation
Comprehensive documentation for the Clipper2 algorithms and API is available at:
Clipper2 Documentation
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.
Quick Start
Add this to your Cargo.toml:
[]
= "1.0"
Boolean Operations
use FillRule;
let subject = vec!;
let clip = vec!;
let intersection = intersect_64;
let union = union_64;
let difference = difference_64;
let xor = xor_64;
Polygon Offsetting
use ;
let paths = vec!;
let inflated = inflate_paths_64;
Rectangle Clipping
use Rect64;
let rect = new;
let paths = vec!;
let clipped = rect_clip_64;
Architecture
Coordinate Systems
The library supports two coordinate systems:
Path64/Paths64: Integer coordinates usingi64— recommended for precisionPathD/PathsD: Floating-point coordinates usingf64— convenient for external data
Key Types
// Points
// Paths (polygons)
pub type Path64 = ;
pub type PathD = ;
pub type Paths64 = ;
pub type PathsD = ;
// Rectangles
Examples
Run the included examples:
Development
Prerequisites
- Rust 1.70+ (2021 edition)
Building
Testing
Benchmarking
Contributing
Contributions 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
License
This project is licensed under the Boost Software License 1.0, the same license as the original Clipper2 C++ library.
Acknowledgments
- Angus Johnson — Author of the original Clipper2 library
- MatterHackers — Developed this Rust port
- Claude by Anthropic — AI assistant used to perform the port
