-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunkify.js
More file actions
43 lines (39 loc) · 1.24 KB
/
Copy pathchunkify.js
File metadata and controls
43 lines (39 loc) · 1.24 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
export function Chunkify(width, height, startX = 0, startY = 0) {
if (!Number.isFinite(width) || !Number.isFinite(height)) {
throw new TypeError('Chunkify width and height must be finite numbers.');
}
return {
x: startX,
y: startY,
width,
height,
*[Symbol.iterator]() {
for (let y = 0; y < height; y++)
for (let x = 0; x < width; x++) {
yield {
x: startX + x,
y: startY + y,
localX: x,
localY: y
};
}
},
*divide(chunkWidth, chunkHeight = chunkWidth) {
if (!Number.isFinite(chunkWidth) || !Number.isFinite(chunkHeight)) {
throw new TypeError('Chunk dimensions must be finite numbers.');
}
if (chunkWidth <= 0 || chunkHeight <= 0) {
throw new RangeError('Chunk dimensions must be greater than 0.');
}
const maxX = startX + width;
const maxY = startY + height;
for (let chunkY = startY; chunkY < maxY; chunkY += chunkHeight) {
for (let chunkX = startX; chunkX < maxX; chunkX += chunkWidth) {
const w = Math.min(chunkWidth, maxX - chunkX);
const h = Math.min(chunkHeight, maxY - chunkY);
yield Chunkify(w, h, chunkX, chunkY);
}
}
}
};
}