Hi @cottonchipper,
Yes it’s possible to code everything you want (almost)!
Before reading the code here is the process:
A Grid
class holds a 2D array storing indices corresponding to different shapes (triangle, square, circle…).
A timer correspond to the cycling time of the current cell. An increasing index correspond to the current cell.
To display the current cell, we use millis()
and modulo to get a cycling index based on the time.
Here is my take on this:
/**
* Joseph HENRY
* Processing Forum
* https://discourse.processing.org/t/brainstorming-is-this-even-possible/39012
*/
import java.util.List;
class Grid {
int timerDuration = 500;
int cyclingShapeDuration = 100;
int timer = 0;
int cyclingCellIndex = 0;
int NSHAPES = 4;
int cols, rows, xCellSize, yCellSize;
List<List<Integer>> cells;
Grid(int cols, int rows) {
this.cols = cols;
this.rows = rows;
this.xCellSize = width / cols;
this.yCellSize = height / rows;
cells = new ArrayList<List<Integer>>();
// Fill the 2D array
for (int i = 0; i < cols; i++) {
List<Integer> column = new ArrayList<Integer>();
for (int j = 0; j < rows; j++) {
column.add(-1);
}
cells.add(column);
}
}
void startTimer() {
timer = millis();
}
void update() {
// The grid is filled entirely
if (cyclingCellIndex >= cols * rows) return;
// The shape cycling animation is over
if (millis() - timer > timerDuration) {
startTimer();
// Get back 2D coordinates
int i = int(cyclingCellIndex / cols);
int j = cyclingCellIndex % rows;
// Set the cell with the random shape
cells.get(i).set(j, getShapeIndex());
// Move to the next cell
cyclingCellIndex++;
cyclingShapeDuration = int(random(50, 200));
}
}
// Returns a cycling shape index depending on the time
int getShapeIndex() {
return int(millis() / cyclingShapeDuration) % NSHAPES;
}
void displayShape(int x, int y, int index) {
float sx = xCellSize * 0.8;
float sy = yCellSize * 0.8;
fill(0);
noStroke();
pushMatrix();
translate(x + xCellSize / 2, y + yCellSize / 2);
switch(index) {
case 0:
ellipse(0, 0, sx, sy);
break;
case 1:
rectMode(CENTER);
rect(0, 0, sx, sy);
break;
case 2:
triangle(0, -sy * 0.5, sx * 0.5, sy * 0.5, -sx * 0.5, sy * 0.5);
break;
case 3:
ellipse(0, 0, sx / 2, sy);
break;
}
popMatrix();
}
void display() {
for (int i = 0; i < cols; i++) {
int x = i * xCellSize;
for (int j = 0; j < rows; j++) {
int y = j * yCellSize;
noFill();
stroke(0);
rectMode(CORNER);
rect(x, y, xCellSize, yCellSize);
int linearIndex = i * rows + j;
int cellValue = cells.get(i).get(j);
// The cell has a shape
if (cellValue != -1) {
displayShape(x, y, cellValue);
}
// This is the current cell
if (linearIndex == cyclingCellIndex) {
displayShape(x, y, getShapeIndex());
}
}
}
}
}
Grid grid;
void setup() {
size(500, 500);
grid = new Grid(5, 5);
grid.startTimer();
}
void draw() {
background(255);
grid.display();
grid.update();
}