# How to iterate video.pixels.length for random area of image

**URL:** <https://discourse.processing.org/t/how-to-iterate-video-pixels-length-for-random-area-of-image/39367>\
**Category:** Libraries\
**Created:** [October 20, 2022, 4:41am UTC](https://discourse.processing.org/t/how-to-iterate-video-pixels-length-for-random-area-of-image/39367 "2022-10-20T04:41:45Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [October 20, 2022, 7:42am UTC](https://discourse.processing.org/t/how-to-iterate-video-pixels-length-for-random-area-of-image/39367/4 "2022-10-20T07:42:01Z")

</div>

Hi again @doni,

This is linked to your previous post:

> [@How to Iterate over 2D region with 1D pixels array](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336):
>
> the snippet code below is doing color tracking and get the average position and draw circle on it, the screen size (640, 480). how to do iteration in one dimensional array in for loop within part of the screen, for example i want to do tracking/iteration from 0 to 100 of x axis and from 0 to 100 of y axis. thank you PGraphics posBuffer; for(int i = 0; i \< posBuffer.pixels.length; i++){ // encoded so blue is \> 0 if a pixel is within threshold if(blue(posBuffer.pixels[i]) \> 0){ coun…

You should use a nested for loop and compute the 1D index with the `x + y * width` formula.

But it seems that you don’t want to use a nested for loop to iterate over the pixels… (I find it more simple and clear 😉 )

You can do that with a single `while` loop (maybe not the best solution thought):

```processing
int i = 0;
int iterations = 0;

while (i < 14) {
  iterations++;
  
  println(i + 1);
  
  if (iterations % 2 == 0) {
    i += 3;
  } else {
    i += 1;
  }
}

// -> 1 2 5 6 9 10 13 14

```

Or shorter:

```processing
while (i < 14) {
  iterations++;
  println(i + 1);
  i += ((iterations % 2) == 0) ? 3 : 1;
}

```

---

_[View the full topic](https://discourse.processing.org/t/how-to-iterate-video-pixels-length-for-random-area-of-image/39367)._
