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

**URL:** <https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445>\
**Category:** Beginners\
**Created:** [November 11, 2018, 1:44pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445 "2018-11-11T13:44:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dino6](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Dino6](https://discourse.processing.org/u/Dino6)\
**Post date:** [November 11, 2018, 1:44pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/1 "2018-11-11T13:44:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)\
**Post date:** [November 11, 2018, 1:57pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/2 "2018-11-11T13:57:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 11, 2018, 1:59pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/3 "2018-11-11T13:59:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [November 11, 2018, 2:00pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/4 "2018-11-11T14:00:54Z")

</div>

Maybe the pixels array is 1D because the computer memory is 1D 🙂

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).

---

<div class="post-metadata">

**Author:** ![Dino6](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Dino6](https://discourse.processing.org/u/Dino6)\
**Post date:** [November 11, 2018, 2:01pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/5 "2018-11-11T14:01:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Dino6](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Dino6](https://discourse.processing.org/u/Dino6)\
**Post date:** [November 11, 2018, 2:15pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/6 "2018-11-11T14:15:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 11, 2018, 5:06pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/7 "2018-11-11T17:06:10Z")

</div>

> [@Dino6](#):
>
> processing/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?

---

<div class="post-metadata">

**Author:** ![Dino6](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Dino6](https://discourse.processing.org/u/Dino6)\
**Post date:** [November 11, 2018, 7:03pm UTC](https://discourse.processing.org/t/why-is-the-pixels-array-unidimensional-and-not-bidimensional/5445/8 "2018-11-11T19:03:24Z")

</div>

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
