diff --git a/visualize/src/lib.rs b/visualize/src/lib.rs index 3be6026..36f4f32 100644 --- a/visualize/src/lib.rs +++ b/visualize/src/lib.rs @@ -1,2 +1,2 @@ pub(crate) mod model; -pub use model::{Model, Technique}; +pub use model::Model; diff --git a/visualize/src/main.rs b/visualize/src/main.rs index 7085e9d..8d1685d 100644 --- a/visualize/src/main.rs +++ b/visualize/src/main.rs @@ -27,7 +27,7 @@ fn create_model(app: &App) -> Model { fn raw_window_event(app: &App, model: &mut Model, event: &WindowEvent) { // Let egui handle things like keyboard and mouse input. model.egui.handle_raw_event(event); - model.mouse_position = None; + model.set_mouse_position(None); // Get the egui context let ctx = model.egui.ctx(); @@ -35,10 +35,11 @@ fn raw_window_event(app: &App, model: &mut Model, event: &WindowEvent) { // Handle mouse input if let WindowEvent::MouseInput { state, button, .. } = event { let point = Point::new(&[app.mouse.x, app.mouse.y]); - model.mouse_position = Some(point); - if !ctx.wants_pointer_input() { - // Only add points if egui is not handling the pointer input + let wants_pointer_input = ctx.wants_pointer_input(); + model.set_mouse_position(Some(point)); + // Only add points if egui is not handling the pointer input + if !wants_pointer_input { // Left-click (adding point manually) if *button == MouseButton::Left && *state == ElementState::Pressed { model.add_point(point); @@ -50,7 +51,7 @@ fn raw_window_event(app: &App, model: &mut Model, event: &WindowEvent) { } } else if let WindowEvent::CursorMoved { .. } = event { let point = Point::new(&[app.mouse.x, app.mouse.y]); - model.mouse_position = Some(point); + model.set_mouse_position(Some(point)); } } @@ -66,7 +67,7 @@ fn view(app: &App, model: &Model, frame: Frame) { let draw = app.draw(); // Draw the points, mouse query circle and QuadTree rectangles - model.draw_app(&draw, &model.points); + model.draw_app(&draw); // Draw FPS counter let fps = app.fps(); diff --git a/visualize/src/model.rs b/visualize/src/model.rs index ffa63fc..0b25dd8 100644 --- a/visualize/src/model.rs +++ b/visualize/src/model.rs @@ -20,19 +20,11 @@ const RADIUS: f32 = 100.0; const DOT_SIZE: f32 = 7.5; type Point2D = Point<2>; -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Technique { - /// Use comparision of coordinates to determine distance - Cartesian, - /// Use a quadtree to determine distance - Quadtree, -} - pub struct Model { pub egui: Egui, - pub points: Vec, - pub mouse_position: Option, - pub region: Region<2>, + points: Vec, + mouse_position: Option, + region: Region<2>, rng: StdRng, } @@ -49,6 +41,10 @@ impl Model { }) } + pub fn set_mouse_position(&mut self, position: Option) { + self.mouse_position = position; + } + pub fn add_point(&mut self, point: Point2D) { self.points.push(point); } @@ -71,18 +67,7 @@ impl Model { qt } - fn points_within_distance( - &self, - qt: &QuadTree<2, Point2D>, - center: &Point2D, - distance: f64, - ) -> HashSet { - // Query - let distance_squared = center.to_distance_based_query(distance); - qt.query(&distance_squared).cloned().collect() - } - - pub fn draw_app(&self, draw: &Draw, points: &[Point2D]) { + pub fn draw_app(&self, draw: &Draw) { let qt = self.quadtree(); // Draw circle around the mouse position if it exists, and find points within that circle. let points_inside_query = match &self.mouse_position { @@ -95,13 +80,14 @@ impl Model { .stroke_weight(2.0) .no_fill(); - self.points_within_distance(&qt, &Point::new(coords), RADIUS as f64) + let distance_query = &Point::new(coords).to_distance_based_query(RADIUS as f64); + qt.query(distance_query).cloned().collect() } None => HashSet::new(), }; // Draw the points, colouring them based on whether they are inside the query circle. - for point in points { + for point in &self.points { let coords = point.dimension_values(); let color = if points_inside_query.contains(point) { GREEN