Skip to main content

difference

Function difference 

Source
pub fn difference<G1, G2, P>(
    g1: &G1,
    g2: &G2,
) -> Result<MultiPolygon<Polygon<P>>, OverlayError>
where G1: PolygonTrait<Point = P>, G2: PolygonTrait<Point = P>, P: PointMut + Default + Copy, P::Scalar: CoordinateScalar + Into<f64>, <P::Cs as CoordinateSystem>::Family: SameAs<CartesianFamily>,
Expand description

Difference of two polygons — the region inside the first but outside the second (A − B).

Mirrors boost::geometry::difference from algorithms/difference.hpp:686-714.

§Errors

OverlayError::Unsupported when coordinates exceed the predicate range.

§Examples

use geometry_cs::Cartesian;
use geometry_model::{polygon, Point2D, Polygon};
use geometry_overlay::operation::difference;
use geometry_trait::MultiPolygon as _;

type P = Point2D<f64, Cartesian>;
let a: Polygon<P> = polygon![[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0), (0.0, 0.0)]];
let b: Polygon<P> = polygon![[(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0), (1.0, 1.0)]];
let out = difference(&a, &b).unwrap();
assert_eq!(out.polygons().count(), 1);