setup()
setup() runs once when the sketch starts. Here it's kept minimal - all the real grid-building logic lives in initGrid() so it can also be reused by windowResized().
function setup() {
createCanvas(windowWidth, windowHeight); // https://p5js.org/reference/#/p5/createCanvas
initGrid();
}
Line-by-line explanation (2 lines)
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, so the grid uses all available space.
initGrid();- Calls the helper function that calculates the grid dimensions and builds the two empty 2D arrays used for the simulation.