Circles spawning area

This commit closes #9
This commit is contained in:
djmil 2024-08-31 14:52:43 +02:00
parent cdb3f26532
commit 7dc727104e
3 changed files with 41 additions and 19 deletions

3
.gitignore vendored
View File

@ -11,3 +11,6 @@ target_wasm
# https://github.com/lycheeverse/lychee
.lycheecache
# IDE
.vscode

View File

@ -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());
}

View File

@ -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)},
}
}
}