-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSandPiles.pde
69 lines (65 loc) · 1.38 KB
/
SandPiles.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
int[][] sandPiles;
final int MAX_VAL = 3;
int getCol(int value){
int col;
switch(value){
case 4:
col = color(148,138,207);
break;
case 3:
col = color(197,207,138);
break;
case 2:
col = color(138,207,171);
break;
case 1:
col = color(177,144,212);
break;
case 0:
col = color(144,195,212);
break;
default:
col = color(0);
break;
}
return col;
}
void setup(){
size(720, 720);
frameRate(3000);
sandPiles = new int[width][height];
sandPiles[width/2][height/2] = 1000000;
}
void render(){
loadPixels();
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
int value = sandPiles[x][y];
pixels[x+y*width] = getCol(value);
}
}
updatePixels();
}
void topple(){
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
int value = sandPiles[x][y];
if (value > MAX_VAL){
int giveAway = floor(value/4);
sandPiles[x][y] = value - giveAway * 4;
if (x-1 >= 0)
sandPiles[x-1][y] += giveAway;
if (x+1 < width)
sandPiles[x+1][y] += giveAway;
if (y-1 >= 0)
sandPiles[x][y-1] += giveAway;
if (y+1 < height)
sandPiles[x][y+1] += giveAway;;
}
}
}
}
void draw(){
render();
topple();
}