Hi,
This is my first post, so please be kind if I missed something regarding rules/etiquette.
So, working through all the tuts so far and everything is fine, except I’m really trying to conceptualize what two lines are doing from the Images and Pixels page, the flashlight example, here is the full code with both the loops provided.
for (int x = 0; x < crawl.width; x++)
{
for (int y = 0; y < crawl.height; y++ )
{
// Calculate the 1D pixel location
int loc = x + y * crawl.width;
// Get the R,G,B values from image
float r = red (crawl.pixels[loc]);
float g = green (crawl.pixels[loc]);
float b = blue (crawl.pixels[loc]);
// Calculate an amount to change brightness based on proximity to the mouse.
float distance = dist(x, y, mouseX, mouseY);
float adjustBrightness = (100 - distance) / 100; // first num = radius of effect, larger num = greater radius. second num = brightness of effect, smaller num = brighter.
r *= adjustBrightness;
g *= adjustBrightness;
b *= adjustBrightness;
// Constrain RGB to between 0-255
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
// Make a new color and set pixel in the window
color c = color(r, g, b);
pixels[loc] = c;
}
}
full code can be found here: https://processing.org/tutorials/pixels/
about halfway down the page.
The part, I can’t quite grasp is are these two lines:
float distance = dist(x, y, mouseX, mouseY);
float adjustBrightness = (100 - distance) / 100;
I understand that we’re calculating the distance from our (x, y) point in the loops to the mouse cursors position and then setting brightness using that distance. I also understand that the first 100 affects the diameter of the ‘torch’ effect, and I also understand that the second 100 affects the brightness of the ‘torch’ effect.
But even knowing all this I still can’t grasp how this is calculated.
If the loops are continually looping in draw(), then why if I keep my mouse still in the center of the image does the brightness not change, as surely even without the mouse moving we are still looping from (0, 0) to whatever the coord of the last pixel is, which in turn would then change the distance as we move across and down the screen left to right, top to bottom as the loc variable is updated based on pixel location at (x, y) and as it moves closer to the mouse pos at the center.
I would expect the brightness to increase as the loop (x, y) approaches the centre pixel where the mouse cursor is, and then decrease as the loop (x, y) moves away from the centre pixel where the mouse cursor is.
I hope all that makes sense.
Thanks in advance.
Fib.