We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Im looking through the introduction of the book: http://natureofcode.com/book/introduction/
and we are looking at 2D Noise and the author presents this code:
loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
float bright = random(255);
pixels[x+y*width] = color(bright);
}
}
updatePixels();
The author then goes on to show how we can use noise() instead of random. But Im confused as to the code snippet and how it creates noise. So this is what I do understand:
Answers
1 yes
2&3
the screen is 2D grid
since both for-loops are nested, we adress all pixels in the screen:
first we can have x=0 then the first column goes all the way down with y ...
then x= 1 column 1 down with a new y from 0 to height
then x= 2 column 2 down etc.
it's a nested for loop for a grid.
4
yes
5
it sets the value at all pixels as described
When you use color(int), it is working with a gray scale, from black at 0 to white at 255, and a shades of gray between these two limits. Because he is calling the random function, then you are going to get random shades of gray for every pixel.
In regards to the for loops, that is the code to access each pixel in your 2D array storing your original image data. A 2D array, or a table, in programming can be represented by a single array of length width*height. Here in your example, you are accessing every pixel by columns. When y finish a loop, the next x value is taken, meaning you are accessing the next column.
The best way to understand this is to work with smaller values, width=4 and height=6. If you interact over the loops, you will see the output. Or check this other way, by printing values:
A better example, coloring every column:
Check this, working with a single for loop:
Kf