-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandpile.js
137 lines (118 loc) · 3.51 KB
/
sandpile.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class Sandpile {
constructor(size, options) {
this.size = size || 50;
if (!isEmpty(options)) this.options = options;
else {
this.options = {
time: 10,
xGap: 0,
size: 5,
gap: 0
}
}
this.create(0);
}
setSize(size) {
this.size = size || 100;
this.create(0);
return this;
}
create(val) {
const filler = new Array(this.size ** 2);
this.grid = fill2DarrFromArr(filler.fill(val));
return this;
}
infGrid(val) {
this.grid = infiniteGridFast(val);
return this;
}
reduce() {
this.grid = reduceTillCompleteFast(this.grid);
return this;
}
reduceAnimate() {
this.grid = reduceAnimate(this.grid, this.options);
return this;
}
addIdentityAnimate() {
this.grid = addPilesAnimate(this.grid, this.identity, this.options);
return this;
}
findIdentity() {
const filler = new Array(this.size ** 2);
let cell = fill2DarrFromArr(filler.fill(6));
this.identity = subPiles(cell, reduceTillCompleteFast(cell));
return this;
}
animateIdentity() {
const filler = new Array(this.size ** 2);
let cell = fill2DarrFromArr(filler.fill(6));
this.identity = subPilesAnimate(cell, reduceTillCompleteFast(cell), this.options);
setTimeout(() => {
this.identity = reduceTillCompleteFast(this.identity);
}, this.options.time);
return this;
}
changeValues(arr) {
for (let i = 0; i < arr.length; i++) {
this.grid[arr[i].y][arr[i].x] = val;
}
return this;
}
changeValue(x, y, val) {
this.grid[y][x] = val;
return this;
}
changeMid(val) {
const mid = (this.size + 1) >> 1;
this.changeValue(mid, mid, val);
return this;
}
drawVertical(y1, y2, x, val) {
for (let i = 0; i <= y2 - y1; i++) {
this.changeValue(x, y1 + i, val);
}
return this;
}
drawHorizontal(y, x1, x2, val) {
for (let i = 0; i <= x2 - x1; i++) {
this.changeValue(x1 + i, y, val);
}
return this;
}
drawAngle(x, y, radius, up, right, val) {
if (up && right) {
this.drawVertical(y - radius, y, x, val);
this.drawHorizontal(y, x, x + radius, val);
} else if (!up && right) {
this.drawVertical(y, y + radius, x, val);
this.drawHorizontal(y, x, x + radius, val);
} else if (up && !right) {
this.drawVertical(y - radius, y, x, val);
this.drawHorizontal(y, x - radius, x, val);
} else {
this.drawVertical(y, y + radius, x, val);
this.drawHorizontal(y, x - radius, x, val);
}
return this;
}
drawRect(x, y, l, L, val) {
for (let i = 0; i < l; i++) {
this.drawVertical(y - L, y, x + i, val);
}
return this;
}
drawCross(x, y, radius) {
this.drawVertical(y - radius, y + radius, x, val);
this.drawHorizontal(y, x - radius, x + radius, val);
return this;
}
show() {
drawGrid(this.grid, this.options.xGap, this.options.size, this.options.gap, colorSandpiles);
return this;
}
showIden() {
drawGrid(this.identity, this.options.xGap, this.options.size, this.options.gap, colorSandpiles);
return this;
}
}