decluster/decluster.rs
1//! An iterative algorithm that searches for a set of points where each point is separated from all others by at least the minimum specified distance.
2use crate::point::Point;
3
4/**
5 The decluster algorithm cycles through the points set calculating the distance between each pair of points. If that distance is greater than the `min_distance` then the point
6 is marked for removal by the vec's `retain` function.
7
8Note that once a point is marked for removal there is no need to process it further.
9
10# Arguments
11 * points - the vec of Point structures
12 * min_distance - the minimum distance allowed between points
13*/
14pub fn decluster(points: &mut Vec<Point>, min_distance: f64) {
15 for i in 0..points.len() {
16 let Point {
17 x: x1,
18 y: y1,
19 retain,
20 } = points[i];
21 if !retain {
22 continue;
23 };
24 for j in i + 1..points.len() {
25 let Point {
26 x: x2,
27 y: y2,
28 retain,
29 } = points[j];
30 if !retain {
31 continue;
32 }
33 let d = ((x1 - x2).powi(2) + (y1 - y2).powi(2)).sqrt();
34 if d <= min_distance {
35 points[j].retain = false;
36 }
37 }
38 }
39 points.retain(|p| p.retain);
40}