We can use nearest_neighbor_iter to obtain a sequence of points sorted by distances to a given point. Here is an example:
use rstar::RTree;
fn main() {
let tree = RTree::bulk_load(vec![(0, 0), (1, 2), (8, 5)]);
for point in tree.nearest_neighbor_iter(&(3, 3)) {
println!("{:?}", point);
}
}
Output:
(1, 2)
(0, 0)
(8, 5)
To obtain the distances associated with the output points, we can use nearest_neighbor_iter_with_distance_2.
use rstar::RTree;
fn main() {
let tree = RTree::bulk_load(vec![(0, 0), (1, 2), (8, 5)]);
for point in tree.nearest_neighbor_iter_with_distance_2(&(3, 3)) {
println!("{:?}", point);
}
}
Output:
((1, 2), 5)
((0, 0), 18)
((8, 5), 29)
The distances are calculated by PointDistance::distance_2.
➡️ Next: Range Queries
📘 Back: Table of contents