Skip to content

Instantly share code, notes, and snippets.

@oolong32
Created December 23, 2016 22:56
Show Gist options
  • Save oolong32/e843dd89570af1fa0209808f7fed6f77 to your computer and use it in GitHub Desktop.
Save oolong32/e843dd89570af1fa0209808f7fed6f77 to your computer and use it in GitHub Desktop.
// set up grid in p5
var rows;
var cols;
var cell_w = 72;
var cell_h = 77;
function setup() {
createCanvas(500, 500);
background(0);
// set up grid
cols = Math.floor(width / cell_w);
rows = Math.floor(height / cell_h);
// define margins
offset_x = (width % cell_w) / 2;
offset_y = (height % cell_h) / 2;
}
function draw() {
noFill();
stroke(255);
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
push();
translate(j*cell_w + offset_x, i*cell_h + offset_y); // translate to upper left corner of each cell instead of calculating the coordinates
rect(0, 0, cell_w, cell_h);
pop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment