Function convex_polygons_intersection_with_tolerances

Source
pub fn convex_polygons_intersection_with_tolerances(
    poly1: &[Point2<f32>],
    poly2: &[Point2<f32>],
    tolerances: PolygonIntersectionTolerances,
    out: impl FnMut(Option<PolylinePointLocation>, Option<PolylinePointLocation>),
)
Expand description

Computes the intersection of two convex polygons with custom tolerances and closure-based output.

This is the most flexible version of the convex polygon intersection function, combining custom tolerances with closure-based output for maximum control.

§Arguments

  • poly1 - First convex polygon as a slice of vertices
  • poly2 - Second convex polygon as a slice of vertices
  • tolerances - Custom tolerances for numerical precision
  • out - Closure called for each intersection vertex

§Algorithm

This function implements the Sutherland-Hodgman-like algorithm for convex polygon intersection. It works by:

  1. Traversing the edges of both polygons simultaneously
  2. Detecting edge-edge intersections
  3. Determining which vertices are inside the other polygon
  4. Outputting the vertices of the intersection polygon in order

§Examples

let hexagon = vec![
    Point2::new(2.0, 0.0),
    Point2::new(1.0, 1.732),
    Point2::new(-1.0, 1.732),
    Point2::new(-2.0, 0.0),
    Point2::new(-1.0, -1.732),
    Point2::new(1.0, -1.732),
];

let square = vec![
    Point2::new(-1.0, -1.0),
    Point2::new(1.0, -1.0),
    Point2::new(1.0, 1.0),
    Point2::new(-1.0, 1.0),
];

let tolerances = PolygonIntersectionTolerances::default();
let mut intersection_points = Vec::new();

convex_polygons_intersection_with_tolerances(
    &hexagon,
    &square,
    tolerances,
    |loc1, loc2| {
        if let Some(loc) = loc1 {
            intersection_points.push(loc.to_point(&hexagon));
        } else if let Some(loc) = loc2 {
            intersection_points.push(loc.to_point(&square));
        }
    }
);

// The intersection should form a polygon
assert!(intersection_points.len() >= 3);