random particles
parent
da25ffb0bf
commit
6d270344cf
36
src/main.rs
36
src/main.rs
|
@ -1,6 +1,7 @@
|
||||||
mod drawable;
|
mod drawable;
|
||||||
mod particle;
|
mod particle;
|
||||||
|
|
||||||
|
use drawable::Drawable;
|
||||||
use nannou::prelude::*;
|
use nannou::prelude::*;
|
||||||
use particle::Particle;
|
use particle::Particle;
|
||||||
|
|
||||||
|
@ -10,6 +11,19 @@ struct Model {
|
||||||
particles: Vec<Particle>,
|
particles: Vec<Particle>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fill_particles(app: &App, particles: &mut Vec<Particle>) {
|
||||||
|
let boundary = app.window_rect();
|
||||||
|
|
||||||
|
for _ in 0..200 {
|
||||||
|
let x = random_range(boundary.left(), boundary.right());
|
||||||
|
let y = random_range(boundary.top(), boundary.bottom());
|
||||||
|
|
||||||
|
let particle = Particle::new(Vec2::new(x, y));
|
||||||
|
|
||||||
|
particles.push(particle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn model(app: &App) -> Model {
|
fn model(app: &App) -> Model {
|
||||||
// Create a new window! Store the ID so we can refer to it later.
|
// Create a new window! Store the ID so we can refer to it later.
|
||||||
let _window = app
|
let _window = app
|
||||||
|
@ -21,34 +35,34 @@ fn model(app: &App) -> Model {
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let boundary = app.window_rect();
|
|
||||||
let mut particles = Vec::new();
|
let mut particles = Vec::new();
|
||||||
|
|
||||||
for _ in 0..100 {
|
fill_particles(app, &mut particles);
|
||||||
let x = random_range(boundary.left(), boundary.right());
|
|
||||||
let y = random_range(boundary.top(), boundary.bottom());
|
|
||||||
let particle = Particle::new(Vec2::new(x, y));
|
|
||||||
particles.push(particle);
|
|
||||||
}
|
|
||||||
|
|
||||||
Model { _window, particles }
|
Model { _window, particles }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle events related to the window and update the model if necessary
|
// Handle events related to the window and update the model if necessary
|
||||||
fn event(_app: &App, _model: &mut Model, event: WindowEvent) {
|
fn event(app: &App, model: &mut Model, event: WindowEvent) {
|
||||||
if let KeyReleased(Key::Escape) = event {
|
if let KeyReleased(Key::Escape) = event {
|
||||||
_app.quit()
|
app.quit()
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Resized(_) = event {
|
||||||
|
model.particles.clear();
|
||||||
|
|
||||||
|
fill_particles(app, &mut model.particles);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{:?}", event);
|
println!("{:?}", event);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the state of your `Model` into the given `Frame` here.
|
// Draw the state of your `Model` into the given `Frame` here.
|
||||||
fn view(app: &App, _model: &Model, frame: Frame) {
|
fn view(app: &App, model: &Model, frame: Frame) {
|
||||||
let draw = app.draw();
|
let draw = app.draw();
|
||||||
draw.background().color(CORNFLOWERBLUE);
|
draw.background().color(CORNFLOWERBLUE);
|
||||||
|
|
||||||
draw.ellipse().color(STEELBLUE).x_y(200.0, -100.0);
|
model.particles.draw(&draw);
|
||||||
|
|
||||||
// I don't think there is even a fail condition in this function, but it returns a result?
|
// I don't think there is even a fail condition in this function, but it returns a result?
|
||||||
draw.to_frame(app, &frame).unwrap();
|
draw.to_frame(app, &frame).unwrap();
|
||||||
|
|
|
@ -30,6 +30,10 @@ impl Default for Particle {
|
||||||
|
|
||||||
impl Drawable for Particle {
|
impl Drawable for Particle {
|
||||||
fn draw(&self, draw: &Draw) {
|
fn draw(&self, draw: &Draw) {
|
||||||
draw.ellipse().color(RED).radius(self.radius).xy(self.pos);
|
draw.ellipse()
|
||||||
|
.color(RED)
|
||||||
|
.stroke_weight(1.0)
|
||||||
|
.radius(self.radius)
|
||||||
|
.xy(self.pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue