‹ pakt20pakt22 ›

pakt21

See /software/p5js/pakt-februari/pakt21/ for a JavaScript version and /software/supercollider/februari-pakt/pakt21/ for accompanying sound code.

use nannou::prelude::*;

const N: usize = 150;

fn main() {
    nannou::app(model).update(update).simple_window(view).size(640, 480).run();
}
struct Model {
    index: f32,
    pos: Vec<Vector2>,
    last: Vec<Vector2>,
    cols: Vec<Rgb>,
}
fn model(_app: &App) -> Model {
    let index = 0.0;
    let mut pos = vec![];
    let mut last = vec![];
    let mut cols = vec![];
    for i in 0..N {
        let t = i as f32 / N as f32 * PI * 2.0;
        pos.push(vec2(0.0, 0.0));
        last.push(vec2(0.0, 0.0));
        cols.push(rgb(t.sin() * 0.5 + 0.5, (t + PI).sin() * 0.5 + 0.5, 1.0));
    }
    Model { index, pos, last, cols }
}
fn update(app: &App, model: &mut Model, _update: Update) {
    let win = app.window_rect();
    let index = app.time * 60.0;
    for i in 0..N {
        let j = i as f32;
        let jj = j.sin() * (j * 2.0).sin();
        model.last[i] = model.pos[i];
        let x = ((j * (index * 0.0126 + jj).sin() * (index * 0.0031).sin() + index) * 0.074).sin();
        let y = ((j * (index * 0.0152 + jj).sin() * (index * 0.0021).sin() + index) * 0.064).cos();
        model.pos[i] = vec2(x, y) * win.wh() * 0.45 + (win.wh() * 0.5);
    }
    model.index = index;
}
fn view(app: &App, model: &Model, frame: Frame) {
    let draw = app.draw();
    let win = app.window_rect();
    
    if frame.nth() == 0 {
        draw.background().color(BLACK);
    } else {
        draw.rect().wh(win.wh()).color(rgba(0.0, 0.0, 0.0, 0.05));
        for i in 0..N {
            let r = (model.pos[i].x - model.last[i].x + model.pos[i].y - model.last[i].y).abs();
            draw.scale_axes(vec3(1.0, -1.0, 1.0)).translate(vec3(win.w(), win.h(), 0.0) * -0.5)
            .ellipse().xy(model.pos[i]).w_h(r, r).color(model.cols[i]);
        }
    }
    
    draw.to_frame(app, &frame).unwrap();
}