#polygon #union #difference #intersection #xor #operations #self-intersections

i_overlay

Boolean Operations for 2D Polygons: Supports intersection, union, difference, xor, and self-intersections for all polygon varieties

38 releases (2 stable)

new 1.0.1 Apr 25, 2024
0.25.0 Apr 18, 2024
0.24.2 Mar 31, 2024
0.8.0 Dec 22, 2023
0.5.0 Nov 16, 2023

#100 in Math

Download history 38/week @ 2024-01-06 45/week @ 2024-01-13 6/week @ 2024-01-20 33/week @ 2024-01-27 99/week @ 2024-02-10 29/week @ 2024-02-17 72/week @ 2024-02-24 2/week @ 2024-03-02 23/week @ 2024-03-09 8/week @ 2024-03-16 219/week @ 2024-03-23 643/week @ 2024-03-30 232/week @ 2024-04-06 152/week @ 2024-04-13 241/week @ 2024-04-20

1,300 downloads per month
Used in 3 crates

MIT license

140KB
3K SLoC

iOverlay

Balloons

The iOverlay is a fast poly-bool library supporting main operations like union, intersection, difference, and xor, governed by either the even-odd or non-zero rule. This algorithm is based on Vatti clipping ideas but is an original implementation.

Documentation

Try out iOverlay with an interactive demo:

Features

  • Operations: union, intersection, difference, and exclusion.
  • Polygons: with holes, self-intersections, and multiple paths.
  • Simplification: removes degenerate vertices and merges collinear edges.
  • Fill Rules: even-odd and non-zero.

Getting Started

Add the following to your Cargo.toml:

[dependencies]
i_float = "^1.0.0"
i_overlay = "^1.0.0"

Hello world

Let's union two squares

let subj = [
    F64Point::new(-10.0, -10.0),
    F64Point::new(-10.0, 10.0),
    F64Point::new(10.0, 10.0),
    F64Point::new(10.0, -10.0),
];

let clip = [
    F64Point::new(-5.0, -5.0),
    F64Point::new(-5.0, 15.0),
    F64Point::new(15.0, 15.0),
    F64Point::new(15.0, -5.0),
];

let mut overlay = FloatOverlay::new();

overlay.add_path(&subj, ShapeType::Subject);
overlay.add_path(&clip, ShapeType::Clip);

let graph = overlay.build_graph(FillRule::NonZero);
let shapes = graph.extract_shapes(OverlayRule::Union);

println!("shapes count: {}", shapes.len());

if shapes.len() > 0 {
    let contour = &shapes[0][0];
    println!("shape 0 contour: ");
    for p in contour {
        let x = p.x;
        let y = p.y;
        println!("({}, {})", x, y);
    }
}

Overlay Rules

Union, A or B

Union

Intersection, A and B

Intersection

Difference, B - A

Difference

Exclusion, A xor B

Exclusion

Dependencies

~0.5–1MB
~25K SLoC