# How to Iterate over 2D region with 1D pixels array

**URL:** <https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336>\
**Category:** Libraries\
**Created:** [October 18, 2022, 12:04pm UTC](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336 "2022-10-18T12:04:27Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![doni](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@doni](https://discourse.processing.org/u/doni)\
**Post date:** [October 19, 2022, 4:36am UTC](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336/21 "2022-10-19T04:36:40Z")

</div>

the limitation done by this

```auto
  for (int x = 0; x < video.width && x < 100; x ++ ) {
    for (int y = 240; y < video.height; y ++ )

```

note affect

```auto
if (blue(video.pixels[loc])

```

since loc come from this

```auto
int loc = x + y*video.width

```

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [October 19, 2022, 4:41am UTC](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336/22 "2022-10-19T04:41:10Z")

</div>

What don’t you like? Your code says you want the circle to be drawn over the pixel sampled and that’s what it does. The circle also moves when the mouse is moved. Is that what you don’t want?

Perhaps you only want to sample the rectangle in the lower left hand corner, where x \< 100 and y \> 240? Is that correct? Right now it’s sampling any pixel in the image.

---

<div class="post-metadata">

**Author:** ![doni](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@doni](https://discourse.processing.org/u/doni)\
**Post date:** [October 19, 2022, 4:53am UTC](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336/23 "2022-10-19T04:53:40Z")

</div>

![processing color tracking](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/c/ec20d25183558aee2693a76224a13e24939dbb94.jpeg)

the tracking, draw circle and the redraw of tracked color done within this range

```auto
for (int x = 0; x < video.width && x < 100; x ++ ) {
    for (int y = 240; y < video.height; y ++ )

```

not over those x and y value

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [October 19, 2022, 5:25am UTC](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336/24 "2022-10-19T05:25:49Z")

</div>

If you only want to trap clicks in the area x \< 100 and y \> 240 then you need to define this area in mousePressed():

```auto
void mousePressed() {
  // Save color where the mouse is clicked in trackColor variable
  if (mouseX < 100 && mouseY > 240) {
    int loc = mouseX + mouseY*video.width;
    trackColor = video.pixels[loc];
    locX = mouseX;
    locY = mouseY;
  }
}

```

locX, locY need to be global variables defined at the top of your code:

```auto
int locX = 0;
int locY = 0;

```

Add a counter and use it like this:

```auto
  int counter = 0;

  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      if (x<100 && y>240) {
        // Draw a circle at the tracked pixel
        fill(trackColor);
        strokeWeight(4.0);
        stroke(0);
        ellipse( locX, locY, 20, 20);
        if (blue(video.pixels[counter]) > 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[loc]) / 255.0, green(video.pixels[loc]) / 255.0);
        }
        counter++;
  ...
   }
}

```

This should allow you to trap clicks only in the designated area and report the color.

It’s getting late here and I’m going to have to sign off. Hopefully this will help you finish your project.

---

<div class="post-metadata">

**Author:** ![doni](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@doni](https://discourse.processing.org/u/doni)\
**Post date:** [October 19, 2022, 5:28am UTC](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336/25 "2022-10-19T05:28:01Z")

</div>

thank you so much brother i really appreciate it, i will inform you for any progress

[Previous page](https://discourse.processing.org/t/how-to-iterate-over-2d-region-with-1d-pixels-array/39336.md?page=1)
