# Changing row-size of dynamic grid

**URL:** https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510
**Category:** Coding Questions
**Created:** [December 19, 2019, 2:44pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510 "2019-12-19T14:44:32Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Namrad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/namrad/32/15938_2.png) [@Namrad](https://discourse.processing.org/u/Namrad)
#### Post date: [December 19, 2019, 2:44pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/1 "2019-12-19T14:44:32Z")

</div>

Hello!  
I’m encountering a problem with a grid-sketch. The answer seems rather simple, yet I can’t wrap my head around it for several days now.  
The idea is simple: I wanted to make a grid of rectangles which alter in width over time. I have that working!

```auto
int index;
float cols, rows;
float scl = 30;
float yoff;

float b;

void setup() {
  size(500, 500, P2D);
  cols = width/scl;
  rows = height/scl;
  index = int(cols*rows);
}

void draw() {
  background(0);
  b+= .05;
  
  ellipseMode(CORNER);
  noFill();
  stroke(255);

  for (int y = 0; y < rows; y++) {      
    for (int x = 0; x < cols; x++) {
      float start = map(y, 0, rows, 0, b);
      float aMap = map(sin(start), -1, 1, -10, 10);
      rect(x*(scl+aMap), y*scl, scl+aMap, scl);
    }
  }
}

```

The idea that I have failed to get working is that I would like to alter the height of each row of rectangles in a similar fashion. I thought this could be done with a similar approach, but this results in rectangles overlapping:

```auto
  yoff = 0;
  for (int y = 0; y < rows; y++) {  
    for (int x = 0; x < cols; x++) {
      float start = map(y, 0, rows, 0, b);
      float startH = map(yoff,0,rows,0,b);
      float changeW = map(sin(start), -1, 1, -10, 10);
      float changeH = map(sin(startH), -1, 1, -10, 10);
      rect(x*(scl+changeW), y*(scl+(changeH/10)), scl+changeW, (scl+changeH));
    }
    yoff+=1;
  }
}

```

I understand why the above code doesn’t work. I have made several sketches on paper, and ‘looped’ through iterations of the code whilst writing everything down. It’s clear to me that the code behaves the way it does.  
Seeing as the idea is really simple, and I’m able to implement it correctly for one dimension, I feel like I’m very close to the solution of my problem. I unfortunately just seem to be unable to see what adjustments I need to make, or how to approach it so that it works the way I want it to.  
I’ve tried more complex set-ups with objects in an array or ArrayList, and have also tried to solve it by using two-dimensional arrays. The problem stays the same however, and the solution stays unclear to me.  
I hope someone here could help me by pointing me in the right direction. I’m eager to learn what solution is right for my problem.

Thanks in advance!  
Namrad

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 19, 2019, 3:03pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/2 "2019-12-19T15:03:07Z")

</div>

not sure this does what you need?

```auto
// easy grid of rectangles,
int x = 10, y = x, w = 10, h = w, off = 5, grid = 3, many = grid*grid;

void setup() {
  size(500, 500);
}

void draw() {
  background(200,200,0);
  drawGrid();
  w++;
  h=w;
}

void drawGrid() {
  println("x "+x+" y "+y+" w "+w+" h "+h+" off "+off+" grid "+grid+" many "+many);
  for (int i = 0; i < many; i++)
    rect(x+(i%grid)*(w+off), y+(floor(i/grid))*(h+off), w, h); // or any other shape/text on that position
}

```

---

<div class="post-metadata">

### Author: ![Namrad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/namrad/32/15938_2.png) [@Namrad](https://discourse.processing.org/u/Namrad)
#### Post date: [December 19, 2019, 3:17pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/3 "2019-12-19T15:17:15Z")

</div>

Thanks kll! This is more or less exactly what I’m looking for.  
It was that easy, huh?  
I’m studying your code right now to see how this is different from what I’m doing.

---

<div class="post-metadata">

### Author: ![Namrad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/namrad/32/15938_2.png) [@Namrad](https://discourse.processing.org/u/Namrad)
#### Post date: [December 19, 2019, 5:15pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/4 "2019-12-19T17:15:58Z")

</div>

