58 lines
1007 B
Rust
58 lines
1007 B
Rust
extern crate num;
|
|
extern crate kdtree;
|
|
|
|
use std::fmt;
|
|
use std::ops::Index;
|
|
|
|
use kdtree::Zero;
|
|
use kdtree::KdTree;
|
|
|
|
#[derive(Copy, Clone)]
|
|
struct Vector2 {
|
|
data: [f64; 2],
|
|
}
|
|
|
|
impl Vector2 {
|
|
pub fn new(x: f64, y: f64) -> Vector2 {
|
|
Vector2 {
|
|
data: [x, y],
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Index<usize> for Vector2 {
|
|
type Output = f64;
|
|
fn index(&self, index: usize) -> &f64 {
|
|
&self.data[index]
|
|
}
|
|
}
|
|
|
|
impl Zero for Vector2 {
|
|
fn zero() -> Vector2 {
|
|
Vector2::new(0.0, 0.0)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Vector2 {
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(formatter, "({}, {})", self[0], self[1])
|
|
}
|
|
}
|
|
|
|
|
|
fn main() {
|
|
let mut points = vec![
|
|
Vector2::new(2.0, 3.0),
|
|
Vector2::new(5.0, 4.0),
|
|
Vector2::new(9.0, 6.0),
|
|
Vector2::new(4.0, 7.0),
|
|
Vector2::new(8.0, 1.0),
|
|
Vector2::new(7.0, 2.0),
|
|
];
|
|
|
|
let kdtree = KdTree::new(&mut points, 2);
|
|
|
|
println!("{}", kdtree);
|
|
|
|
}
|