From 45fbe5a37fab71f6320bed7cdfa3d0591b5ab442 Mon Sep 17 00:00:00 2001 From: annieversary Date: Wed, 1 Sep 2021 20:01:22 +0200 Subject: [PATCH] subtitled #12 --- Cargo.lock | 26 +++++++++++++ crates/subtitled12/Cargo.toml | 9 +++++ crates/subtitled12/src/main.rs | 67 ++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 crates/subtitled12/Cargo.toml create mode 100644 crates/subtitled12/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index e60337e..086a7cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1188,6 +1188,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "libm" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" + [[package]] name = "line_drawing" version = "0.7.0" @@ -1649,6 +1655,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" dependencies = [ "autocfg", + "libm", ] [[package]] @@ -2027,6 +2034,16 @@ dependencies = [ "getrandom 0.2.3", ] +[[package]] +name = "rand_distr" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "051b398806e42b9cd04ad9ec8f81e355d0a382c543ac6672c62f5a5b452ef142" +dependencies = [ + "num-traits", + "rand 0.8.4", +] + [[package]] name = "rand_hc" version = "0.2.0" @@ -2364,6 +2381,15 @@ dependencies = [ "utils", ] +[[package]] +name = "subtitled12" +version = "0.1.0" +dependencies = [ + "nannou", + "rand_distr", + "utils", +] + [[package]] name = "subtitled2" version = "0.1.0" diff --git a/crates/subtitled12/Cargo.toml b/crates/subtitled12/Cargo.toml new file mode 100644 index 0000000..0b55836 --- /dev/null +++ b/crates/subtitled12/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "subtitled12" +version = "0.1.0" +edition = "2018" + +[dependencies] +nannou = "0.17" +rand_distr = "0.4.1" +utils = { path = "../utils" } diff --git a/crates/subtitled12/src/main.rs b/crates/subtitled12/src/main.rs new file mode 100644 index 0000000..43c3d7a --- /dev/null +++ b/crates/subtitled12/src/main.rs @@ -0,0 +1,67 @@ +use nannou::prelude::*; +use nannou::rand::prelude::*; +use rand_distr::Normal; +use utils::*; + +fn main() { + nannou::app(model).update(update).simple_window(view).run(); +} + +struct Model { + lines: Vec>, +} + +fn lines(max_width: f32) -> Vec> { + let normal = Normal::new(0.0, 0.2).unwrap(); + + (0..20) + .map(|_| { + let mut line = vec![Vec2::Y]; + + // generate the y coordinates for all points + let n = random_range(4, 12); + let mut ys = (0..n).map(|_| random_range(-1.0, 1.0)).collect::>(); + ys.sort_by(|a, b| b.partial_cmp(a).unwrap()); + + // make the actual points and add them to the vec + for y in ys { + let w = map_range(1.0 - y.abs(), 0.0, 1.0, 0.01, max_width); + let x = nannou::rand::thread_rng().sample::(normal) * w; + line.push(vec2(x, y)); + } + + line.push(-Vec2::Y); + + line + }) + .collect() +} + +fn model(_app: &App) -> Model { + Model { lines: lines(0.0) } +} + +fn update(app: &App, model: &mut Model, _update: Update) { + let t = app.elapsed_frames() as f32 / 60.0; + + if app.elapsed_frames() % 2 == 0 { + model.lines = lines(map_sin(t, 0.1, 1.0)); + } +} + +fn view(app: &App, model: &Model, frame: Frame) { + let _t = frame.nth() as f32 / 60.0; + + let draw = app.draw(); + draw.background().color(BLACK); + + for line in &model.lines { + draw.polyline() + .stroke_weight(1.0) + .points(line.iter().map(|v| *v * 300.0)) + .color(PINK); + } + + draw.to_frame(app, &frame).unwrap(); + utils::record::record(app, &frame); +}