‹ pakt07pakt09 ›

pakt08

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

use nannou::prelude::*;

const N: u16 = 25;

fn main() {
    nannou::app(model).update(update).simple_window(view).size(640, 480).run();
}
struct Model {
    d: f32,
    theta: f32,
    speed: f32,
    spread: f32,
    points: Vec<Point2>,
}
fn model(_app: &App) -> Model {
    let d = 0.0001;
    let theta = 0.0;
    let speed = 0.00002;
    let spread = 2.0;
    let mut points = vec![];
    for _i in 0..N {
        points.push(pt2(0.0, 0.0));
    }
    Model { d, theta, speed, spread, points }
}
fn update(app: &App, model: &mut Model, _update: Update) {
    let win = app.window_rect();
    for (i, p) in model.points.iter_mut().enumerate() {
        let i = i as f32;
        let c = (model.theta + (i * model.d)).cos();
        let s = (model.theta + (i * model.d)).sin();
        let x = p.x / win.w();
        let y = p.y / win.h();
        p.x = ((c * x) - (s * y)) * win.w() + model.spread;
        p.y = ((c * y) + (s * x)) * win.h() + model.spread;
        model.theta += model.speed;
    }
}
fn view(app: &App, model: &Model, frame: Frame) {
    let draw = app.draw();
    draw.background().color(BLACK);
    let mut builder = geom::path::Builder::new();
    for p in model.points.iter() {
        builder = builder.line_to(*p);
    }
    let path = builder.build();
    draw.scale_axes(vec3(1.0, -1.0, 1.0))
    .color_blend(BLEND_SUBTRACT).path().fill().color(RED).events(path.iter());
    
    draw.to_frame(app, &frame).unwrap();
}