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!
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:
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