Review: Basic grid

let rows = 5;
let columns = 6;

function setup() {
	createCanvas(windowWidth, windowHeight);
}

function draw() {
	background(200);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			square(x, y, 30);
		}
	}
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d9ede55e-5451-4909-9af5-ea3a4b3d29e3/Untitled.png

Note: From here on, the definitions of rows, columns, and setup() are not shown. Each of the following code samples assumes that the sketch also contains a setup() function, and definitions of rows and columns:

let rows = 5;
let columns = 6;

function setup() {
	createCanvas(windowWidth, windowHeight);
}

Draw some cells differently

Draw some cells differently: using row and column

function draw() {
	background(200);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			let size = 30;
			if (row === 3) {
				if (column === 2) {
					size = 50;
				}
			}
			square(x, y, size);
		}
	}
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/0fe1500f-1cbe-4c9c-ab38-782c7ed2250d/Untitled.png

The nested if (the if within another if) above can be replaced by "and" (&&).

function draw() {
	background(200);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			let size = 30;
			if (row === 3 && column === 2) {
				size = 50;
			}
			square(x, y, size);
		}
	}
}

Replace a cell by a circle. Rectangle positions are specified as the upper left corner, and circle positions are specified as the center, so this looks bad.

function draw() {
	background(200);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			let size = 30;
			if (row === 3 && column === 2) {
				circle(x, y, size);
			} else {
				square(x, y, size);
			}
		}
	}
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bc0b6c54-e700-4ac0-a2a6-5e00ca44a745/Untitled.png

rectMode() changes the behavior of square() (and rect()) to match circle(), so that it is easier to use square() and circle() (and rect() and ellipse()) interchangeably.

Note: This is a general principle. If there are two things – in this case functions — that you want to use interchangeably, see if there’s a way to make them more alike. In this case, there is a setting that does this. If they things that you created (such as functions you wrote), you have total control over this. Otherwise, you can create a shim or wrapper around one of the things.

function draw() {
	background(200);
	rectMode(CENTER);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			let size = 30;
			if (row === 3 && column === 2) {
				circle(x, y, size);
			} else {
				square(x, y, size);
			}
		}
	}
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5c93c0d9-eebe-49b9-a14f-e5e212bd124c/Untitled.png

We can look at any combination of and row and column, and functions that combine them, to decide what to draw in a cell.

function draw() {
	background(200);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			let size = 30;
			if (row % 3 === 0) {
				circle(x, y, size);
			} else {
				square(x, y, size);
			}
		}
	}
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/45afa33d-5521-4885-8c7f-6bd59065500f/Untitled.png

function draw() {
	background(200);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			let size = 30;
			if ((row * column) % 3 === 0) {
				circle(x, y, size);
			} else {
				square(x, y, size);
			}
		}
	}
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8300f7a9-8988-4656-9125-23d5afcfc195/Untitled.png

Note: For the following code samples, the values of rows and columns have been changed to 20.

function draw() {
	background(200);
	for (let row = 0; row < rows; row++) {
		for (let column = 0; column < columns; column++) {
			let x = 30 + 50 * column;
			let y = 40 + 50 * row;

			let size = 30;
			if ((row ** 2 + column ** 2) % 3 === 0) {
				circle(x, y, size);
			} else {
				square(x, y, size);
			}
		}
	}
}

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/991ef13b-db7e-41be-9e27-9fd6d1f9ff7a/Untitled.png