Dan Quinn

Code Sketch - Flow Field


Nannou source code:

use nannou::noise::*;
use nannou::prelude::*;

fn main() {
    nannou::app(model).run();
}

struct Model {
    cols: u32,
    rows: u32,
    noise: Perlin,
}

fn model(app: &App) -> Model {
    // Create a new window! Store the ID so we can refer to it later.
    let width = 800;
    let height = 800;
    let scale = 40;
    app.new_window().size(width, height).view(view).build().unwrap();
    let cols = width / scale;
    let rows = height / scale;
    let mut noise = Perlin::new();
    noise = noise.set_seed(1);
    Model {
        cols,
        rows,
        noise
    }
}

// Draw the state of your `Model` into the given `Frame` here.
fn view(app: &App, model: &Model, frame: Frame) {
    frame.clear(BLACK);
    let win = app.main_window();
    let win_r = win.rect();

    let draw = app.draw();
    let mut yoff = 0.0;
    let zoff = app.time / 5.0;
    let line_length = (win_r.right() - win_r.left()) / model.cols as f32;

    for col in 0..model.cols {
        let mut xoff = 0.0;
        for row in 0..model.rows {
            let random = model.noise.get([xoff as f64, yoff as f64, zoff as f64]);
            let angle = map_range(random, 0.0, 1.0, 0.0, 360.0).to_radians();
            let start_x = map_range(col as f32, 0.0, model.cols as f32, win_r.left(), win_r.right());
            let start_y = map_range(row as f32, 0.0, model.rows as f32, win_r.bottom(), win_r.top());
            let start = pt2(start_x, start_y);
            let end = start + pt2(line_length as f32 * angle.cos(), line_length as f32 * angle.sin());
            draw.line()
                .start(start)
                .end(end)
                .weight(1.0)
                .color(WHITE);
            xoff += 0.1;
        }
        yoff += 0.1;
    }

    draw.to_frame(app, &frame).unwrap();
    
    if frame.nth() < 300 {
        let file_path = captured_frame_path(app, &frame);
        app.main_window().capture_frame(file_path);
    }
}

fn captured_frame_path(app: &App, frame: &Frame) -> std::path::PathBuf {
    app.project_path()
        .expect("failed to locate `project_path`")
        .join("frames")
        .join(format!("{:04}", frame.nth()))
        .with_extension("png")
}