A grid of grids with variable number of cells

Hello! :slightly_smiling_face:

I’m trying to figure out where I can initialize a new random number of cells for each grid object. For example, the first grid might be divided 2 times, the second grid divided 7 times, etc…

The attached code runs, but all 4 grids have the same number of cell divisions… :thinking:

Any guidance is most welcome!
:nerd_face:

Grid [] grid = new Grid[4]; //number of grids inside the overall grid

int div = int(random(2, 9)); //division of cells in each grid object

void setup() {
  size(600, 600);
  //noLoop();
  frameRate(2);

  int k = 0;
  int counter = width/2; // equals "2"

  for (int x = 0; x<width; x+=counter) {
    for (int y = 0; y<height; y+=counter) {
      pushMatrix();
      translate(0, 0);
      grid[k] = new Grid(div, x, y);
      popMatrix();

      k++;
    }
  }
}

void draw() {

  strokeWeight(1);
  
  //div = int(random(2, 9)); //division of cells in each grid object

  for (Grid g : grid) {
    g.display();
  }


  //grid of grids guide
  pushStyle();
  strokeWeight(2);
  stroke(0, 255, 0);
  line(width/2, 0, width/2, height);
  line(0, height/2, width, height/2);
  popStyle();
}

///////////////////////////////////////////////////////////

class Grid {

  float divisions;
  float sz;
  float x;
  float y;


  Grid(float divisions_, float x_, float y_) {

    divisions = divisions_;
    sz = width/2/divisions;
    x = x_;
    y = y_;
  }

  void display() {

    for (float i = 0; i<divisions; i++) {
      for (float j = 0; j<divisions; j++) {

        fill(random(150, 255), random(10), random(10));
        rect(x+i*sz, y+j*sz, sz, sz); //top left

        
      }
    }
    println(divisions);
  }
}
1 Like

in setup()?

Grid [] grid = new Grid[4]; //number of grids inside the overall grid

int div = int(random(2, 9)); //division of cells in each grid object

void setup() {
  size(600, 600);
  //noLoop();
  frameRate(2);

  int k = 0;
  int counter = width/2; // equals "2"

  for (int x = 0; x<width; x+=counter) {
    for (int y = 0; y<height; y+=counter) {
      pushMatrix();
      translate(0, 0);
      div = int(random(2, 9)); //division of cells in each grid object
      grid[k] = new Grid(div, x, y);
      popMatrix();

      k++;
    }
  }
}

void draw() {

  strokeWeight(1);

  //div = int(random(2, 9)); //division of cells in each grid object

  for (Grid g : grid) {
    g.display();
  }


  ////grid of grids guide
  //pushStyle();
  //strokeWeight(2);
  //stroke(0, 255, 0);
  //line(width/2, 0, width/2, height);
  //line(0, height/2, width, height/2);
  //popStyle();
}

/////////////////////////

class Grid {

  float divisions;
  float sz;
  float x;
  float y;


  Grid(float divisions_, float x_, float y_) {

    divisions = divisions_;
    sz = width/2/divisions;
    x = x_;
    y = y_;
  }

  void display() {

    for (float i = 0; i<divisions; i++) {
      for (float j = 0; j<divisions; j++) {

        fill(random(150, 255), random(10), random(10));
        rect(x+i*sz, y+j*sz, sz, sz); //top left
      }
    }
    println(divisions);
  }
}

1 Like



Grid [] grid = new Grid[16]; // number of grids inside the overall grid

int div;   //division of cells in each grid object

void setup() {
  size(600, 600);
  frameRate(2);
  strokeWeight(1);

  int k = 0;
  int counter = width/4; // equals "2"

  for (int x = 0; x<width; x+=counter) {
    for (int y = 0; y<height; y+=counter) {
      div = int(random(2, 9)); //division of cells in each grid object
      grid[k] = new Grid(div, x, y);

      k++;
    }
  }
}

void draw() {
  background(0); 

  for (Grid g : grid) {
    if (g!=null)
      g.display();
  }

  ////grid of grids guide
  //pushStyle();
  //strokeWeight(2);
  //stroke(0, 255, 0);
  //line(width/2, 0, width/2, height);
  //line(0, height/2, width, height/2);
  //popStyle();
}

/////////////////////////

class Grid {

  float divisions;
  float sz;
  float x;
  float y;

  int col1=int(random(256)); 
  int col2=int(random(256)); 

  // constr 
  Grid(float divisions_, 
    float x_, float y_) {

    divisions = divisions_;
    sz = width/4/divisions;
    x = x_;
    y = y_;
  }// constr 

  void display() {

    for (float i = 0; i<divisions; i++) {
      for (float j = 0; j<divisions; j++) {

        fill(random(150, 255), col1, col2);
        // stroke(random(150, 255), col1, col2);
        // noFill(); 
        rect(x+i*sz, y+j*sz, 
          sz, sz);
      }
    }
    println(divisions);
  }
}//class

2 Likes

Thank you @Chrisir !! :slightly_smiling_face:
Very much appreciated!!

Oh yeah that works! At one point I had tried it in setup() BUT had placed it above the for loops…

And then regarding:

I’m curious about the purpose of line:

if(g! = null)

When would (or could) null happen? Is it some kind of built-in safety valve? Though for what reason I’m not sure. I did some googling, but… :thinking:

:nerd_face:

2 Likes

I had null once when I set the upper bounds of the for loops in setup() to 1. (then only one quarter was drawn but the array still had full length.)

1 Like