‹ pakt05pakt07 ›

pakt06

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

use nannou::prelude::*;

const N: u16 = 50;

fn main() {
    nannou::app(model).update(update).simple_window(view).size(640, 480).run();
}
struct Model {
    theta: f32,
    speed: f32,
    spread: f32,
    points: Vec<Vector2>,
}
fn model(_app: &App) -> Model {
    let theta = 0.0;
    let speed = 0.00002;
    let spread = 1.0;
    let mut points = vec![];
    for _i in 0..N {
        points.push(vec2(0.0, 0.0));
    }
    Model { theta, speed, spread, points }
}
fn update(app: &App, model: &mut Model, _update: Update) {
    let win = app.window_rect();
    for p in model.points.iter_mut() {
        let x = p.x / win.w();
        let y = p.y / win.h();
        p.x = ((model.theta.cos() * x) - (model.theta.sin() * y)) * win.w() + model.spread;
        p.y = ((model.theta.cos() * y) + (model.theta.sin() * 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);
    for p in model.points.iter() {
        draw.ellipse().xy(*p).radius(50.0).stroke_weight(1.0).stroke(WHITE).color(RED);
    }
    
    draw.to_frame(app, &frame).unwrap();
}