-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellAuto.js
100 lines (85 loc) · 2.53 KB
/
cellAuto.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
class CellAuto {
constructor(size, rules, options) {
this.size = size || 100;
if (typeof rules === "function") this.rules = rules;
else this.rules = ulamStanislawRules;
if (!isEmpty(options)) this.options = options;
else {
this.options = {
time: 10,
xGap: 0,
size: 5,
gap: 0
}
}
const filler = new Array(this.size ** 2);
this.grid = fill2DarrFromArr(filler.fill(0));
}
setSize(size) {
this.size = size || 100;
const filler = new Array(this.size ** 2);
this.grid = fill2DarrFromArr(filler.fill(0));
return this;
}
changeValues(arr) {
for (let i = 0; i < arr.length; i++) {
this.grid[arr[i].y][arr[i].x] = 1;
}
return this;
}
changeValue(x, y) {
this.grid[y][x] = 1;
return this;
}
changeMid() {
const mid = (this.size + 1) >> 1;
this.changeValue(mid, mid);
return this;
}
drawVertical(y1, y2, x) {
for (let i = 0; i <= y2 - y1; i++) {
this.changeValue(x, y1 + i);
}
return this;
}
drawHorizontal(y, x1, x2) {
for (let i = 0; i <= x2 - x1; i++) {
this.changeValue(x1 + i, y);
}
return this;
}
drawAngle(x, y, radius, up, right) {
if (up && right) {
this.drawVertical(y - radius, y, x);
this.drawHorizontal(y, x, x + radius);
} else if (!up && right) {
this.drawVertical(y, y + radius, x);
this.drawHorizontal(y, x, x + radius);
} else if (up && !right) {
this.drawVertical(y - radius, y, x);
this.drawHorizontal(y, x - radius, x);
} else {
this.drawVertical(y, y + radius, x);
this.drawHorizontal(y, x - radius, x);
}
return this;
}
drawCross(x, y, radius) {
this.drawVertical(y - radius, y + radius, x);
this.drawHorizontal(y, x - radius, x + radius);
return this;
}
drawRect(x, y, l, L) {
for (let i = 0; i < l; i++) {
this.drawVertical(y - L, y, x + i);
}
return this;
}
show() {
drawGrid(this.grid, this.options.xGap, this.options.size, this.options.gap, colorGOL);
return this;
}
run() {
return stepContinuousFromAlg(this.grid, this.options.time, this.options.xGap, this.options.size, this.options.gap, this.rules);
}
}