for loop - just learning...help......
in
Programming Questions
•
2 years ago
/*
I am just learning Processing and do not understand why the first of the two array populations in the for loop in setup will not produce a grid, but the second one (commented out here) will...
*/
int h = height;
int cols = 4;
int rows = 4;
Module[][] mods;
void setup() {
size(400, 400);
mods = new Module[cols][rows];
int widthdiv = w / cols;
int heightdiv = h / rows;
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
//why does this not create a grid?
mods[i][j] = new Module(i*widthdiv,j*heightdiv,widthdiv,heightdiv);
//but this does?
//mods[i][j] = new Module(i*100,j*100,100,100);
}
}
}
void draw() {
background(255);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
mods[i][j].display();
}
}
}
//////
class Module {
float x, y;
float w, h;
Module(int tempX, int tempY, int tempW, int tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
}
void display() {
stroke(0);
rect(x, y, w, h);
}
}
1
