-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.js
197 lines (177 loc) · 4.99 KB
/
grid.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
class Grid {
constructor(width, height, resolution = 5) {
this.resolution = resolution;
this.width = width;
this.height = height;
this.cols = floor(this.width / this.resolution);
this.rows = floor(this.height / this.resolution);
this.items = new Array(this.cols);
this.next = new Array(this.cols);
}
init = (dimensions) => {
// ignore minimumDensity for 3D mode
if (dimensions !== "3D") {
this.setMinimumDensity();
}
this.make2DArray(this.items);
this.make2DArray(this.next);
this.populateGrid();
this.countNeighbors();
if (dimensions !== "3D") {
background(0);
this.render();
}
};
setMinimumDensity = () => {
if (this.cols * this.rows <= 10000) {
this.resolution = 3;
this.cols = floor(this.width / this.resolution);
this.rows = floor(this.height / this.resolution);
}
};
make2DArray = (arr) => {
for (let x = 0; x < this.cols; x++) {
arr[x] = new Array(this.rows);
}
};
populateGrid = () => {
this.loopRunner((x, y) => {
let rand = floor(random(1.6));
let alive;
rand ? (alive = true) : (alive = false);
this.items[x][y] = new Cell(alive);
});
};
render = () => {
this.loopRunner((x, y, w, h) => {
if (this.items[x][y].alive) {
// color cell by age (red (new) --> purple (old))
const hue = this.clamp(this.items[x][y].age * 0.25, 0, 270);
const intensity = this.clamp(this.items[x][y].age * 0.5 + 10, 0, 100);
fill(hue, intensity, intensity + 20);
rect(w, h, this.resolution);
}
// this.debug(x, y, w, h);
});
};
render3D = () => {
colorMode(HSB);
this.loopRunner((x, y, w, h) => {
if (this.items[x][y].alive) {
shininess(this.items[x][y].age * 0.25);
const hue = this.clamp(this.items[x][y].age, 0, 270);
const intensity = this.clamp(this.items[x][y].age * 0.5 + 10, 0, 100);
specularMaterial(hue, intensity, intensity + 20);
push();
translate(w - width / 2, h - height / 2);
box(this.resolution, this.resolution, this.items[x][y].age * 0.5);
pop();
}
});
colorMode(RGB);
};
clicked = (mouseX, mouseY) => {
this.loopRunner((x, y, w, h) => {
// check if mousePressed location is within bounds
if (
mouseX > w &&
mouseX < w + this.resolution &&
mouseY > h &&
mouseY < h + this.resolution
) {
this.items[x][y].kill();
this.items[x][y].toggle();
}
});
};
countNeighbors = () => {
this.loopRunner((x, y) => {
// reset neighborCount
this.items[x][y].neighborCount = 0;
// loop over adjacent cells +/- 1
for (let i = -1; i < 2; i++) {
for (let j = -1; j < 2; j++) {
// use modulo to wrap around for edge cells
let col = (x + i + this.cols) % this.cols;
let row = (y + j + this.rows) % this.rows;
// omit self from count
if (i !== 0 || j !== 0) {
// only count live neighbors
if (this.items[col][row].alive === true) {
this.items[x][y].neighborCount++;
}
}
}
}
});
};
runSimulation = (dimensions) => {
this.countNeighbors();
this.next = this.items;
this.loopRunner((x, y, w, h) => {
let cell = this.items[x][y];
// underpopulation
if (cell.alive && cell.neighborCount < 2) {
this.next[x][y] = new Cell(false);
}
// overpopulation
else if (cell.alive && cell.neighborCount > 3) {
this.next[x][y] = new Cell(false);
}
// reproduction
else if (!cell.alive && cell.neighborCount === 3) {
this.next[x][y] = new Cell(true);
}
// lives on
else {
this.next[x][y].age += 1;
}
});
this.items = this.next;
dimensions === "3D" ? this.render3D() : this.render();
};
resize = (w, h) => {
this.width = w;
this.height = h;
this.cols = floor(this.width / this.resolution);
this.rows = floor(this.height / this.resolution);
};
clear = (dimensions) => {
this.loopRunner((x, y) => {
this.items[x][y].kill();
});
dimensions === "3D" ? this.render3D() : this.render();
};
reseed = (dimensions) => {
this.clear();
this.populateGrid();
dimensions === "3D" ? this.render3D() : this.render();
};
clamp = (value, min, max) => {
if (value < min) {
value = min;
} else if (value > max) {
value = max;
}
return value;
};
loopRunner = (callback) => {
for (let x = 0; x < this.cols; x++) {
for (let y = 0; y < this.rows; y++) {
let w = x * this.resolution;
let h = y * this.resolution;
callback(x, y, w, h);
}
}
};
debug = (x, y, w, h) => {
fill(255);
text(`${x},${y}`, x + w, y + h + 10);
fill(0, 255, 0);
text(
`${this.items[x][y].neighborCount}`,
x + w + this.resolution / 2,
y + h + this.resolution / 2
);
};
}