Was trying to do metaballs in processing and got this error on line 3 pixels[index] = color(x,0,y). Don’t know what caused it or how to fix it. The code doesn’t look any different from Shiffman’s coding challenge #28.
Both for (int y = 0; x < height; y++)
& int index = x + y*width;
are wrong, given the y coordinate is related to height instead!
I was not paying attention. All I had to do was y = 0; y < height; y++ instead I was doing y = 0; x < height. It is working now. Thanks for the reply.
I don’t think there’s anything wrong with index = x + y * width though. What should be the right formula then? If you follow Shiffman’s youtube challenge, he goes through the derivation of the formula in processing tutorial(pixels in processing to be exact).
Well, just try out something like size(800, 600);
and see if it’s still gonna work for ya.
You have it right
Oops! W/ all that width & height confusion, I’ve got them all mixed up!
The worst thing, I’ve done that formula so many times in my own example sketches!
Indeed, the y coordinate has to be multiplied by width in order to have the correct index row within pixels[]:
I had to look it several times too to be sure =p
If you ever forget the y*width+x
form for a 2D index lookup, it is written on the reference page for get()
– where they suggest using it instead!
Getting the color of a single pixel with
get(x, y)
is easy, but not as fast as grabbing the data directly frompixels[]
. The equivalent statement toget(x, y)
usingpixels[]
ispixels[y*width+x]
. get() / Reference / Processing.org