1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
//! quadtree //! //! Easy Quad Tree (ezquadtree) //! //! This is the first Implementation of a quad tree I have made so there is some room for //! improvement. //! //! A [QuadTree](https://www.i-programmer.info/programming/theory/1679-quadtrees-and-octrees.html) uses the Hilbert curve and can be explained [here](http://blog.notdot.net/2009/11/Damn-Cool-Algorithms-Spatial-indexing-with-Quadtrees-and-Hilbert-Curves) //! ```rust //! use ezquadtree::{Vector, QuadTree, Rectangle}; //! #[derive(Debug, Clone)] //! struct Foo { //! item: String, //! x: u32, //! y: u32, //! } //! //! impl Foo { //! fn new(x: u32, y: u32, item: &str) -> Self { //! Self { item: item.to_string(), x, y } //! } //! } //! //! impl Vector for Foo { //! fn as_point(&self) -> (u32, u32) { //! (self.x, self.y) //! } //! } //! //! impl PartialEq for Foo { //! fn eq(&self, other: &Foo) -> bool { //! self.x == other.x && self.y == other.y //! } //! } //! //! fn main() { //! let old = Foo::new(5, 5, "old"); //! let new = Foo::new(5, 5, "new"); //! //! let (w, h) = (40, 40); //! let bb = Rectangle::new(0, 0, w, h); //! let mut qt = QuadTree::new(bb, 4); //! //! qt.insert(&old); //! qt.insert(&new); //! //! let mut result = Vec::new(); //! //! qt.query(None, &mut |e| result.push(e.clone())); //! //! assert_eq!(result, vec![old.clone()]); //! assert_eq!(qt.len(), 1); //! //! let return_of_replace = qt.replace(&new); //! //! assert_eq!(Some(old.clone()), return_of_replace); //! assert_eq!(qt.len(), 1); //! //! qt.query(None, &mut |inner_item| { //! assert_eq!(inner_item, &new); //! }); //! } //! ``` #[cfg(test)] mod test; mod shape; mod qt; use serde::{Serialize, Deserialize}; pub use shape::Rectangle; pub use qt::{QuadTree, Vector};