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

Hi again @doni,

This is linked to your previous post:

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 :wink: )

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

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:

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