‹ pakt08pakt10 ›

pakt09

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

use nannou::prelude::*;

const N: u32 = 1000;

fn main() {
    nannou::app(model).update(update).simple_window(view).size(640, 480).run();
}
struct Model {
    index: f32,
    m: u32,
    rows: f32,
    cols: f32,
    magic: f32,
}
fn model(_app: &App) -> Model {
    let index = 0.0;
    let m = 0;
    let rows = 3.0;
    let cols = 4.0;
    let magic = 50.0;
    Model { index, m, rows, cols, magic }
}
fn update(app: &App, model: &mut Model, _update: Update) {
    model.index = app.time * 60.0;
    if model.m < N {
        model.m += 1;
    }
    model.magic += (model.index * 0.001).sin() * 0.01;
    model.rows += (model.index * 0.0004).sin();
    model.cols += (model.index * 0.0005).sin();
}
fn view(app: &App, model: &Model, frame: Frame) {
    let draw = app.draw();
    let win = app.window_rect();
    
    let rad = win.h();
    for angle in 0..360 {
        let angle = angle as f32;
        let mut points = Vec::new();
        points.push((pt2(0.0, 0.0), rgb(0.5, 0.5, 0.5)));
        
        let vx = angle.to_radians().cos();
        let vy = angle.to_radians().sin();
        points.push((pt2(vx, vy) * rad, rgb(0.0, 0.0, 0.0)));
        
        let next_vx = (angle + 1.0).to_radians().cos();
        let next_vy = (angle + 1.0).to_radians().sin();
        points.push((pt2(next_vx, next_vy) * rad, rgb(0.0, 0.0, 0.0)));
        
        draw.polygon().points_colored(points);
    }
    
    for i in 0..model.m {
        let i = i as f32;
        let p = pt2((model.rows + i) % win.w(), (model.cols + i) * (i % model.magic) % win.h());
        let d = pt2(8.0 * ((model.index + i) * 0.012).sin(), 8.0 * ((model.index + i) * 0.011).cos());
        draw.scale_axes(vec3(1.0, -1.0, 1.0)).translate(vec3(win.w(), win.h(), 0.0) * -0.5)
        .rect().xy(p).wh(d).color(rgba(1.0, 1.0, 1.0, 0.85));
    }
    
    draw.to_frame(app, &frame).unwrap();
}