/**
* Checkerboard.
*
*
Elie Zananiri
* CART 253, Winter 2008
*/
/*------- initialization ------------*/
// global variables
int cellSize = 20; // size of our rectangles
// sets up the applet
void setup() {
size(300, 300);
background(255);
noStroke();
}
/*------ function definitions -------*/
// draws a checkerboard pattern
void drawPattern() {
boolean white = true; // flag to indicate the fill
boolean oddColumn = true; // flag to indicate the column type
// 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 (white) {
fill(255);
} else {
fill(0);
}
rect(x, y, cellSize, cellSize);
// toggle the fill flag
white = !white;
}
// flip the flag that tells us whether we're starting an even or odd column
oddColumn = !oddColumn;
// make sure that each successive column starts with a different fill
white = oddColumn;
}
}
// outlines the cell under the mouse in red
void showFocus() {
noFill();
// calculate the coordinates of the cell using integer division
int x = mouseX/cellSize;
x = x*cellSize;
int y = mouseY/cellSize;
y = y*cellSize;
stroke(255, 0, 0);
rect(x, y, cellSize, cellSize);
// clean up after yourself by resetting the stroke
noStroke();
}
/*------ main loop ------------------*/
void draw() {
drawPattern();
showFocus();
}