This program calculates a grid and dumps it to the screen.
After pressing the mouse a new grid is drawn which begins from the portion left over from the previous. So if the last square on the X was a 25% portion of dark grey, the next grid would start with the left over 75% dark, before carrying on.
A row and column count means that after a number of columns have been calculated, x goes back to 0 and a y offset is calculated, again the starting top left square is offset and coloured depending upon the left over portion.
This almost works, but not quite, any pointers much appreciated.
Code:
int col = 4;
int row = 5;
int xoff = 0;
int yoff = 0;
int cell = 40;
void setup() {
size (308, 200);
initgrid();
}
void draw () {
}
int count = 0;
void mouseReleased() {
if (count >= col*row) {
count =0;
}
calcgrid ( count );
count++;
}
int bg, fl;
void initgrid() {
bg = 120;
fl = 200;
}
void calcgrid ( int index ) {
// find the grid reference
if (index%col == 0) {
println("row: " + index/col);
// xoff = 0;
// calculate yoffset
}
float xoffset = xoff + width/(float)cell;
noStroke();
fill (fl);
background (bg);
int odd = (int) xoffset & 2;
if (odd == 1) {
int tmp = bg;
bg = fl;
fl = tmp;
}
xoffset = (xoffset%1)*cell;
drawgrid (xoff%cell , 0);
xoff += xoffset;
}
void drawgrid( int xoff, int yoff ) {
int xor = 0;
for(int y=0; y<height/cell+cell; y++){
xor = (xor+cell)%(cell*2);
for(int x=0; x<width/cell+cell; x++){
rect((x*(cell*2)+xor)-xoff, (y*cell)-yoff, cell, cell);
}
}
}