‹ pakt01pakt03 ›

pakt02

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

use nannou::prelude::*;

const N: u16 = 50;

fn main() {
    nannou::sketch(view).size(640, 480).run();
}

fn view(app: &App, frame: Frame) {
    let draw = app.draw();
    let win = app.window_rect();
    
    let index = app.time * 60.0;
    let x = win.w() * -0.5 + 10.0 + ((index * 0.016).sin() * 2.0);
    let y = (index * 0.010).sin() * 50.0;
    let x3 = win.w() * 0.5 - 10.0 + ((index * 0.012).sin() * 2.0);
    let y3 = (index * 0.014).sin() * 50.0;
    let spread = map_range((index * 0.0005).sin(), -1.0, 1.0, 0.5, 1.5);
    
    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.6, 0.7)));
        
        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 mut builder = geom::path::Builder::new();
    for i in 0..N {
        let i = i as f32;
        let x1 = map_range((index * 0.135 + (i * spread)).sin(), -1.0, 1.0, 0.1, 0.5);
        let y1 = ((index + i) * 0.130 + (i * spread)).cos();
        let x2 = map_range((index * 0.125 + (i * spread)).sin(), -1.0, 1.0, 0.5, 0.9);
        let y2 = ((index + i) * 0.120 + (i * spread)).cos();
        builder = builder.move_to(pt2(x, y));
        builder = builder.cubic_bezier_to(pt2(x1, y1) * win.wh() * 0.5, pt2(x2, y2) * win.wh() * 0.5, pt2(x3, y3));
    }
    let path = builder.build();
    draw.scale_axes(vec3(1.0, -1.0, 1.0))
    .path().stroke().color(rgba(1.0, 1.0, 0.0, 0.8)).weight(1.0).events(path.iter());
    
    draw.to_frame(app, &frame).unwrap();
}