/**
* Three-color checkerboard.
*
*
Elie Zananiri
* CART 253, Winter 2008
*/
/*------- global variables ----------*/
int cellSize = 20; // size of our rectangles
// counter to tracks which color to use as fill
// can have three possible states: 0, 1, 2
int currColor = 0;
// counter to tracks the first color of the current column
// ensures that we end up with a checkerboard instead of solid columns
int initialColor = 0;
/*------- initialization ------------*/
void setup() {
size(300, 300);
background(255);
noStroke();
}
/*------ function definitions -------*/
// draws a checkerboard pattern
void drawPattern() {
// walk across the x-axis
for (int x=0; x < width; x = x+cellSize) {
// walk down the y-axis
for (int y=0; y < height; y = y+cellSize) {
if (currColor%3 == 0) {
fill(255, 212, 0);
} else if (currColor%3 == 1) {
fill(255, 254, 96);
} else {
fill(247, 147, 30);
}
rect(x, y, cellSize, cellSize);
currColor++;
}
// column is finished, advance the counter for initialColor
setInitialFill();
}
}
// advances the counter that determines the color of the first cell of a column
void setInitialFill() {
if (initialColor == 0) {
currColor = initialColor = 1;
} else if (initialColor == 1) {
currColor = initialColor = 2;
} else {
currColor = initialColor = 0;
}
}
/*------ main loop ------------------*/
void draw() {
drawPattern();
}