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 verticespoly2- Second convex polygon as a slice of verticestolerances- Custom tolerances for numerical precisionout- Closure called for each intersection vertex
§Algorithm
This function implements the Sutherland-Hodgman-like algorithm for convex polygon intersection. It works by:
- Traversing the edges of both polygons simultaneously
- Detecting edge-edge intersections
- Determining which vertices are inside the other polygon
- 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);