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.
IndexProgramming Questions & HelpPrograms › Basic grid of squares, random colors
Page Index Toggle Pages: 1
Basic grid of squares, random colors ? (Read 898 times)
Basic grid of squares, random colors ?
Apr 16th, 2010, 6:52am
 
Hi got this code , i want all the squares to have a random color , now , i think the random color comes only on the horizontal columns , but how do i do all the rects have a random color ?

...

thanx !

Code:


int largo = 20;
int r = 0;
int g = 0 ;
int b = 0;
void setup() {
size(300,300);


}

void draw()
{

frameRate(12);
for (int x = 0; x < width; x+=20)
{
for (int y = 0; y < height; y+=20)
{
stroke(255);
fill(r,g,b);
rect(x,y,largo-1,largo-1);
}
r +=mouseX+ random(25);
g += mouseX+random(25);
b += mouseX+random(25);


if ( r > 255 )
{
r = 0;
}
if ( g > 255 )
{
g = 0;
}

if ( b > 255 )
{
b = 0;
}
}

}
Re: Basic grid of squares, random colors ?
Reply #1 - Apr 16th, 2010, 7:25am
 
Just move the computation of r, g, b to the inner loop (on y).
And % (modulo) might help you in simplifying code.
Page Index Toggle Pages: 1