Function nearest_point_from_point_to_multiple_lines
pub fn nearest_point_from_point_to_multiple_lines<'a, F>(
point: &Matrix<F, Const<2>, Const<1>, ArrayStorage<F, 2, 1>>,
polygon_lines: impl IntoIterator<Item = &'a (Matrix<F, Const<2>, Const<1>, ArrayStorage<F, 2, 1>>, Matrix<F, Const<2>, Const<1>, ArrayStorage<F, 2, 1>>)>,
) -> Option<(usize, (F, Matrix<F, Const<2>, Const<1>, ArrayStorage<F, 2, 1>>, F))>where
F: Copy + RealField,
Expand description
Generalizes the nearest_point_from_point_to_line function for a collection of line segments.
let point = Vector2::from([0.5; 2]);
let polygon = vec![
Vector2::from([0.0, 0.0]),
Vector2::from([0.5, 0.0]),
Vector2::from([0.7, 0.4]),
Vector2::from([0.9, 0.8]),
];
let lines = polygon.clone().into_iter().tuple_windows::<(_, _)>().collect::<Vec<_>>();
// This looks something like this:
// 3
// /
// p /
// 2
// /
// /
// 0 --- 1
let (n, (dist, calculated_point, t)) = nearest_point_from_point_to_multiple_lines(
&point,
&lines,
).unwrap();
assert_eq!(n, 1);
let test_dist: f64 = (calculated_point - point).norm();
assert!((dist - test_dist).abs() < 1e-6);
let (v1, v2) = lines[n];
assert!((calculated_point - ((1.0 - t)*v1 + t*v2)).norm() < 1e-6);
for p in polygon {
assert!(dist <= (p - point).norm());
}