-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSandpiles.pde
More file actions
75 lines (60 loc) · 1.45 KB
/
Copy pathSandpiles.pde
File metadata and controls
75 lines (60 loc) · 1.45 KB
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
70
71
72
73
74
75
int[][] sandpiles;
void setup() {
size(200, 200);
sandpiles = new int[width][height];
sandpiles[width/2][height/2] = int(pow(2, 12));
}
void topple() {
int[][] nextpiles = new int[width][height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int num = sandpiles[j][i];
if (num < 4) {
nextpiles[j][i] = num;
}
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int num = sandpiles[j][i];
if (num >= 4) {
nextpiles[j][i] += num - 4;
if (j + 1 < width)
nextpiles[j+1][i]++;
if (j - 1 >= 0)
nextpiles[j-1][i]++;
if (i + 1 < height)
nextpiles[j][i+1]++;
if (i - 1 >= 0)
nextpiles[j][i-1]++;
}
}
}
sandpiles = nextpiles;
}
void render() {
loadPixels();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int num = sandpiles[j][i];
color col = color(255, 0, 0);
if (num == 0) {
col = color(255, 255, 0);
} else if (num == 1) {
col = color(0, 185, 63);
} else if (num == 2) {
col = color(0, 104, 255);
} else if (num == 3) {
col = color(122, 0, 229);
}
pixels[i * width + j] = col;
}
}
updatePixels();
}
void draw() {
render();
for (int i = 0; i < 10; i++){
topple();
}
}