From c39dec8f61e6e8d02cf4b8e8693a780e3babedc3 Mon Sep 17 00:00:00 2001 From: annieversary Date: Tue, 24 Aug 2021 23:54:06 +0200 Subject: [PATCH] subtitled #10 --- Cargo.lock | 8 +++ crates/subtitled10/Cargo.toml | 8 +++ crates/subtitled10/src/main.rs | 89 ++++++++++++++++++++++++++++++++++ crates/template/src/main.rs | 3 ++ 4 files changed, 108 insertions(+) create mode 100644 crates/subtitled10/Cargo.toml create mode 100644 crates/subtitled10/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 2c51002..cb5844d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2348,6 +2348,14 @@ dependencies = [ "utils", ] +[[package]] +name = "subtitled10" +version = "0.1.0" +dependencies = [ + "nannou", + "utils", +] + [[package]] name = "subtitled2" version = "0.1.0" diff --git a/crates/subtitled10/Cargo.toml b/crates/subtitled10/Cargo.toml new file mode 100644 index 0000000..a2c72b9 --- /dev/null +++ b/crates/subtitled10/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "subtitled10" +version = "0.1.0" +edition = "2018" + +[dependencies] +nannou = "0.17" +utils = { path = "../utils" } diff --git a/crates/subtitled10/src/main.rs b/crates/subtitled10/src/main.rs new file mode 100644 index 0000000..bc3b309 --- /dev/null +++ b/crates/subtitled10/src/main.rs @@ -0,0 +1,89 @@ +use nannou::prelude::*; + +fn main() { + nannou::app(model).update(update).simple_window(view).run(); +} + +#[derive(Clone, Copy, Debug)] +struct Params { + a: f32, + b: f32, + c: f32, + d: f32, +} + +impl Params { + fn new() -> Self { + // magic numbers from here: + // http://paulbourke.net/fractals/clifford/paul_richards/main.cpp + let min_a = f32::acos(1.6 / 2.0); + let max_a = f32::acos(1.3 / 2.0); + let min_b = f32::acos(-0.6 / 2.0); + let max_b = f32::acos(1.7 / 2.0); + let min_c = f32::acos(-1.2 / 2.0); + let max_c = f32::acos(0.5 / 2.0); + let min_d = f32::acos(1.6 / 2.0); + let max_d = f32::acos(1.4 / 2.0); + + Self { + a: random_range(min_a, max_a), + b: random_range(min_b, max_b), + c: random_range(min_c, max_c), + d: random_range(min_d, max_d), + } + } +} +struct Model { + params: Params, + points: Vec, + base_hue: f32, +} + +fn model(_app: &App) -> Model { + let params = Params::new(); + dbg!(params); + Model { + params, + base_hue: random_range(0., 360.0), + points: vec![Vec2::ZERO; 10000], + } +} + +fn advance(params: Params, point: Vec2) -> Vec2 { + // xn+1 = sin(a yn) + c cos(a xn) + // yn+1 = sin(b xn) + d cos(b yn) + vec2( + (params.a * point.y).sin() + (params.a * point.x).cos() * params.c, + (params.b * point.x).sin() + (params.b * point.y).cos() * params.d, + ) +} + +fn update(_app: &App, model: &mut Model, _update: Update) { + let mut last = model.points.last().unwrap().clone(); + + for v in &mut model.points { + last = advance(model.params, last); + *v = last; + } +} + +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); + } + + for &p in &model.points { + let h = random_range(model.base_hue, model.base_hue + 60.0) / 360.0; + draw.ellipse() + .radius(0.1) + .xy(150.0 * p) + .color(hsla(h.fract(), 1.0, 0.5, 0.1)); + } + + draw.to_frame(app, &frame).unwrap(); + utils::record::record(app, &frame); +} diff --git a/crates/template/src/main.rs b/crates/template/src/main.rs index 2817ac0..f7c1638 100644 --- a/crates/template/src/main.rs +++ b/crates/template/src/main.rs @@ -17,6 +17,9 @@ fn view(app: &App, _model: &Model, frame: Frame) { let draw = app.draw(); draw.background().color(PLUM); + draw.ellipse().color(STEELBLUE); + draw.to_frame(app, &frame).unwrap(); + utils::record::record(app, &frame); }