int a = 20;
int columns = 30;
int rows = 30;
int s = 3; //critical slope
int[] h = new int[columns];
void setup(){
size(600, 600); // size should be a * (columns, rows)
smooth();
frameRate(5);
//Draw grids
stroke(0,200,0);
for (int j = 0; j < rows; j++){
for (int i = 0; i < columns; i++){
rect(i*a,j*a,a,a);
}
}
// display();
}
void draw() {
drop();
display();
generate();
display();
saveFrame();
}
void display() {
for (int i = 0; i < columns; i++){ // i runs 0 to columns-1 (columns)
for (int j = 0; j < h[i]; j++){ // j runs 0 to h[i]-1 ( h[i] )
fill(0);
rect((i)*a,(rows-j-1)*a,a,a); //rows-1-j
}
for (int j = h[i]; j < rows-1; j++){ // j runs h[i] to rows -1 ( rows-h[i])
fill(255);
rect(i*a,(rows-j-1)*a,a,a);
}
}
}
void generate() {
// int next[] = new int[columns];
for (int i = 0; i < columns-1; i++){
int d = h[i] - h[i+1];
if (d > s-1){ // edit here
h[i] = h[i] - d;
for (int j=0; j < columns-1; j++){
if ( j > i && j < i+ d +1 )
h[j] +=1; // drop cells
}
}
}
if (h[columns-1] > s-1){ // edit here
h[columns-1] = 0;
}
}
void drop(){
h[0] +=1;
//if (mousePressed == true){
//h[int(mouseX/a)] +=1;
// }
}
void mousePressed(){
noLoop();
//save("img.png");
}
//void mouseReleased() {
//loop();
//}