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 › Matrix of squares, color wich are close 2 mouse
Page Index Toggle Pages: 1
Matrix of squares, color wich are close 2 mouse? (Read 664 times)
Matrix of squares, color wich are close 2 mouse?
Apr 16th, 2010, 7:23am
 
Hi , i got this basic matrix of squares , done with 2 for loops.

I want , that if my mouse is hoovering over one that this rect gets colored lets say white, and that the surrounding squares get also colored but the color goes fading out.

this is how mysketch looks :
...


this is a muckup in photoshop,but here you see a gradient, i dont want that i just want the rects to have a clearer color  as they are more far away.
...

any ideas ?
Code:



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


}

void draw()
{


frameRate(3);


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




}

}


thanx !
Re: Matrix of squares, color wich are close 2 mouse?
Reply #1 - Apr 16th, 2010, 8:35am
 
First, some advices: frameRate can be moved to setup(). You defined constants, that's nice, but you should use them... And take care of proper indentation, it eases re-reading your own code. Smiley
And instead of r, g, b, you should use gray levels.

Rewritten version of your code:
Code:
int largo = 20;
int c = 0;

void setup()
{
size(300, 300);
frameRate(3);
}

void draw()
{
stroke(255);
for (int x = 0; x < width; x += largo)
{
for (int y = 0; y < height; y += largo)
{
fill(c);
rect(x, y, largo-1, largo-1);
}
}
}

It doesn't solve your problem but that's a first step...
Re: Matrix of squares, color wich are close 2 mouse?
Reply #2 - Apr 16th, 2010, 8:47am
 
And here is the solution...
Code:
int largo = 20;
int c = 0;
int maxRadius = 4 * largo;

void setup()
{
size(300, 300);
frameRate(3);
}

void draw()
{
stroke(255);
for (int x = 0; x < width; x += largo)
{
// Center of current square (x)
float cx = x + largo / 2;
for (int y = 0; y < height; y += largo)
{
// Center of current square (y)
float cy = y + largo / 2;
// Distance between mouse and current square
float d = dist(mouseX, mouseY, cx, cy);
int c = 0; // Default to black
if (d < maxRadius) // If close enough of mouse
{
// Compute a gray level proportional to distance
c = int(255 * (1 - d / maxRadius));
}
fill(c);
rect(x, y, largo-1, largo-1);
}
}
}
Re: Matrix of squares, color wich are close 2 mouse?
Reply #3 - Apr 16th, 2010, 10:35am
 
awesome !

ill check it out and try to understand it   Wink


thanx !!!!
Page Index Toggle Pages: 1