We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Grids (Read 398 times)
Grids
Mar 30th, 2006, 2:33pm
 
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);
}
}
}
Re: Grids
Reply #1 - Mar 30th, 2006, 7:53pm
 
Sorted it. Just in case anyone needs this sort of thing (though hell alone knows why!) here it is:

Code:



int col = 4;
int row = 5;

int xoff = 0;
int yoff = 0;

int cell = 32;

void setup() {
size (408, 200);

initgrid();

}

void draw () {


}


int count = 0;

void mouseReleased() {

if (count >= col*row) {
initgrid();
}
calcgrid ( count );
count++;
}

int bg, fl;

void initgrid() {

count = 0;

bg = 120;
fl = 200;

xoff = 0;
yoff = 0;
}

void calcgrid ( int index ) {

// find the grid reference
if (index%col == 0) {
println("row: " + index/col);

xoff = 0;

if (index/col > 0) {
// calculate yoffset, but only after 1st row is finished

int y = height/cell; // number of cells high
int y1 = height - y * cell; // remainder
y = y & 1;
if (y ==1) {

swap();
}

yoff = (yoff+y1);
}
}

int x = width/cell; // number of cells in wide
int x1 = width - x * cell; // remainder

noStroke();
fill (fl);
background (bg);

drawgrid (xoff, yoff);
x = x & 1;
if (x ==1) {

swap();
}

xoff = (xoff+x1);
}



void swap( ) {

int tmp = bg;
bg = fl;
fl = tmp;

}

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);
}
}
}


Page Index Toggle Pages: 1