xxxxxxxxxx
27
// Q3 Create a grid of 100 cells that turn red when you hover over them.
//Challenge: Rewrite your grid so that you can change the total width of the grid, the total height of the grid, the total number of columns and the total number of rows without having to change any of the code in the loops.
let numColumns = 10
let numRows = 10
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
//column
for (let y = 0; y < height; y += height/numColumns) {
//row
for (let x = 0; x < width; x += width/numRows) {
if (mouseX > x && mouseX < x + width/numRows && mouseY > y && mouseY < y + height/numColumns) {
fill("red");
} else {
fill("white");
}
rect(x, y, width / numRows, height / numColumns);
}
}
}