1

could any one explain me the difference between one dimensional for loop and two dimensional for loop. and how could i change following one dimensional for loop to two dimensional for loop on the second snippet code. thank you

i think following is one dimensional for loop

Capture video;

for(int i = 0; i < video.pixels.length; i++){
    // encoded so blue is > 0 if a pixel is within threshold
    if(blue(video.pixels[i]) > 0){
      count++;
      // processing takes 0-1 (float) color values from shader to 0-255 (int) values for color
      // to decode, we need to divide the color by 255 to get the original value
      avg.add(red(video.pixels[i]) / 255.0, green(video.pixels[i]) / 255.0);
    }
  }

and following snippet code is 2 dimensional for loop

Capture video;
   for (int x = 0; x < video.width && x < 100; x++ ) {
      for (int y = 240; y < video.height; y++ ) {
       int loc = x + y*video.width;
        }
    }
march
  • 11
  • 4

1 Answers1

2

A for loop has 3 parts initialization; condition; increment/decrement Typically used as

for (int x = 0; x < video.width && x < 100; x++ )

Where int x = 0 initializes the variable x. x < video.width && x < 100 checks whether the loop should continue. And x++ increases x every time it finishes one iteration of the loop.


A 2D for is just 2 for loops nested. In the example

for (int x = 0; x < video.width && x < 100; x++ ) {
  for (int y = 240; y < video.height; y++ ) {
   int loc = x + y*video.width;
    }
}

For every increase of x, the for loop will loop through every value 240 ≤ y < video.height.


The 2D for loop is assigning a 1D position to each pixel in the video. You can use this position to read each pixel in video.pixels.

for (int x = 0; x < video.width && x < 100; x++ ) {
   for (int y = 240; y < video.height; y++ ) {
      int loc = x + y*video.width;
      if(blue(video.pixels[loc]) > 0){
         count++;
         avg.add(red(video.pixels[i]) / 255.0, green(video.pixels[i]) / 255.0);
      }
   }
}

Both the code above and your original code achieve the same thing but this way is using a 2D for loop


[Edit 1]

To have the same limits on a 1D for loop is a bit more complicated (untested)

for(int i = 0; i < video.pixels.length; i++){
    const pX = i % video.width;
    const pY = (i - pX) / video.width;
    if(pX > minX && pX < maxX && pY > minY && pY < maxY){
        ... //It is within minX and maxX and within minY and maxY
    }
}
Henhen1227
  • 392
  • 1
  • 3
  • 12
  • in this code i could limit iteration 'for (int x = 0; x < video.width && x < 100; x++ ) { for (int y = 240; y < video.height; y++ ) { int loc = x + y*video.width; } }' how to do that in with this line 'for(int i = 0; i < video.pixels.length; i++)' – march Oct 18 '22 at 23:37
  • Why wouldn't you use the 2D for loop? – Henhen1227 Oct 19 '22 at 01:30
  • I made an edit showing how you could achieve the same limits. However, I think it would be much simpler to use a 2D array. – Henhen1227 Oct 19 '22 at 01:44
  • for(int i = 0; i < video.pixels.length; i++){ int pX = i % video.width; int pY = (i - pX) / video.width; if(pX > 0 && pX < 100 && pY > 240 && pY < height){ if(blue(posBuffer.pixels[i]) > 0){ count++; // processing takes 0-1 (float) color values from shader to 0-255 (int) values for color // to decode, we need to divide the color by 255 to get the original value avg.add(red(posBuffer.pixels[i]) / 255.0, green(posBuffer.pixels[i]) / 255.0); } } } – march Oct 19 '22 at 03:03
  • it works to limit the movement of the average circle on x axis but kept drawn the tracked color although the tracked color goes beyond 100 of x axis – march Oct 19 '22 at 03:06