I'm trying to create a program in processingjs that allows the user to create a square grid of whatever size they want, and whenever you click on a square it turns another color. So far I've done it flawlessly. You can check it out at: https://www.khanacademy.org/computer-programming/tile-creator/5356772601151488
The entire code is:
var fraction = width/gridSize; //divides the width of the canvas by the grid size. this allows us to put squares in each fraction (ej. if the gridsize is 5, then the fraction will divide the width of the canvas by 5, then with this we can draw a square in the first fifth of the canvas, second fifth of the canvas, etc. by multiplying the fraction by 1, 2, 3, 4, etc.)
for (var e = 0; e < gridSize; e++){ //i cant believe this works
for (var i = 0; i < gridSize; i++){ //first it draws the squares by columns
rect(i * fraction, fraction*e, fraction, fraction); //however it only draws them column by column, to make it go through all the rows i put it in yet another for loop that increments its variable e by 1, and draw each individual row with that
}}
var squareOnTopOfMouseX = 69;
var squareOnTopOfMouseY = 69;
var foundLocation = 0;
mouseClicked = function() {
fill(255, 0, 0); //makes the color of the squares red
if(mouseX < 1 * fraction){ //if the mouse X coord is within the FIRST fifth of the fraction
squareOnTopOfMouseX = 0 * fraction; //sets the coord for the square to be on the 0th tile place. and so on for the rest
} else if(mouseX < 2 * fraction){
squareOnTopOfMouseX = 1 * fraction;
} else if(mouseX < 3 * fraction){
squareOnTopOfMouseX = 2 * fraction;
} else if(mouseX < 4 * fraction){
squareOnTopOfMouseX = 3 * fraction;
} else if(mouseX < 5 * fraction){
squareOnTopOfMouseX = 4 * fraction;
}
if(mouseY < 1 * fraction){ //if the mouse Y coord is within the FIRST fifth of the fraction
squareOnTopOfMouseY = 0 * fraction; //sets the coord for the square to be on the 0th tile place. and so on for the rest
} else if(mouseY < 2 * fraction){
squareOnTopOfMouseY = 1 * fraction;
} else if(mouseY < 3 * fraction){
squareOnTopOfMouseY = 2 * fraction;
} else if(mouseY < 4 * fraction){
squareOnTopOfMouseY = 3 * fraction;
} else if(mouseY < 5 * fraction){
squareOnTopOfMouseY = 4 * fraction;
}
rect(squareOnTopOfMouseX, squareOnTopOfMouseY, fraction, fraction);
};
Now, I don't think you need to understand my code to answer my question. If you look at the very end of the code, I have a sequence of if statements that essentially is checking for: if the user clicked on the first division of the canvas (ej. if the grid is 5x5, then its looking for if you clicked in the first fifth of the canvas), then put the X coordinate of the red square on the first fifth of the canvas. If the user clicked on the second fifth, then put the X coordinate on the second fifth of the canvas, etc. It also does the exact same thing but for the Y coordinates.
Once again, I think its ok if you dont understand that. But not only is my code at the end inefficient, it breaks if you set the grid size any higher than 5 (because I manually need to add more statements to it)
I've tried to create things like
for (var i = 0; i < gridSize; i++){
if(mouseX < 1 * fraction){
squareOnTopOfMouseX = i * fraction;
}
}
(for simplicity lets ignore the Y coordinates right now because it should be the exact same code once fixed anyways)
But this code above just doesn't work. No matter what I do I just can't get the same code I pasted in the beginning to work properly once written as a for or while loop. Can anyone help point me towards the right direction? I think that for loops are what I should be using, because all of the code after the initial if statement is the same:
else if(mouseX < (SomeGivenNumber) * fraction){
squareOnTopOfMouseX = (SomeGivenNumber-1) * fraction;
(SomeGivenNumber should go all the way up to whatever the gridSize is set to)
And as the title of my question says, something I think would work is for me to keep adding else if statements as many as are needed. Something like, if the grid is set to something like 90x90, then the 'else if' statements will go all the way up to 90. But I don't know how to do this.