| 81 | } |
| 82 | |
| 83 | fn snap_coord(to_snap: Coord, index: &mut GeomKdTree, tolerance: f64) -> Coord { |
| 84 | let query = [to_snap.x, to_snap.y]; |
| 85 | let neighbors = index.within(&query, tolerance, &squared_euclidean).unwrap(); |
| 86 | |
| 87 | if !neighbors.is_empty() { |
| 88 | let (mut _distance, mut found_coords) = neighbors[0]; |
| 89 | // If we found ourselves, snap to the next closest point |
| 90 | if found_coords == &to_snap && neighbors.len() > 1 { |
| 91 | // The next closest point |
| 92 | (_distance, found_coords) = neighbors[1]; |
| 93 | } |
| 94 | |
| 95 | // Remove the point that we snapped to, so that future snaps don't find it again |
| 96 | let snapped_coord = *found_coords; |
| 97 | index.remove(&query, &to_snap).unwrap(); |
| 98 | |
| 99 | snapped_coord |
| 100 | } else { |
| 101 | to_snap |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn snap_coord_grid(coord: Coord, tolerance: f64) -> Coord { |
| 106 | Coord { |