Function convex_polygons_intersection

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

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

This function is similar to convex_polygons_intersection_points but provides more flexibility by calling a closure for each intersection vertex. The closure receives the location of the vertex on each polygon (if applicable).

This is useful when you need to track which polygon each intersection vertex comes from, or when you want to process vertices as they’re computed rather than collecting them all.

§Arguments

  • poly1 - First convex polygon as a slice of vertices
  • poly2 - Second convex polygon as a slice of vertices
  • out - Closure called for each intersection vertex with its location on both polygons

§Closure Arguments

The closure receives (Option<PolylinePointLocation>, Option<PolylinePointLocation>):

  • If the point comes from poly1, the first option contains its location on poly1
  • If the point comes from poly2, the second option contains its location on poly2
  • At least one of the options will always be Some

§Examples

let square = vec![
    Point2::origin(),
    Point2::new(2.0, 0.0),
    Point2::new(2.0, 2.0),
    Point2::new(0.0, 2.0),
];

let diamond = vec![
    Point2::new(1.0, -0.5),
    Point2::new(2.5, 1.0),
    Point2::new(1.0, 2.5),
    Point2::new(-0.5, 1.0),
];

let mut intersection_points = Vec::new();
convex_polygons_intersection(&square, &diamond, |loc1, loc2| {
    if let Some(loc) = loc1 {
        intersection_points.push(loc.to_point(&square));
    } else if let Some(loc) = loc2 {
        intersection_points.push(loc.to_point(&diamond));
    }
});

// The intersection should have multiple vertices
assert!(intersection_points.len() >= 3);

§See Also