@thi.ng/grid-iterators 4.0.34 → 4.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/api.js +0 -1
- package/circle.js +53 -66
- package/clipping.js +33 -74
- package/column-ends.js +10 -13
- package/columns.js +6 -8
- package/diagonal-ends.js +17 -24
- package/diagonal-slope.js +73 -105
- package/diagonal.js +20 -28
- package/diamond-square.js +51 -71
- package/filters.js +10 -20
- package/flood-fill.js +42 -76
- package/hilbert.js +43 -63
- package/hvline.js +34 -28
- package/interleave.js +16 -41
- package/line.js +27 -37
- package/package.json +14 -12
- package/random.js +9 -15
- package/row-ends.js +10 -13
- package/rows.js +6 -9
- package/spiral.js +36 -45
- package/transforms.js +17 -22
- package/utils.js +7 -5
- package/zcurve.js +11 -16
- package/zigzag-columns.js +12 -18
- package/zigzag-diagonal.js +27 -32
- package/zigzag-rows.js +12 -18
package/diamond-square.js
CHANGED
|
@@ -1,74 +1,54 @@
|
|
|
1
1
|
import { defBitField } from "@thi.ng/bitfield/bitfield";
|
|
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
|
-
const x2 = x + r2;
|
|
49
|
-
$(x, y);
|
|
50
|
-
$(size - x, y);
|
|
51
|
-
$(size - x, size - y);
|
|
52
|
-
$(x, size - y);
|
|
53
|
-
$(x2, y1);
|
|
54
|
-
$(size - x2, y1);
|
|
55
|
-
$(size - x2, size - y1);
|
|
56
|
-
$(x2, size - y1);
|
|
57
|
-
$(x, y2);
|
|
58
|
-
$(size - x, y2);
|
|
59
|
-
$(size - x, size - y2);
|
|
60
|
-
$(x, size - y2);
|
|
61
|
-
$(x1, y1);
|
|
62
|
-
$(size - x1, y1);
|
|
63
|
-
$(size - x1, size - y1);
|
|
64
|
-
$(x1, size - y1);
|
|
65
|
-
$(x, y1);
|
|
66
|
-
$(size - x, y1);
|
|
67
|
-
$(size - x, size - y1);
|
|
68
|
-
$(x, size - y1);
|
|
69
|
-
yield* acc;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
res = r2;
|
|
2
|
+
function* diamondSquare(exp) {
|
|
3
|
+
const size = 1 << exp;
|
|
4
|
+
const size1 = size + 1;
|
|
5
|
+
const s2 = size >> 1;
|
|
6
|
+
let res = size;
|
|
7
|
+
const idx = defBitField(size1 * size1);
|
|
8
|
+
const acc = [];
|
|
9
|
+
const $ = (x, y) => {
|
|
10
|
+
!idx.setAt(x + y * size1) && acc.push([x, y]);
|
|
11
|
+
};
|
|
12
|
+
$(0, 0);
|
|
13
|
+
$(res, 0);
|
|
14
|
+
$(res, res);
|
|
15
|
+
$(0, res);
|
|
16
|
+
yield* acc;
|
|
17
|
+
while (res > 1) {
|
|
18
|
+
const r2 = res >> 1;
|
|
19
|
+
for (let y = 0; y <= s2; y += res) {
|
|
20
|
+
const y1 = y + r2;
|
|
21
|
+
const y2 = y + res;
|
|
22
|
+
for (let x = r2; x <= s2; x += res) {
|
|
23
|
+
acc.length = 0;
|
|
24
|
+
const x1 = x - r2;
|
|
25
|
+
const x2 = x + r2;
|
|
26
|
+
$(x, y);
|
|
27
|
+
$(size - x, y);
|
|
28
|
+
$(size - x, size - y);
|
|
29
|
+
$(x, size - y);
|
|
30
|
+
$(x2, y1);
|
|
31
|
+
$(size - x2, y1);
|
|
32
|
+
$(size - x2, size - y1);
|
|
33
|
+
$(x2, size - y1);
|
|
34
|
+
$(x, y2);
|
|
35
|
+
$(size - x, y2);
|
|
36
|
+
$(size - x, size - y2);
|
|
37
|
+
$(x, size - y2);
|
|
38
|
+
$(x1, y1);
|
|
39
|
+
$(size - x1, y1);
|
|
40
|
+
$(size - x1, size - y1);
|
|
41
|
+
$(x1, size - y1);
|
|
42
|
+
$(x, y1);
|
|
43
|
+
$(size - x, y1);
|
|
44
|
+
$(size - x, size - y1);
|
|
45
|
+
$(x, size - y1);
|
|
46
|
+
yield* acc;
|
|
47
|
+
}
|
|
73
48
|
}
|
|
49
|
+
res = r2;
|
|
50
|
+
}
|
|
74
51
|
}
|
|
52
|
+
export {
|
|
53
|
+
diamondSquare
|
|
54
|
+
};
|
package/filters.js
CHANGED
|
@@ -1,22 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* @param x
|
|
5
|
-
* @param y
|
|
6
|
-
* @param n
|
|
7
|
-
*/
|
|
8
|
-
export const isOnDiagonal = ([x, y], n) => {
|
|
9
|
-
const d = ((x + y) / n) | 0;
|
|
10
|
-
return y === d * n - x;
|
|
1
|
+
const isOnDiagonal = ([x, y], n) => {
|
|
2
|
+
const d = (x + y) / n | 0;
|
|
3
|
+
return y === d * n - x;
|
|
11
4
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export const isOnDiagonalAlt = ([x, y], N) => {
|
|
20
|
-
const d = ((x - y) / N) | 0;
|
|
21
|
-
return -y === d * N - x;
|
|
5
|
+
const isOnDiagonalAlt = ([x, y], N) => {
|
|
6
|
+
const d = (x - y) / N | 0;
|
|
7
|
+
return -y === d * N - x;
|
|
8
|
+
};
|
|
9
|
+
export {
|
|
10
|
+
isOnDiagonal,
|
|
11
|
+
isOnDiagonalAlt
|
|
22
12
|
};
|
package/flood-fill.js
CHANGED
|
@@ -1,81 +1,47 @@
|
|
|
1
1
|
import { BitField, defBitField } from "@thi.ng/bitfield/bitfield";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* const img = [
|
|
16
|
-
* 1,0,1,0,
|
|
17
|
-
* 0,0,0,0,
|
|
18
|
-
* 0,1,1,0,
|
|
19
|
-
* 0,1,1,1,
|
|
20
|
-
* ];
|
|
21
|
-
*
|
|
22
|
-
* // flood fill connected region from point (2,1)
|
|
23
|
-
* [...floodFill((x, y) => img[y * 4 + x] === 0, 2, 1, 4, 4)]
|
|
24
|
-
* // [
|
|
25
|
-
* // [2, 1], [1, 1], [0, 1], [3, 1], [3, 2],
|
|
26
|
-
* // [3, 0], [0, 2], [0, 3], [1, 0]
|
|
27
|
-
* // ]
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @param pred -
|
|
31
|
-
* @param x -
|
|
32
|
-
* @param y -
|
|
33
|
-
* @param width -
|
|
34
|
-
* @param height -
|
|
35
|
-
*/
|
|
36
|
-
export function* floodFill(pred, x, y, width, height) {
|
|
37
|
-
x |= 0;
|
|
38
|
-
y |= 0;
|
|
39
|
-
if (!pred(x, y))
|
|
40
|
-
return;
|
|
41
|
-
const queue = [[x, y]];
|
|
42
|
-
const visited = defBitField(width * height);
|
|
43
|
-
height--;
|
|
44
|
-
while (queue.length) {
|
|
45
|
-
[x, y] = queue.pop();
|
|
46
|
-
yield* partialRow(pred, queue, visited, x, y, width, height, -1);
|
|
47
|
-
yield* partialRow(pred, queue, visited, x + 1, y, width, height, 1);
|
|
48
|
-
}
|
|
2
|
+
function* floodFill(pred, x, y, width, height) {
|
|
3
|
+
x |= 0;
|
|
4
|
+
y |= 0;
|
|
5
|
+
if (!pred(x, y))
|
|
6
|
+
return;
|
|
7
|
+
const queue = [[x, y]];
|
|
8
|
+
const visited = defBitField(width * height);
|
|
9
|
+
height--;
|
|
10
|
+
while (queue.length) {
|
|
11
|
+
[x, y] = queue.pop();
|
|
12
|
+
yield* partialRow(pred, queue, visited, x, y, width, height, -1);
|
|
13
|
+
yield* partialRow(pred, queue, visited, x + 1, y, width, height, 1);
|
|
14
|
+
}
|
|
49
15
|
}
|
|
50
|
-
/** @internal */
|
|
51
16
|
function* partialRow(pred, queue, visited, x, y, width, height1, step) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
scanDown = false;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
x += step;
|
|
79
|
-
idx += step;
|
|
17
|
+
let idx = y * width + x;
|
|
18
|
+
if (visited.at(idx))
|
|
19
|
+
return;
|
|
20
|
+
let scanUp = false;
|
|
21
|
+
let scanDown = false;
|
|
22
|
+
while (x >= 0 && x < width && pred(x, y)) {
|
|
23
|
+
visited.setAt(idx);
|
|
24
|
+
yield [x, y];
|
|
25
|
+
if (y > 0) {
|
|
26
|
+
if (pred(x, y - 1) && !scanUp) {
|
|
27
|
+
queue.push([x, y - 1]);
|
|
28
|
+
scanUp = true;
|
|
29
|
+
} else {
|
|
30
|
+
scanUp = false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (y < height1) {
|
|
34
|
+
if (pred(x, y + 1) && !scanDown) {
|
|
35
|
+
queue.push([x, y + 1]);
|
|
36
|
+
scanDown = true;
|
|
37
|
+
} else {
|
|
38
|
+
scanDown = false;
|
|
39
|
+
}
|
|
80
40
|
}
|
|
41
|
+
x += step;
|
|
42
|
+
idx += step;
|
|
43
|
+
}
|
|
81
44
|
}
|
|
45
|
+
export {
|
|
46
|
+
floodFill
|
|
47
|
+
};
|
package/hilbert.js
CHANGED
|
@@ -1,65 +1,45 @@
|
|
|
1
1
|
import { __opts } from "./utils.js";
|
|
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
|
-
t = (s & swap) ^ comp;
|
|
43
|
-
s = s ^ sr ^ t ^ (t << 1);
|
|
44
|
-
// Clear out any junk on the left (unpad).
|
|
45
|
-
s = s & ((1 << (2 * hOrder)) - 1);
|
|
46
|
-
// Now "unshuffle" to separate the x and y bits.
|
|
47
|
-
t = (s ^ (s >>> 1)) & 0x22222222;
|
|
48
|
-
s = s ^ t ^ (t << 1);
|
|
49
|
-
t = (s ^ (s >>> 2)) & 0x0c0c0c0c;
|
|
50
|
-
s = s ^ t ^ (t << 2);
|
|
51
|
-
t = (s ^ (s >>> 4)) & 0x00f000f0;
|
|
52
|
-
s = s ^ t ^ (t << 4);
|
|
53
|
-
t = (s ^ (s >>> 8)) & 0x0000ff00;
|
|
54
|
-
s = s ^ t ^ (t << 8);
|
|
55
|
-
// Assign the two halves to x and y.
|
|
56
|
-
hx = s >>> 16;
|
|
57
|
-
hy = s & 0xffff;
|
|
58
|
-
hIndex++;
|
|
59
|
-
} while (
|
|
60
|
-
// Dont't emit any outside cells
|
|
61
|
-
(hx >= cols || hy >= rows || hx < 0 || hy < 0) &&
|
|
62
|
-
hIndex < numBuckets);
|
|
63
|
-
yield tx(hx, hy);
|
|
64
|
-
}
|
|
2
|
+
function* hilbert2d(opts) {
|
|
3
|
+
const { cols, rows, tx } = __opts(opts);
|
|
4
|
+
let hIndex = 0;
|
|
5
|
+
let hOrder = 0;
|
|
6
|
+
while ((1 << hOrder < cols || 1 << hOrder < rows) && hOrder < 15)
|
|
7
|
+
hOrder++;
|
|
8
|
+
const numBuckets = 1 << 2 * hOrder;
|
|
9
|
+
for (let i = 0, n = cols * rows; i < n; i++) {
|
|
10
|
+
let hx, hy;
|
|
11
|
+
do {
|
|
12
|
+
let s, t, comp, swap, cs, sr;
|
|
13
|
+
s = hIndex | 1431655765 << 2 * hOrder;
|
|
14
|
+
sr = s >>> 1 & 1431655765;
|
|
15
|
+
cs = (s & 1431655765) + sr ^ 1431655765;
|
|
16
|
+
cs = cs ^ cs >>> 2;
|
|
17
|
+
cs = cs ^ cs >>> 4;
|
|
18
|
+
cs = cs ^ cs >>> 8;
|
|
19
|
+
cs = cs ^ cs >>> 16;
|
|
20
|
+
swap = cs & 1431655765;
|
|
21
|
+
comp = cs >>> 1 & 1431655765;
|
|
22
|
+
t = s & swap ^ comp;
|
|
23
|
+
s = s ^ sr ^ t ^ t << 1;
|
|
24
|
+
s = s & (1 << 2 * hOrder) - 1;
|
|
25
|
+
t = (s ^ s >>> 1) & 572662306;
|
|
26
|
+
s = s ^ t ^ t << 1;
|
|
27
|
+
t = (s ^ s >>> 2) & 202116108;
|
|
28
|
+
s = s ^ t ^ t << 2;
|
|
29
|
+
t = (s ^ s >>> 4) & 15728880;
|
|
30
|
+
s = s ^ t ^ t << 4;
|
|
31
|
+
t = (s ^ s >>> 8) & 65280;
|
|
32
|
+
s = s ^ t ^ t << 8;
|
|
33
|
+
hx = s >>> 16;
|
|
34
|
+
hy = s & 65535;
|
|
35
|
+
hIndex++;
|
|
36
|
+
} while (
|
|
37
|
+
// Dont't emit any outside cells
|
|
38
|
+
(hx >= cols || hy >= rows || hx < 0 || hy < 0) && hIndex < numBuckets
|
|
39
|
+
);
|
|
40
|
+
yield tx(hx, hy);
|
|
41
|
+
}
|
|
65
42
|
}
|
|
43
|
+
export {
|
|
44
|
+
hilbert2d
|
|
45
|
+
};
|
package/hvline.js
CHANGED
|
@@ -1,33 +1,39 @@
|
|
|
1
1
|
import { asInt } from "@thi.ng/api/typedarray";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
function* hline(x, y, len) {
|
|
3
|
+
[x, y, len] = asInt(x, y, len);
|
|
4
|
+
for (const xmax = x + len; x < xmax; x++) {
|
|
5
|
+
yield [x, y];
|
|
6
|
+
}
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
function* vline(x, y, len) {
|
|
9
|
+
[x, y, len] = asInt(x, y, len);
|
|
10
|
+
for (const ymax = y + len; y < ymax; y++) {
|
|
11
|
+
yield [x, y];
|
|
12
|
+
}
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
function* hlineClipped(x, y, len, left, top, right, bottom) {
|
|
15
|
+
[x, y, len] = asInt(x, y, len);
|
|
16
|
+
if (x >= right || y < top || y >= bottom)
|
|
17
|
+
return;
|
|
18
|
+
if (x < left) {
|
|
19
|
+
len += x - left;
|
|
20
|
+
x = left;
|
|
21
|
+
}
|
|
22
|
+
yield* hline(x, y, Math.min(len, right - x));
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
function* vlineClipped(x, y, len, left, top, right, bottom) {
|
|
25
|
+
[x, y, len] = asInt(x, y, len);
|
|
26
|
+
if (x < left || x >= right || y >= bottom)
|
|
27
|
+
return;
|
|
28
|
+
if (y < top) {
|
|
29
|
+
len += y - top;
|
|
30
|
+
y = top;
|
|
31
|
+
}
|
|
32
|
+
yield* vline(x, y, Math.min(len, bottom - y));
|
|
33
33
|
}
|
|
34
|
+
export {
|
|
35
|
+
hline,
|
|
36
|
+
hlineClipped,
|
|
37
|
+
vline,
|
|
38
|
+
vlineClipped
|
|
39
|
+
};
|
package/interleave.js
CHANGED
|
@@ -1,46 +1,21 @@
|
|
|
1
1
|
import { map } from "@thi.ng/transducers/map";
|
|
2
2
|
import { range2d } from "@thi.ng/transducers/range2d";
|
|
3
3
|
import { __opts } from "./utils.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* - 0, step, 2 * step, 3 * step...
|
|
12
|
-
* - 1, 2 * step + 1, 3 * step + 1...
|
|
13
|
-
* - etc.
|
|
14
|
-
*
|
|
15
|
-
* Also see {@link interleaveRows2d}.
|
|
16
|
-
*
|
|
17
|
-
* @param opts -
|
|
18
|
-
*/
|
|
19
|
-
export function* interleaveColumns2d(opts) {
|
|
20
|
-
const { cols, rows, tx } = __opts(opts);
|
|
21
|
-
const step = (opts.step != null ? opts.step : 2) | 0;
|
|
22
|
-
for (let j = 0; j < step; j++) {
|
|
23
|
-
yield* map((p) => tx(p[1], p[0]), range2d(0, rows, j, cols, 1, step));
|
|
24
|
-
}
|
|
4
|
+
function* interleaveColumns2d(opts) {
|
|
5
|
+
const { cols, rows, tx } = __opts(opts);
|
|
6
|
+
const step = (opts.step != null ? opts.step : 2) | 0;
|
|
7
|
+
for (let j = 0; j < step; j++) {
|
|
8
|
+
yield* map((p) => tx(p[1], p[0]), range2d(0, rows, j, cols, 1, step));
|
|
9
|
+
}
|
|
25
10
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* Returns rows in this order:
|
|
33
|
-
*
|
|
34
|
-
* - 0, step, 2 * step, 3 * step...
|
|
35
|
-
* - 1, 2 * step + 1, 3 * step + 1...
|
|
36
|
-
* - etc.
|
|
37
|
-
*
|
|
38
|
-
* @param opts -
|
|
39
|
-
*/
|
|
40
|
-
export function* interleaveRows2d(opts) {
|
|
41
|
-
const { cols, rows, tx } = __opts(opts);
|
|
42
|
-
const step = (opts.step != null ? opts.step : 2) | 0;
|
|
43
|
-
for (let j = 0; j < step; j++) {
|
|
44
|
-
yield* map((p) => tx(p[0], p[1]), range2d(0, cols, j, rows, 1, step));
|
|
45
|
-
}
|
|
11
|
+
function* interleaveRows2d(opts) {
|
|
12
|
+
const { cols, rows, tx } = __opts(opts);
|
|
13
|
+
const step = (opts.step != null ? opts.step : 2) | 0;
|
|
14
|
+
for (let j = 0; j < step; j++) {
|
|
15
|
+
yield* map((p) => tx(p[0], p[1]), range2d(0, cols, j, rows, 1, step));
|
|
16
|
+
}
|
|
46
17
|
}
|
|
18
|
+
export {
|
|
19
|
+
interleaveColumns2d,
|
|
20
|
+
interleaveRows2d
|
|
21
|
+
};
|
package/line.js
CHANGED
|
@@ -1,42 +1,32 @@
|
|
|
1
1
|
import { asInt } from "@thi.ng/api/typedarray";
|
|
2
2
|
import { liangBarsky } from "./clipping.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
if (t > dy) {
|
|
20
|
-
err += dy;
|
|
21
|
-
ax += sx;
|
|
22
|
-
}
|
|
3
|
+
function* line(ax, ay, bx, by) {
|
|
4
|
+
[ax, ay, bx, by] = asInt(ax, ay, bx, by);
|
|
5
|
+
const dx = Math.abs(bx - ax);
|
|
6
|
+
const dy = -Math.abs(by - ay);
|
|
7
|
+
const sx = ax < bx ? 1 : -1;
|
|
8
|
+
const sy = ay < by ? 1 : -1;
|
|
9
|
+
let err = dx + dy;
|
|
10
|
+
while (true) {
|
|
11
|
+
yield [ax, ay];
|
|
12
|
+
if (ax === bx && ay === by)
|
|
13
|
+
return;
|
|
14
|
+
let t = err << 1;
|
|
15
|
+
if (t < dx) {
|
|
16
|
+
err += dx;
|
|
17
|
+
ay += sy;
|
|
23
18
|
}
|
|
19
|
+
if (t > dy) {
|
|
20
|
+
err += dy;
|
|
21
|
+
ax += sx;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
24
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* @param x2 -
|
|
33
|
-
* @param y2 -
|
|
34
|
-
* @param left -
|
|
35
|
-
* @param top -
|
|
36
|
-
* @param right -
|
|
37
|
-
* @param bottom -
|
|
38
|
-
*/
|
|
39
|
-
export const lineClipped = (x1, y1, x2, y2, left, top, right, bottom) => {
|
|
40
|
-
const res = liangBarsky(x1, y1, x2, y2, left, top, right - 1, bottom - 1);
|
|
41
|
-
return res ? line(res[0], res[1], res[2], res[3]) : undefined;
|
|
25
|
+
const lineClipped = (x1, y1, x2, y2, left, top, right, bottom) => {
|
|
26
|
+
const res = liangBarsky(x1, y1, x2, y2, left, top, right - 1, bottom - 1);
|
|
27
|
+
return res ? line(res[0], res[1], res[2], res[3]) : void 0;
|
|
28
|
+
};
|
|
29
|
+
export {
|
|
30
|
+
line,
|
|
31
|
+
lineClipped
|
|
42
32
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/grid-iterators",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.36",
|
|
4
4
|
"description": "2D grid and shape iterators w/ multiple orderings",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,8 +24,10 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
28
|
"build:assets": "node tools/build-assets",
|
|
29
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
30
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
29
31
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc internal",
|
|
30
32
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
31
33
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -35,18 +37,18 @@
|
|
|
35
37
|
"tool:assets": "tools:node-esm tools/build-assets.ts"
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
|
-
"@thi.ng/api": "^8.9.
|
|
39
|
-
"@thi.ng/arrays": "^2.7.
|
|
40
|
-
"@thi.ng/binary": "^3.
|
|
41
|
-
"@thi.ng/bitfield": "^2.3.
|
|
42
|
-
"@thi.ng/errors": "^2.4.
|
|
43
|
-
"@thi.ng/morton": "^3.1.
|
|
44
|
-
"@thi.ng/random": "^3.6.
|
|
45
|
-
"@thi.ng/transducers": "^8.8.
|
|
40
|
+
"@thi.ng/api": "^8.9.12",
|
|
41
|
+
"@thi.ng/arrays": "^2.7.8",
|
|
42
|
+
"@thi.ng/binary": "^3.4.0",
|
|
43
|
+
"@thi.ng/bitfield": "^2.3.10",
|
|
44
|
+
"@thi.ng/errors": "^2.4.6",
|
|
45
|
+
"@thi.ng/morton": "^3.1.57",
|
|
46
|
+
"@thi.ng/random": "^3.6.18",
|
|
47
|
+
"@thi.ng/transducers": "^8.8.15"
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
50
|
"@microsoft/api-extractor": "^7.38.3",
|
|
49
|
-
"
|
|
51
|
+
"esbuild": "^0.19.8",
|
|
50
52
|
"rimraf": "^5.0.5",
|
|
51
53
|
"tools": "^0.0.1",
|
|
52
54
|
"typedoc": "^0.25.4",
|
|
@@ -165,5 +167,5 @@
|
|
|
165
167
|
],
|
|
166
168
|
"year": 2019
|
|
167
169
|
},
|
|
168
|
-
"gitHead": "
|
|
170
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
169
171
|
}
|