stars/src/main.rs

47 lines
1.1 KiB
Rust

use rand::{
distributions::{Distribution, Uniform},
thread_rng,
};
use rstar::{PointDistance, RTree, RTreeObject, AABB};
struct MyThing {
field: String,
x: f32,
y: f32,
}
impl RTreeObject for MyThing {
type Envelope = AABB<[f32; 2]>;
fn envelope(&self) -> Self::Envelope {
AABB::from_point([self.x, self.y])
}
}
impl PointDistance for MyThing {
fn distance_2(&self, point: &[f32; 2]) -> f32 {
let dx = self.x - point[0];
let dy = self.y - point[1];
dx * dx - dy * dy
}
}
fn main() {
let mut rng = thread_rng();
let distribution = Uniform::from(1..=100);
let mut tree: RTree<MyThing> = RTree::new();
for _ in 1..=50 {
let x = distribution.sample(&mut rng) as f32;
let y = distribution.sample(&mut rng) as f32;
let thing = MyThing {
field: String::from("hi"),
x,
y
};
if tree.locate_within_distance([x, y], 10 as f32).into_iter().count() == 0 {
tree.insert(thing);
println!("{} {}", x, y)
}
}
}