‹ pakt09pakt11 ›

pakt10

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

use nannou::prelude::*;

const N: u32 = 1200;

fn main() {
    nannou::app(model).update(update).simple_window(view).size(640, 480).run();
}
struct Model {
    index: f32,
    rows: f32,
    cols: f32,
    row_mod: f32,
    col_mod: f32,
    magic: f32,
}
fn model(_app: &App) -> Model {
    let index = 0.0;
    let rows = 11.0;
    let cols = 12.0;
    let row_mod = 2.0;
    let col_mod = 3.0;
    let magic = 0.0;
    Model { index, rows, cols, row_mod, col_mod, magic }
}
fn update(app: &App, model: &mut Model, _update: Update) {
    model.rows += (model.index * 0.0004).sin();
    model.cols += (model.index * 0.0005).sin();
    model.index = app.time * 60.0;
    model.magic = (model.magic + 0.00002) % 15.0;
}
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.2, 0.2, 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);
    }
    
    let r = model.rows + ((model.index * 0.013).sin() * model.row_mod) + ((model.index * 0.00133).sin() * model.row_mod * 0.56);
    let c = model.cols + ((model.index * 0.022).cos() * model.col_mod) + ((model.index * 0.00122).cos() * model.col_mod * 0.45);
    for i in 0..N {
        let i = i as f32;
        let p = vec2((r + i) % win.w(), ((c + i) * (i % (model.magic + 1.01))) % win.h());
        let d = vec2(10.0 * ((model.index + i) * 0.02).sin(), 10.0 * ((model.index + i) * 0.03).cos());
        draw.scale_axes(vec3(1.0, -1.0, 1.0)).translate(vec3(win.w(), win.h(), 0.0) * -0.5)
        .line().points(p, p + d).color(rgba(1.0, 1.0, 0.2, 0.85));
    }
    
    draw.to_frame(app, &frame).unwrap();
}