Perlin Circles

Circles made with Nannou.

Perlin Circles
0:00
/

Circles made with Nannou.

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 {
    let width = 200;
    let height = 200;
    let scale = 10;
    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
    }
}

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 cell_size = (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 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 size = random as f32 * cell_size;

            draw.ellipse()
                .width(size)
                .height(size)
                .x_y(start_x, start_y)
                .color(hsl(random as f32, 1.0, 0.5));

                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")
}