From 808fc8e90bfbf1478752b9e20270e6bd9c4434f2 Mon Sep 17 00:00:00 2001 From: annieversary Date: Wed, 18 Aug 2021 23:31:17 +0200 Subject: [PATCH] sketch #5 --- Cargo.lock | 8 +++ crates/subtitled5/Cargo.toml | 8 +++ crates/subtitled5/src/main.rs | 44 +++++++++++++ crates/subtitled5/src/shapes.rs | 106 ++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) create mode 100644 crates/subtitled5/Cargo.toml create mode 100644 crates/subtitled5/src/main.rs create mode 100644 crates/subtitled5/src/shapes.rs diff --git a/Cargo.lock b/Cargo.lock index 032ecc4..4cad3da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2372,6 +2372,14 @@ dependencies = [ "utils", ] +[[package]] +name = "subtitled5" +version = "0.1.0" +dependencies = [ + "nannou", + "utils", +] + [[package]] name = "syn" version = "1.0.74" diff --git a/crates/subtitled5/Cargo.toml b/crates/subtitled5/Cargo.toml new file mode 100644 index 0000000..ca11bf2 --- /dev/null +++ b/crates/subtitled5/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "subtitled5" +version = "0.1.0" +edition = "2018" + +[dependencies] +nannou = "0.17" +utils = { path = "../utils" } diff --git a/crates/subtitled5/src/main.rs b/crates/subtitled5/src/main.rs new file mode 100644 index 0000000..b88b78a --- /dev/null +++ b/crates/subtitled5/src/main.rs @@ -0,0 +1,44 @@ +use nannou::prelude::*; + +mod shapes; +use shapes::*; + +fn main() { + nannou::app(model).update(update).simple_window(view).run(); +} + +struct Model { + shape: Shapes, +} + +fn model(_app: &App) -> Model { + Model { + shape: Shapes::new(), + } +} + +fn update(app: &App, model: &mut Model, _update: Update) { + if app.elapsed_frames() % 60 == 0 { + model.shape = Shapes::new(); + } +} + +fn view(app: &App, model: &Model, frame: Frame) { + let _t = frame.nth() as f32 / 60.0; + + let draw = app.draw(); + if frame.nth() == 0 { + draw.background().color(BLACK); + } else if frame.nth() % 60 > 40 { + let win = app.window_rect(); + draw.rect().wh(win.wh()).color(srgba(0.0, 0.0, 0.0, 0.2)); + } + + if frame.nth() % 60 == 0 { + model.shape.draw(draw.clone()); + } + + draw.to_frame(app, &frame).unwrap(); + + utils::record::record(app, &frame); +} diff --git a/crates/subtitled5/src/shapes.rs b/crates/subtitled5/src/shapes.rs new file mode 100644 index 0000000..7f30228 --- /dev/null +++ b/crates/subtitled5/src/shapes.rs @@ -0,0 +1,106 @@ +use nannou::prelude::*; + +pub struct Shapes(Vec); +impl Shapes { + pub fn draw(&self, draw: Draw) { + for shape in &self.0 { + match shape { + Shape::Triangle { center, radius } => { + let points = vec![vec2(0., 1.), vec2(0.866, -0.5), vec2(-0.866, -0.5)] + .into_iter() + .map(|v| v * *radius + *center) + .collect::>(); + draw.polyline().weight(3.).points_closed(points).color(PINK); + } + Shape::Circle { center, radius } => { + draw.ellipse() + .radius(*radius) + .no_fill() + .stroke_weight(3.) + .xy(*center) + .stroke_color(PINK); + } + Shape::Line { center, radius } => {} + } + } + } + + pub fn new() -> Self { + let root = Shape::random(Vec2::ZERO, 100.0); + + let mut vec = vec![]; + + for _ in 0..3 { + let (c, r) = root.get_random_point_distance(); + let s = Shape::random(c, r); + + let children = random_range(0, 3); + for _ in 0..children { + let (c, r) = s.get_random_point_distance(); + let s = Shape::random(c, r); + vec.push(s); + } + + vec.push(s); + } + vec.push(root); + + Self(vec) + } +} + +pub enum Shape { + Triangle { center: Vec2, radius: f32 }, + Circle { center: Vec2, radius: f32 }, + Line { center: Vec2, radius: f32 }, +} +impl Shape { + fn random(center: Vec2, radius: f32) -> Self { + match random_range(0, 3) { + 0 => Self::tri(center, radius), + 1 => Self::line(center, radius), + _ => Self::circ(center, radius), + } + } + + fn tri(center: Vec2, radius: f32) -> Self { + Self::Triangle { center, radius } + } + fn circ(center: Vec2, radius: f32) -> Self { + Self::Circle { center, radius } + } + fn line(center: Vec2, radius: f32) -> Self { + Self::Line { center, radius } + } + + fn get_random_point_distance(&self) -> (Vec2, f32) { + match self { + Shape::Triangle { center, radius } => { + let points = vec![vec2(0., 1.), vec2(0.866, -0.5), vec2(-0.866, -0.5)] + .into_iter() + .map(|v| v * *radius + *center) + .collect::>(); + + let denom = random_range(1.0, 5.0).floor(); + (points[random_range(0, points.len())], radius / denom) + } + Shape::Circle { center, radius } => { + let point = match random_range(0, 4) { + 0 => *center + Vec2::X * *radius, + 1 => *center - Vec2::X * *radius, + 2 => *center + Vec2::Y * *radius, + _ => *center - Vec2::Y * *radius, + }; + + let denom = random_range(1.0, 5.0).floor(); + (point, radius / denom) + } + Shape::Line { center, radius } => { + let point = todo!("one of the two vertices"); + + let denom = random_range(1.0, 5.0).floor(); + (point, radius / denom) + } + } + } +}