Skip to content

Latest commit

 

History

History
48 lines (32 loc) · 1.34 KB

nearest_neighbor_queries.md

File metadata and controls

48 lines (32 loc) · 1.34 KB

Nearest Neighbor Queries

To find the nearest neighbor of a given point, we can use nearest_neighbor. Here is an example:

use rstar::RTree;

fn main() {
    let tree = RTree::bulk_load(vec![(0, 0), (1, 2), (8, 5)]);

    println!("{:?}", tree.nearest_neighbor(&(3, 3)));
}

Output:

Some((1, 2))

The distances of points are computed by PointDistance::distance_2.

The method nearest_neighbor returns at most one point. If it is possible to have more than one nearest neighbor, we can use nearest_neighbors. For example:

use rstar::RTree;

fn main() {
    let tree = RTree::bulk_load(vec![(1, 1), (3, 3)]);

    println!("{:?}", tree.nearest_neighbors(&(2, 2)));
}

Output:

[(1, 1), (3, 3)]

Calling nearest_neighbor in the example will only return one of (1, 1) and (3, 3).

➡️ Next: Nearest Neighbor Iterations

📘 Back: Table of contents