For my last assignment, I had to create a program that will produce a checkerboard pattern using loops. I had to do research on how to create loops that alternate colors in order to do this assignment and I found out about modulo(%) and I modulo in order to create my checkerboard. Modulo basically calculates what the remainder of a number is when two numbers are divided. In order to create this checkerboard I used i & j as two of my int's and made it so that when i+j divided by thirty was more than 0 one part of the screen would be black and the other part of the screen would be red. I used the number thirty because i and j were constantly being increased by 75 and 30 was the only number I could find that when i and j were divided were divided by it caused a pattern. For example, 75/30 has a remainder, 150/30 does not, 225/30 had a remainder, but 300/30 does not and so on. This is what made me able to create a certain pattern. My finished code is down below.
int square = 75;
void setup() {
size(600, 600);
}
void draw() {
// i starts at 0, i increases by 75, i will stop when it's less than or equal to (width)
// j starts at 0, j increases by 75, j will stop when it's less than or equal to (height)
// 600/75= 8 , this board will be 8 across and 8 down(8x8)
for ( int i = 0; i <= width; i += square) {
for ( int j = 0; j <= height; j += square) {
//When i+j divided by 30 has a remainder of 0 make the block black
//When i + j divided by 30 has a remainder higher than 0 make the block red
if ( (i+j) % 30 == 0) { //== (means equality)
fill(#080707);//Black
} else {
fill(#FF0000);//Red
}
rect (i, j, square, square);
//(x,y,width,height)
}
}
}
“A program is never less than 90% complete, and never more than 95% complete.”
– Terry Baker
No comments:
Post a Comment