-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
45 lines (39 loc) · 933 Bytes
/
utils.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
function* border(center, radius) {
let x = center.x - radius;
let y = center.y - radius;
let dx = 1;
let dy = 0;
const max = (radius * 2 + 1) * 4 - 4;
for (let i = 0; i < max; i++) {
yield {x, y};
if (i > 0 && i % (max / 4) == 0) {
const tmp = dx;
dx = -dy;
dy = tmp;
}
x += dx;
y += dy;
}
}
function *radiate(center, maxRadius) {
yield center;
for (let r = 1; r < maxRadius; r++) {
for (const pt of border(center, r)) {
yield pt;
}
}
}
function computeObjectsDelta(a, b) {
const result = [];
for (const k in a) {
const delta = a[k] - (b[k] || 0);
for (let i = 0; i < delta; i++) {
result.push(k);
}
}
return result;
}
const sleep = t => new Promise(r => {
setTimeout(r, t);
});
const $ = (tag) => document.createElement(tag);