Why is the pixels array "unidimensional" and not bidimensional?

I’m just curious, I’m trying to build my own kind of pixels array out of processing/p5 and I’m wondering if it should be a normal array or a bidimensional array.
I can easily pull the xy coords from the unidimensional array with a simple algorithm and viceversa but it would be easier to use a two-dimensional array.
So is there any reason processing/p5’s pixels array is not two-dimensional?

Here are some drawbacks of multidimensional arrays in Processing (Java):

  • you need to create one array to hold all the rows and then one array for each row - initialization is more complicated
  • some rows can have different length than others
  • some rows can be null
  • it’s harder to iterate over all pixels: now you need two loops instead of one
  • harder to copy and resize: instead of System.arrayCopy() one array you need to copy each row separately
  • it might be a bit slower, because to access a value you need to follow two steps of indirection instead of one (load array holding the rows, load array holding the values in the row, access the value); with single array all the values are contiguous in memory, which should be faster to access
  • there is probably more

Basically the complexity of most operations increases, more things can go wrong, it might be slower, and the only benefit seems to be an “easier” access.

3 Likes

sorry, is your question?
why this
https://processing.org/reference/pixels.html
is not done this
https://p5js.org/reference/#/p5/set
way?
good question, ?just history?

Maybe the pixels array is 1D because the computer memory is 1D :slight_smile:

In computer programming there’s often a struggle between: should we make it easier for humans or for computers? Often the faster approach for the computer is not the intuitive approach for us (unfortunately).

1 Like

Thanks! The algorithms for getting the coords are not a problem for me and it’ll save some of my lifeforce using one loop instead of two everytime I want to iterate throught it.

1 Like

Exactly. I didn’t know about the set method, thanks! sadly I can’t use it in this case as I’m not using processing or p5.

I’m not using processing or p5.

you posted under
/ processing / beginners /
but used the “p5” word , now your last comment even confuses me more.

what is your working environment?

Well I’m trying to make my own pixels array in an html canvas js program and I didn’t know if I should make it 1D or 2D so I’m asking why is Processing’s 1D instead of 2D

1 Like