Skip to content

Instantly share code, notes, and snippets.

@Jachimo
Last active August 6, 2018 00:44
Show Gist options
  • Save Jachimo/f4a239d227f1ee5f2690e901bf79ebd4 to your computer and use it in GitHub Desktop.
Save Jachimo/f4a239d227f1ee5f2690e901bf79ebd4 to your computer and use it in GitHub Desktop.
Draw a 2D grid of objects, filling the viewport, using Processing 3.x
import processing.svg.*; // for SVG output
// SET THESE
int numCols = 50;
int numRows = 50;
void setup() {
size(500, 500); // for screen display
//size(864, 864, SVG, "new.svg"); // 9in x 9in SVG
background(255);
smooth();
}
void draw() {
// Draw a 2D grid of something, filling the viewport
float colWidth = (width / numCols);
float rowHeight = (height / numRows);
float xpos = (colWidth / 2); // start at middle of first column
float ypos = (rowHeight / 2); // and start in middle of first row
float ystart = ypos;
for (int i=0; i<numCols; i++) {
ypos = ystart;
for (int j=0; j<numRows; j++) {
ellipse(xpos,ypos,10,10); // or whatever you want to draw
ypos += rowHeight;
}
xpos += colWidth;
}
noLoop();
}
@Jachimo
Copy link
Author

Jachimo commented Aug 6, 2018

This is a quick-and-dirty method of drawing a regular grid of items in Processing 3.
Probably you would want to replace the ellipse() on Line 25 with translate(), followed by a separate function to draw whatever you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment