Skip to content

Instantly share code, notes, and snippets.

@pLabarta
Created December 10, 2021 11:57
Show Gist options
  • Save pLabarta/c23bd5a4f2c9fc692b1f6120ebb9ef07 to your computer and use it in GitHub Desktop.
Save pLabarta/c23bd5a4f2c9fc692b1f6120ebb9ef07 to your computer and use it in GitHub Desktop.
Schotter en Rust
use nannou::prelude::*;
const ROWS: u32 = 22;
const COLS: u32 = 12;
const SIZE: u32 = 30;
const MARGIN: u32 = 35;
const WIDTH: u32 = COLS * SIZE + 2 * MARGIN;
const HEIGHT: u32 = ROWS * SIZE + 2 * MARGIN;
const LINE_WIDTH: f32 = 0.06;
fn main() {
nannou::sketch(view)
.size(WIDTH,HEIGHT)
.run()
}
fn view(app: &App, frame: Frame) {
// Loop Once Config
app.set_loop_mode(LoopMode::loop_once());
// Assign Draw interface to the draw variable
let draw = app.draw();
// Customized coordinates in gdraw
let gdraw = draw.scale(SIZE as f32)
.scale_y(-1.0)
.x_y(COLS as f32 / -2.0 + 0.5, ROWS as f32 / -2.0 + 0.5);
// Using the Draw interface
draw.background().color(WHITE);
// Drawing grid
for y in 0..ROWS {
for x in 0..COLS {
// New Draw interface for each shape
let cdraw = gdraw.x_y(x as f32, y as f32);
// Randomize position and rotation
// depending on distance from top
let factor = y as f32 / ROWS as f32;
let x_offset = factor * random_range(-0.5,0.5);
let y_offset = factor * random_range(-0.5, 0.5);
let rotation = factor * random_range(-PI / 4.0, PI / 4.5);
// Draw each square
cdraw.rect()
.no_fill()
.stroke(BLACK)
.stroke_weight(LINE_WIDTH)
.w_h(1.0,1.0)
.x_y(x_offset,y_offset)
.rotate(rotation)
;
}
}
// Render everything to the Frame
draw.to_frame(app, &frame).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment