‹ pakt18pakt20 ›

pakt19

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

use nannou::prelude::*;

const N: usize = 200;

fn main() {
    nannou::app(model).update(update).simple_window(view).size(640, 480).run();
}
struct Model {
    index: f32,
    rad: f32,
    bubbles: Vec<Vector3>,
}
fn model(_app: &App) -> Model {
    let index = 0.0;
    let rad = 25.0;
    let mut bubbles = vec![];
    for _i in 0..N {
        bubbles.push(vec3(0.0, 0.0, 1.0));
    }
    Model { index, rad, bubbles }
}
fn update(app: &App, model: &mut Model, _update: Update) {
    model.index = app.time * 60.0;
    let speed = (model.index * 0.02).sin() * 0.01 + 0.065;
    let disorder = (model.index * 0.0022).sin() * 25.0 + 25.0;
    for (i, b) in model.bubbles.iter_mut().enumerate() {
        let i = i as f32;
        let dxyz = (((i * speed * disorder + model.index) * 0.01).sin() * 0.1 + 0.15) * speed;
        b.x = (b.x + dxyz) % 1.0;
        b.y = (b.y + dxyz) % 1.0;
        b.z = (b.z + dxyz) % 1.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(1.0, 0.3, 0.3)));
        
        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, b) in model.bubbles.iter().enumerate() {
        let i = i as f32;
        let theta = ((i / N as f32) + (model.index * 0.0001).sin()) * PI * 2.0;
        let p = pt2(
            (theta + ((model.index * 0.0008).sin() * 0.25 + 1.0) * i).sin() * b.x,
            (theta + ((model.index * 0.0012).sin() * 0.25 + 1.0) * i).cos() * b.y,
        ) * win.wh() * 0.4;
        let r = model.rad * b.z * 2.0;
        draw.scale_axes(vec3(1.0, -1.0, 1.0))
        .ellipse().xy(p).w_h(r, r).color(rgba(1.0, 1.0, 1.0, 1.0 - b.z));
    }
    
    draw.to_frame(app, &frame).unwrap();
}