From 7dc727104eff296af4e4b4029390d5bfdc69e603 Mon Sep 17 00:00:00 2001 From: djmil Date: Sat, 31 Aug 2024 14:52:43 +0200 Subject: [PATCH] Circles spawning area This commit closes #9 --- .gitignore | 3 +++ src/app.rs | 39 +++++++++++++++++++++------------------ src/circle.rs | 18 +++++++++++++++++- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 54ace48..5b7f16e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ target_wasm # https://github.com/lycheeverse/lychee .lycheecache + +# IDE +.vscode diff --git a/src/app.rs b/src/app.rs index 3836060..983f36b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,5 @@ +use std::cmp::min; + use egui::{Color32, Stroke, Vec2}; use rand::Rng; @@ -110,22 +112,6 @@ impl eframe::App for Simulation { ui.add(egui::Slider::new(&mut self.circles_count, 0..=25).text("circles count")); - let diff = (self.circles.len() as i32) - (self.circles_count as i32); - if diff > 0 { - self.circles.truncate(self.circles_count); - } else { - for _ in diff..0 { - self.circles.push(Circle::default()); - self.colors.push( - egui::Color32::from_rgba_premultiplied( - rand::thread_rng().gen_range(64..255), - rand::thread_rng().gen_range(64..255), - rand::thread_rng().gen_range(64..255), - 128) - ); - } - } - let painter = ui.painter(); let (hover_pos, any_down, any_released) = ctx.input(|input| ( @@ -142,7 +128,7 @@ impl eframe::App for Simulation { if any_down { painter.line_segment( [circle.c, circle.c +d], - Stroke{width: 1.0, color: Color32::from_rgb(128, 255, 255)}); + Stroke{width: 3.0, color: Color32::from_rgb(255, 0, 0)}); } if any_released { @@ -152,7 +138,7 @@ impl eframe::App for Simulation { }); } - for i in 0..self.circles_count { + for i in 0..min(self.circles.len(), self.colors.len()) { painter.circle( self.circles[i].c, self.circles[i].r, @@ -172,6 +158,23 @@ impl eframe::App for Simulation { }); }); + + let diff = (self.circles.len() as i32) - (self.circles_count as i32); + if diff > 0 { + self.circles.truncate(self.circles_count); + } else { + for _ in diff..0 { + self.circles.push(Circle::new(&ctx.used_rect())); + self.colors.push( + egui::Color32::from_rgba_premultiplied( + rand::thread_rng().gen_range(64..255), + rand::thread_rng().gen_range(64..255), + rand::thread_rng().gen_range(64..255), + 128) + ); + } + } + for circle in &mut self.circles { circle.apply_force(&ctx.used_rect()); } diff --git a/src/circle.rs b/src/circle.rs index 134ea01..91da5c4 100644 --- a/src/circle.rs +++ b/src/circle.rs @@ -1,4 +1,5 @@ use egui::{Pos2, Rect, Vec2}; +use log::debug; use rand::Rng; pub static FRICTION: f32 = 0.995; @@ -13,7 +14,7 @@ impl Default for Circle { fn default() -> Self { let r = rand::thread_rng().gen_range(20.0..50.0); Self { - r: r, + r, c: Pos2 { x: rand::thread_rng().gen_range(r..400.0-r), y: rand::thread_rng().gen_range(r..400.0-r)}, @@ -50,4 +51,19 @@ impl Circle { } } + pub fn new(bb: &Rect) -> Self { + + debug!("bb: {}..{} {}..{}",bb.left(), bb.right(), bb.top(), bb.bottom() ); + + Self { + r: rand::thread_rng().gen_range(20.0..50.0), + c: Pos2 { + x: rand::thread_rng().gen_range(bb.left()..bb.right()), + y: rand::thread_rng().gen_range(bb.top()..bb.bottom())}, + v: Vec2 { + x: rand::thread_rng().gen_range(-2.0..2.0), + y: rand::thread_rng().gen_range(-2.0..2.0)}, + } + } + }