# Checking objects in a certain area

**URL:** <https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220>\
**Category:** Coding Questions\
**Created:** [March 3, 2021, 12:30pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220 "2021-03-03T12:30:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 3, 2021, 12:30pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/1 "2021-03-03T12:30:31Z")

</div>

İ = 8  
J= 6

How can I check if there is a part between these rows and columns? If there is no part, I want to have an operation.

Edit; I’m doing a chess-like game … example I want to check if there is a piece in row 6 or between row 6 and column 8

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 3, 2021, 12:50pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/2 "2021-03-03T12:50:32Z")

</div>

please explain the context

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 3, 2021, 12:53pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/3 "2021-03-03T12:53:39Z")

</div>

I’m doing a chess-like game … example I want to check if there is a piece in row 6 or between row 6 and column 8

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 3, 2021, 1:07pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/4 "2021-03-03T13:07:51Z")

</div>

you mean check a few cells in one row

or check an area in a sector FROM 6,6 TO 8,8 or so

use a single for-loop OR a nested for-loop and then count if there is a piece

Chrisir

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 3, 2021, 1:18pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/5 "2021-03-03T13:18:24Z")

</div>

thanks for your answer. I’m trying to figure it out yet. Can you write a little example?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 3, 2021, 5:21pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/6 "2021-03-03T17:21:06Z")

</div>

here is an example.

You can select/ unselect cells with the mouse

Two areas are marked with a black outline. (one line, one rectangle area)

The cells in the areas are counted separately.

Make sure that the area is marked like the area that you count in count() function

Chrisir

```auto

Cell [] [] grid;

int unit = 44; // size of one tile

int wideCount = 8;
int highCount = 8; 
int count = wideCount * highCount;

int count1=0; 
int count2=0; 

// ---------------------------------------------------------

void setup() {  
  size(700, 400);

  noStroke();
  background(255);

  String[] list = {"b", "c", "d", "q"};

  grid = new Cell[wideCount] [highCount];

  int index = 0;
  for (int x=0; x < wideCount; x++) {
    for (int y=0; y < highCount; y++) {
      //start creating objects
      grid[x][y] = new Cell (x*unit, y*unit, unit, str(index));
      index++;
    } //for
  } //for

  // making AREAS
  for (int x=4; x < 8; x++) {
    for (int y=4; y < 7; y++) {  
      grid[x][y].area=true;  
      //
    }//for
  }//for

  for (int x=4; x < 7; x++) {
    grid[x][2].area=true;
  }//for
  //
} //func 

void draw() {
  background(255);

  for (int x=0; x < wideCount; x++) {
    for (int y=0; y < highCount; y++) {  
      grid[x][y].display();
    }//for
  }//for

  fill(0); 
  text(" <- in this area: "
    +count1, 
    grid[7][7].x+grid[7][7].side+6, grid[6][6].y-1);
  //
  fill(0); 
  text(" <- in this line: "
    +count2, 
    grid[7][7].x+grid[7][7].side+6, grid[2][2].y+19);
}//func

void mousePressed() {
  //
  for (int x=0; x < wideCount; x++) {
    for (int y=0; y < highCount; y++) {  
      grid[x][y].click();
      //
    }//for
  }//for

  count();
}

void count() {

  // count areas 

  count1=0;
  for (int x=4; x < 8; x++) {
    for (int y=4; y < 7; y++) {  
      if (grid[x][y].selected) 
        count1++; 
      //
    }//for
  }//for

  count2=0;
  for (int x=4; x < 7; x++) {
    if (grid[x][2].selected) 
      count2++;
  }//for
  //
}

// =============================================
// and the class

class Cell {
  ///
  float x, y;
  float side; // size 
  String text=""; 
  boolean selected; 
  boolean area=false;  

  Cell (float tempX, float tempY, 
    float tempSide, 
    String tempText) {
    x = tempX;
    y = tempY;
    side = tempSide;
    text = tempText; 
    //
    float fate = random(2);
  }//constr

  void display() {
    if (selected)
      fill(122, 0, 0); // dark
    else
      fill(255, 0, 220); // light 

    stroke(255);
    if (area)
      stroke(0); 
    //fill(255, 0, 222); // light 
    rect (x, y, side, side);

    fill(255); 
    text(text, x+55, y+60);
  }//method

  void click () {
    if (mouseX>x&&
      mouseX<x+side&&
      mouseY>y&&
      mouseY<y+side)
    {
      selected = !
        selected ;
    }
  }
  //
}//class
//

```

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 3, 2021, 5:30pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/7 "2021-03-03T17:30:06Z")

</div>

I’ll find a simpler way to do this anyway thank you …

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 3, 2021, 5:31pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/8 "2021-03-03T17:31:10Z")

</div>

apologies

these are the relevant lines

```auto
void count() {

  // count areas 

  count1=0;
  for (int x=4; x < 8; x++) {
    for (int y=4; y < 7; y++) {  
      if (grid[x][y].selected) 
        count1++; 
      //
    }//for
  }//for

  count2=0;
  for (int x=4; x < 7; x++) {
    if (grid[x][2].selected) 
      count2++;
  }//for
  //
}

```

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 3, 2021, 5:33pm UTC](https://discourse.processing.org/t/checking-objects-in-a-certain-area/28220/9 "2021-03-03T17:33:23Z")

</div>

ok thanks a lot, i will thin these.
