1 unstable release

0.1.0 Mar 24, 2024

#883 in Algorithms

Download history 117/week @ 2024-03-22 24/week @ 2024-03-29 6/week @ 2024-04-05

147 downloads per month

Custom license

16KB
346 lines

closest

A simple nearest neighbors implementation in rust.

A rust example, this same example can be run with the following command.

cargo run --example colors
use std::error::Error;

use closest::{Data, KDTree, Point, SquaredEuclideanDistance};

fn main() -> Result<(), Box<dyn Error>> {
    // RGB color coordinates
    let colors = vec![
        Data::new("blue", vec![0., 0., 255.]),
        Data::new("red", vec![255., 0., 0.]),
        Data::new("navy", vec![17., 4., 89.]),
        Data::new("purple", vec![171., 3., 255.]),
        Data::new("light-blue", vec![61., 118., 224.]),
        Data::new("pink", vec![255., 3., 213.]),
        Data::new("yellow", vec![255., 234., 0.]),
        Data::new("green", vec![16., 145., 25.]),
        Data::new("orange", vec![255., 106., 0.]),
    ];
    // Construct the tree from the vector of data points.
    let tree = KDTree::from_vec(colors, 1).unwrap();
    let point = Point::new(vec![237., 139., 69.]); // Light Orange
    let closest_colors =
        tree.get_nearest_neighbors(&point, 2, &SquaredEuclideanDistance::default());
    println!("The nearest colors to light orange.");
    for color in closest_colors {
        println!("color: {}, squared euclidean distance: {}", color.data, color.distance);
    }
    Ok(())
}

// The nearest colors to light orange.
// color: yellow, squared euclidean distance: 14110
// color: orange, squared euclidean distance: 6174

And the equivalent python example.

from nearest import KDTree

colors = [
        ("blue", [0., 0., 255.]),
        ("red", [255., 0., 0.]),
        ("navy", [17., 4., 89.]),
        ("purple", [171., 3., 255.]),
        ("light-blue", [61., 118., 224.]),
        ("pink", [255., 3., 213.]),
        ("yellow", [255., 234., 0.]),
        ("green", [16., 145., 25.]),
        ("orange", [255., 106., 0.]),
    ];
tree = KDTree(colors)
light_orange = [237., 139., 69.]
print(tree.get_nearest_neighbors(light_orange, 2))
#> [(14110.0, 'yellow'), (6174.0, 'orange')]

Dependencies

~0.3–0.8MB
~19K SLoC