Hi,
I’m having trouble getting some nested for
loops to ‘animate’…
My code draws an enlarged ‘pixel grid’ by iterating through a 3D array, which contains a number of pixel ‘configurations’, each containing 3 ‘rows’ of four ‘columns’.
int[][][] pixelConfigs = {
{{0, 0, 1, 1}, {0, 1, 0, 0}, {1, 0, 1, 1}},
{{0, 1, 1, 1}, {0, 0, 0, 0}, {1, 1, 1, 0}},
{{0, 1, 1, 1}, {0, 0, 0, 1}, {1, 0, 1, 0}}
};
I want to display these ‘grids’ in sequence, looping through first to last.
I’ve used three nested loops to run through the array - configuration, row, and column - and draw the pixel grid, with each ‘pixel’ being on or off depending on the value found in the pixelConfigs array.
When I run this code, It only displays the grid that represents the final ‘configuration’ in pixelConfigs,
int[][][] pixelConfigs = {
{{0, 0, 1, 1}, {0, 1, 0, 0}, {1, 0, 1, 1}},
{{0, 1, 1, 1}, {0, 0, 0, 0}, {1, 1, 1, 0}},
{{0, 1, 1, 1}, {0, 0, 0, 1}, {1, 0, 1, 0}}
};
int LCD_pixelSize = 28;
int LCD_pixelGap = 2;
int fillVal;
void setup() {
size(1100, 400);
}
void draw() {
noStroke();
drawSingleCharacter(0, 0);
}
void drawSingleCharacter() {
for (int config = 0; config < pixelConfigs.length; config++) {
for (int row = 0; row < pixelConfigs[0].length; row++) {
for (int column = 0; column < pixelConfigs[0][row].length; column++) {
fillVal = pixelConfigs[config][row][column] * 255;
fill(fillVal);
int xPos = (row * LCD_pixelSize) + (row * LCD_pixelGap);
int yPos = (column * LCD_pixelSizet) + (column * LCD_pixelGap);
rect(xPos, yPos, LCD_pixelSize, LCD_pixelSize);
}
}
}
}
BUT when I remove the outermost for
loop, and use a random number to decide which ‘configuration’ to display, it animates, but not as intended - i.e sequentially…
void drawSingleCharacter() {
int randomIndex = int(random(0, pixelConfigs.length));
for ( int row = 0; row < pixelConfigs[0].length; row++) {
for (int column = 0; column < pixelConfigs[0][row].length; column++) {
int fillVal = pixelConfigs[randomIndex][row][column] * 255;
fill(fillVal);
int xPos = (row * LCD_pixelSize) + (row * LCD_pixelGap);
int yPos = (column * LCD_pixelSize) + (column * LCD_pixelGap);
rect(xPos, yPos, LCD_pixelSize, LCD_pixelSize);
}
}
}
I think I’m missing something basic and fundamental about the way the first bit of code with 3 loops is iterating through the array - any advice gratefully received!