After studying and trying things out with your code I don’t think it offers me what I was looking for. It did teach me a new method of building grids with loops, so thank you for that!  
For the idea I had I want every row to (be able to) change size on its own. It’s based of [a sketch by David Mrugala.](https://66.media.tumblr.com/21cb529965ad59d0bb8a7b0b7cfe7d0b/e6e0602e2985132f-17/s540x810/4cd079a43f38cd2170ab047c68a8d1fa99047ddc.jpg) When I start to implement variables to the sketch-example you gave above, I recieve the same problem. Rectangles start to overlap and to separate in unorderly ways: ![Screenshot 2019-12-19 at 18.09.47](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a97d7a43f46a0656ddca0dde42b3edc1f1cfd41d.png)

As stated earlier, I think I understand why this happens (the way I generate numbers to create this altercation of sizes causes these ‘jumps’ from row to row). I can’t wrap my head around how to solve that problem however: how to make them vary in height per row, yet still make them connect per row.

I hope my question and my examples are clear. I also hope you or someone else can still help me!

Thanks for your efforts nontheless!

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [December 20, 2019, 9:39am UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/5 "2019-12-20T09:39:28Z")

</div>

When you’re using a nested loop to draw a grid, remember you’re drawing in ‘chunks’. You don’t draw a single grid cell (rectangle) each loop. Instead you use two loops: one to decide the y position, the other to decide the x position and drawing a chunk of grid cells.

In your first sketch you seem to try and do everything, including the y positioning of each grid cell, in the second for loop. Seeing that all rectangles of each row are going to have the same y position anyway, why not make use of that? You could use [translate](https://processing.org/reference/translate_.html) to decide the y position for the row, and then draw all of the rectangles on y = 0.

Other suggestions:

- You can place drawing settings such as `ellipseMode` and `stroke` in the setup since these don’t change throughout your sketch.
- The values of `float start` and `float aMap` change based on `y`, meaning they can be moved up a level in the loop chain.

Hope that helps!

---

<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: [December 20, 2019, 4:53pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/6 "2019-12-20T16:53:51Z")

</div>

> [@Namrad](#):
>
> yoff+=1;

In theory, store the y-Position of the cell and add its individual height in a variable named nextYPos or so

Use it when drawing the next row

---

<div class="post-metadata">

### Author: ![Namrad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/namrad/32/15938_2.png) [@Namrad](https://discourse.processing.org/u/Namrad)
#### Post date: [December 21, 2019, 11:13pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/7 "2019-12-21T23:13:24Z")

</div>

It hasn’t guided me towards the end result I wanted, but I appreciate your advice nontheless considering you made some good points regarding my code structure (even though it was kind of a rough draft). I painstakingly managed to figure it out a way of reaching my desired result over the last two days.

Dank voor je hulp Tiemen. 😉

---

<div class="post-metadata">

### Author: ![Namrad](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/namrad/32/15938_2.png) [@Namrad](https://discourse.processing.org/u/Namrad)
#### Post date: [December 21, 2019, 11:18pm UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/8 "2019-12-21T23:18:21Z")

</div>

I think your comment nudged me to the solution of my problem. It made me realise I was not utilizing the nested loop effectively. I made it work by recalculating the y-location and height of each rectangle every iterations of the outer(y-)loop.  
Probably made it harder for myself than necessary, but this is how you learn. Thanks for helping me out Chrisir! 😃

---

<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: [December 22, 2019, 12:09am UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/9 "2019-12-22T00:09:17Z")

</div>

Brillant!

Nice to hear this!

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [December 22, 2019, 7:11am UTC](https://discourse.processing.org/t/changing-row-size-of-dynamic-grid/16510/10 "2019-12-22T07:11:16Z")

</div>

> [@Namrad](#):
>
> I painstakingly managed to figure it out a way of reaching my desired result over the last two days.

Good to hear you figured it out. Is it close to what I had in mind with my suggestions?

```auto
int index;
float cols, rows, scl = 30, yoff, b;

void setup() {
  size(500, 500, P2D);
  ellipseMode(CORNER);
  noFill();
  stroke(255);
  cols = width / scl;
  rows = height / scl;
  index = int(cols * rows);
}

void draw() {
  background(0);
  b += .05;
  for (int y = 0; y < rows; y++) {  
    float start = map(y, 0, rows, 0, b);
    float aMap = map(sin(start), -1, 1, -10, 10);
    float xBla = scl + aMap;
    for (int x = 0; x < cols; x++) {
      float xPos = x * xBla;
      rect(xPos, 0, xBla, xBla);
    }
    translate(0, xBla);
  }
}

```
