changes to #11

main
annieversary 2021-08-25 18:01:34 +02:00
parent c39dec8f61
commit 906e2a66c5
2 changed files with 77 additions and 31 deletions

View File

@ -1,9 +1,12 @@
use nannou::prelude::*; use nannou::prelude::*;
use utils::*;
fn main() { fn main() {
nannou::app(model).update(update).simple_window(view).run(); nannou::app(model).update(update).simple_window(view).run();
} }
// http://paulbourke.net/fractals/clifford/
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
struct Params { struct Params {
a: f32, a: f32,
@ -11,39 +14,65 @@ struct Params {
c: f32, c: f32,
d: 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 { struct Model {
params: Params,
points: Vec<Vec2>, points: Vec<Vec2>,
base_hue: f32, base_hue: f32,
/// params for the attractor
params: Params,
// modulation values for each param
a_mul: f32,
a_add: f32,
b_mul: f32,
b_add: f32,
c_mul: f32,
c_add: f32,
d_mul: f32,
d_add: f32,
// max range for each param
// this would be a const, but acos isn't a const fn
min_a: f32,
max_a: f32,
min_b: f32,
max_b: f32,
min_c: f32,
max_c: f32,
min_d: f32,
max_d: f32,
} }
fn model(_app: &App) -> Model { fn model(_app: &App) -> Model {
let params = Params::new();
dbg!(params);
Model { Model {
params, params: Params {
a: 0.0,
b: 0.0,
c: 0.0,
d: 0.0,
},
a_mul: random_range(0.3, 1.0) * random_range(-1.0, 1.0).signum(),
b_mul: random_range(0.3, 1.0) * random_range(-1.0, 1.0).signum(),
c_mul: random_range(0.3, 1.0) * random_range(-1.0, 1.0).signum(),
d_mul: random_range(0.3, 1.0) * random_range(-1.0, 1.0).signum(),
a_add: random_range(0.3, 2.0),
b_add: random_range(0.3, 2.0),
c_add: random_range(0.3, 2.0),
d_add: random_range(0.3, 2.0),
// magic numbers from here:
// http://paulbourke.net/fractals/clifford/paul_richards/main.cpp
min_a: f32::acos(1.6 / 2.0),
max_a: f32::acos(1.3 / 2.0),
min_b: f32::acos(-0.6 / 2.0),
max_b: f32::acos(1.7 / 2.0),
min_c: f32::acos(-1.2 / 2.0),
max_c: f32::acos(0.5 / 2.0),
min_d: f32::acos(1.6 / 2.0),
max_d: f32::acos(1.4 / 2.0),
base_hue: random_range(0., 360.0), base_hue: random_range(0., 360.0),
points: vec![Vec2::ZERO; 10000], points: vec![Vec2::ZERO; 10000],
} }
@ -58,13 +87,24 @@ fn advance(params: Params, point: Vec2) -> Vec2 {
) )
} }
fn update(_app: &App, model: &mut Model, _update: Update) { fn update(app: &App, model: &mut Model, _update: Update) {
let mut last = model.points.last().unwrap().clone(); let t = app.elapsed_frames() as f32 / 60.0;
// modulate params
model.params.a = map_sin(t * model.a_mul + model.a_add, model.min_a, model.max_a);
model.params.b = map_sin(t * model.b_mul + model.b_add, model.min_b, model.max_b);
model.params.c = map_sin(t * model.c_mul + model.c_add, model.min_c, model.max_c);
model.params.d = map_sin(t * model.d_mul + model.d_add, model.min_d, model.max_d);
// advance all points in list
let mut last = model.points.last().unwrap().clone();
for v in &mut model.points { for v in &mut model.points {
last = advance(model.params, last); last = advance(model.params, last);
*v = last; *v = last;
} }
// move base_hue
model.base_hue += 0.5;
} }
fn view(app: &App, model: &Model, frame: Frame) { fn view(app: &App, model: &Model, frame: Frame) {
@ -74,12 +114,15 @@ fn view(app: &App, model: &Model, frame: Frame) {
if frame.nth() == 0 { if frame.nth() == 0 {
draw.background().color(BLACK); draw.background().color(BLACK);
} else {
let win = app.window_rect();
draw.rect().wh(win.wh()).color(srgba(0., 0.0, 0.0, 0.02));
} }
for &p in &model.points { for &p in &model.points {
let h = random_range(model.base_hue, model.base_hue + 60.0) / 360.0; let h = random_range(model.base_hue, model.base_hue + 120.0) / 360.0;
draw.ellipse() draw.ellipse()
.radius(0.1) .radius(0.2)
.xy(150.0 * p) .xy(150.0 * p)
.color(hsla(h.fract(), 1.0, 0.5, 0.1)); .color(hsla(h.fract(), 1.0, 0.5, 0.1));
} }

View File

@ -1,4 +1,5 @@
use nannou::prelude::*; use nannou::prelude::*;
use utils::*;
fn main() { fn main() {
nannou::app(model).update(update).simple_window(view).run(); nannou::app(model).update(update).simple_window(view).run();
@ -10,7 +11,9 @@ fn model(_app: &App) -> Model {
Model {} Model {}
} }
fn update(_app: &App, _model: &mut Model, _update: Update) {} fn update(app: &App, _model: &mut Model, _update: Update) {
let _t = app.elapsed_frames() as f32 / 60.0;
}
fn view(app: &App, _model: &Model, frame: Frame) { fn view(app: &App, _model: &Model, frame: Frame) {
let _t = frame.nth() as f32 / 60.0; let _t = frame.nth() as f32 / 60.0;