MCPcopy Create free account
hub / github.com/donkeyteethUX/iced_plot / nearest_control_point_index

Function nearest_control_point_index

examples/interactive_spline.rs:191–210  ·  view source on GitHub ↗
(
    control_points: &[[f64; 2]],
    world: [f64; 2],
    pick_radius_world: f64,
)

Source from the content-addressed store, hash-verified

189}
190
191fn nearest_control_point_index(
192 control_points: &[[f64; 2]],
193 world: [f64; 2],
194 pick_radius_world: f64,
195) -> Option<usize> {
196 let max_distance_sq = pick_radius_world * pick_radius_world;
197
198 control_points
199 .iter()
200 .enumerate()
201 .min_by(|(_, a), (_, b)| {
202 let da = (a[0] - world[0]).powi(2) + (a[1] - world[1]).powi(2);
203 let db = (b[0] - world[0]).powi(2) + (b[1] - world[1]).powi(2);
204 da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal)
205 })
206 .and_then(|(index, point)| {
207 let distance_sq = (point[0] - world[0]).powi(2) + (point[1] - world[1]).powi(2);
208 (distance_sq <= max_distance_sq).then_some(index)
209 })
210}

Callers 1

updateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected