@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/random.js
CHANGED
|
@@ -2,19 +2,13 @@ import { shuffle } from "@thi.ng/arrays/shuffle";
|
|
|
2
2
|
import { SYSTEM } from "@thi.ng/random/system";
|
|
3
3
|
import { range } from "@thi.ng/transducers/range";
|
|
4
4
|
import { __opts } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
12
|
-
* @param opts -
|
|
13
|
-
*/
|
|
14
|
-
export function* random2d(opts) {
|
|
15
|
-
const { cols, rows, tx } = __opts(opts);
|
|
16
|
-
const rnd = opts.rnd || SYSTEM;
|
|
17
|
-
for (let i of shuffle([...range(cols * rows)], undefined, rnd)) {
|
|
18
|
-
yield tx(i % cols, (i / cols) | 0);
|
|
19
|
-
}
|
|
5
|
+
function* random2d(opts) {
|
|
6
|
+
const { cols, rows, tx } = __opts(opts);
|
|
7
|
+
const rnd = opts.rnd || SYSTEM;
|
|
8
|
+
for (let i of shuffle([...range(cols * rows)], void 0, rnd)) {
|
|
9
|
+
yield tx(i % cols, i / cols | 0);
|
|
10
|
+
}
|
|
20
11
|
}
|
|
12
|
+
export {
|
|
13
|
+
random2d
|
|
14
|
+
};
|
package/row-ends.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
import { __opts } from "./utils.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
let { cols, rows, tx } = __opts(opts);
|
|
10
|
-
cols--;
|
|
11
|
-
for (let y = 0; y < rows; y++) {
|
|
12
|
-
yield tx(0, y);
|
|
13
|
-
yield tx(cols, y);
|
|
14
|
-
}
|
|
2
|
+
function* rowEnds2d(opts) {
|
|
3
|
+
let { cols, rows, tx } = __opts(opts);
|
|
4
|
+
cols--;
|
|
5
|
+
for (let y = 0; y < rows; y++) {
|
|
6
|
+
yield tx(0, y);
|
|
7
|
+
yield tx(cols, y);
|
|
8
|
+
}
|
|
15
9
|
}
|
|
10
|
+
export {
|
|
11
|
+
rowEnds2d
|
|
12
|
+
};
|
package/rows.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
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
|
-
export const rows2d = (opts) => {
|
|
11
|
-
const { cols, rows, tx } = __opts(opts);
|
|
12
|
-
return map((p) => tx(p[0], p[1]), range2d(cols, rows));
|
|
4
|
+
const rows2d = (opts) => {
|
|
5
|
+
const { cols, rows, tx } = __opts(opts);
|
|
6
|
+
return map((p) => tx(p[0], p[1]), range2d(cols, rows));
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
rows2d
|
|
13
10
|
};
|
package/spiral.js
CHANGED
|
@@ -1,48 +1,39 @@
|
|
|
1
1
|
import { __opts } from "./utils.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const { cols, rows, tx } = __opts(opts);
|
|
13
|
-
const num = cols * rows;
|
|
14
|
-
const center = (Math.min(cols, rows) - 1) >> 1;
|
|
15
|
-
for (let i = 0; i < num; i++) {
|
|
16
|
-
let nx = cols;
|
|
17
|
-
let ny = rows;
|
|
18
|
-
while (i < nx * ny) {
|
|
19
|
-
nx--;
|
|
20
|
-
ny--;
|
|
21
|
-
}
|
|
22
|
-
const nxny = nx * ny;
|
|
23
|
-
const minnxny = Math.min(nx, ny);
|
|
24
|
-
const m2 = minnxny >> 1;
|
|
25
|
-
let bx, by;
|
|
26
|
-
if (minnxny & 1) {
|
|
27
|
-
if (i <= nxny + ny) {
|
|
28
|
-
bx = nx - m2;
|
|
29
|
-
by = -m2 + i - nxny;
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
bx = nx - m2 - (i - (nxny + ny));
|
|
33
|
-
by = ny - m2;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
if (i <= nxny + ny) {
|
|
38
|
-
bx = -m2;
|
|
39
|
-
by = ny - m2 - (i - nxny);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
bx = -m2 + (i - (nxny + ny));
|
|
43
|
-
by = -m2;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
yield tx(bx + center, by + center);
|
|
2
|
+
function* spiral2d(opts) {
|
|
3
|
+
const { cols, rows, tx } = __opts(opts);
|
|
4
|
+
const num = cols * rows;
|
|
5
|
+
const center = Math.min(cols, rows) - 1 >> 1;
|
|
6
|
+
for (let i = 0; i < num; i++) {
|
|
7
|
+
let nx = cols;
|
|
8
|
+
let ny = rows;
|
|
9
|
+
while (i < nx * ny) {
|
|
10
|
+
nx--;
|
|
11
|
+
ny--;
|
|
47
12
|
}
|
|
13
|
+
const nxny = nx * ny;
|
|
14
|
+
const minnxny = Math.min(nx, ny);
|
|
15
|
+
const m2 = minnxny >> 1;
|
|
16
|
+
let bx, by;
|
|
17
|
+
if (minnxny & 1) {
|
|
18
|
+
if (i <= nxny + ny) {
|
|
19
|
+
bx = nx - m2;
|
|
20
|
+
by = -m2 + i - nxny;
|
|
21
|
+
} else {
|
|
22
|
+
bx = nx - m2 - (i - (nxny + ny));
|
|
23
|
+
by = ny - m2;
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
if (i <= nxny + ny) {
|
|
27
|
+
bx = -m2;
|
|
28
|
+
by = ny - m2 - (i - nxny);
|
|
29
|
+
} else {
|
|
30
|
+
bx = -m2 + (i - (nxny + ny));
|
|
31
|
+
by = -m2;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
yield tx(bx + center, by + center);
|
|
35
|
+
}
|
|
48
36
|
}
|
|
37
|
+
export {
|
|
38
|
+
spiral2d
|
|
39
|
+
};
|
package/transforms.js
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*/
|
|
19
|
-
export const compTransforms = (a, b) => (cols, rows) => {
|
|
20
|
-
const $a = a(cols, rows);
|
|
21
|
-
const $b = b(cols, rows);
|
|
22
|
-
return (x, y) => $b(...$a(x, y));
|
|
1
|
+
const ident = () => (x, y) => [x, y];
|
|
2
|
+
const flipX = (cols) => (x, y) => [cols - 1 - x, y];
|
|
3
|
+
const flipY = (_, rows) => (x, y) => [x, rows - 1 - y];
|
|
4
|
+
const flipXY = (cols, rows) => (x, y) => [cols - 1 - x, rows - 1 - y];
|
|
5
|
+
const swapXY = () => (x, y) => [y, x];
|
|
6
|
+
const compTransforms = (a, b) => (cols, rows) => {
|
|
7
|
+
const $a = a(cols, rows);
|
|
8
|
+
const $b = b(cols, rows);
|
|
9
|
+
return (x, y) => $b(...$a(x, y));
|
|
10
|
+
};
|
|
11
|
+
export {
|
|
12
|
+
compTransforms,
|
|
13
|
+
flipX,
|
|
14
|
+
flipXY,
|
|
15
|
+
flipY,
|
|
16
|
+
ident,
|
|
17
|
+
swapXY
|
|
23
18
|
};
|
package/utils.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
// thing:no-export
|
|
2
1
|
import { asInt } from "@thi.ng/api/typedarray";
|
|
3
2
|
import { ident } from "./transforms.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
const __opts = (opts) => {
|
|
4
|
+
let { cols, rows, tx } = { rows: opts.cols, tx: ident, ...opts };
|
|
5
|
+
[cols, rows] = asInt(cols, rows);
|
|
6
|
+
return { cols, rows, tx: tx(cols, rows) };
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
__opts
|
|
8
10
|
};
|
package/zcurve.js
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
import { ceilPow2 } from "@thi.ng/binary/pow";
|
|
2
2
|
import { demux2 } from "@thi.ng/morton/mux";
|
|
3
3
|
import { __opts } from "./utils.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
export function* zcurve2d(opts) {
|
|
13
|
-
const { cols, rows, tx } = __opts(opts);
|
|
14
|
-
const max = ceilPow2(Math.pow(Math.max(cols, rows), 2));
|
|
15
|
-
for (let i = 0; i < max; i++) {
|
|
16
|
-
const [x, y] = demux2(i);
|
|
17
|
-
if (x < cols && y < rows) {
|
|
18
|
-
yield tx(x, y);
|
|
19
|
-
}
|
|
4
|
+
function* zcurve2d(opts) {
|
|
5
|
+
const { cols, rows, tx } = __opts(opts);
|
|
6
|
+
const max = ceilPow2(Math.pow(Math.max(cols, rows), 2));
|
|
7
|
+
for (let i = 0; i < max; i++) {
|
|
8
|
+
const [x, y] = demux2(i);
|
|
9
|
+
if (x < cols && y < rows) {
|
|
10
|
+
yield tx(x, y);
|
|
20
11
|
}
|
|
12
|
+
}
|
|
21
13
|
}
|
|
14
|
+
export {
|
|
15
|
+
zcurve2d
|
|
16
|
+
};
|
package/zigzag-columns.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { __opts } from "./utils.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export function* zigzagColumns2d(opts) {
|
|
12
|
-
const { cols, rows, tx } = __opts(opts);
|
|
13
|
-
const num = cols * rows;
|
|
14
|
-
for (let i = 0; i < num; i++) {
|
|
15
|
-
const x = (i / rows) | 0;
|
|
16
|
-
let y = i % rows;
|
|
17
|
-
x & 1 && (y = rows - 1 - y);
|
|
18
|
-
yield tx(x, y);
|
|
19
|
-
}
|
|
2
|
+
function* zigzagColumns2d(opts) {
|
|
3
|
+
const { cols, rows, tx } = __opts(opts);
|
|
4
|
+
const num = cols * rows;
|
|
5
|
+
for (let i = 0; i < num; i++) {
|
|
6
|
+
const x = i / rows | 0;
|
|
7
|
+
let y = i % rows;
|
|
8
|
+
x & 1 && (y = rows - 1 - y);
|
|
9
|
+
yield tx(x, y);
|
|
10
|
+
}
|
|
20
11
|
}
|
|
12
|
+
export {
|
|
13
|
+
zigzagColumns2d
|
|
14
|
+
};
|
package/zigzag-diagonal.js
CHANGED
|
@@ -1,36 +1,31 @@
|
|
|
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
|
-
down = !down;
|
|
26
|
-
dx *= -1;
|
|
27
|
-
dy *= -1;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
x += dx;
|
|
31
|
-
y += dy;
|
|
32
|
-
}
|
|
33
|
-
} while (x >= cols || y >= rows);
|
|
2
|
+
function* zigzagDiagonal2d(opts) {
|
|
3
|
+
const { cols, rows, tx } = __opts(opts);
|
|
4
|
+
const num = cols * rows - 1;
|
|
5
|
+
for (let x = 0, y = 0, ny = 0, dx = -1, dy = 1, d = 0, down = true, i = 0; i <= num; i++) {
|
|
6
|
+
yield tx(x, y);
|
|
7
|
+
if (i !== num) {
|
|
8
|
+
do {
|
|
9
|
+
if (y === ny) {
|
|
10
|
+
if (down) {
|
|
11
|
+
y++;
|
|
12
|
+
d++;
|
|
13
|
+
ny = 0;
|
|
14
|
+
} else {
|
|
15
|
+
x++;
|
|
16
|
+
ny = ++d;
|
|
17
|
+
}
|
|
18
|
+
down = !down;
|
|
19
|
+
dx *= -1;
|
|
20
|
+
dy *= -1;
|
|
21
|
+
} else {
|
|
22
|
+
x += dx;
|
|
23
|
+
y += dy;
|
|
34
24
|
}
|
|
25
|
+
} while (x >= cols || y >= rows);
|
|
35
26
|
}
|
|
27
|
+
}
|
|
36
28
|
}
|
|
29
|
+
export {
|
|
30
|
+
zigzagDiagonal2d
|
|
31
|
+
};
|
package/zigzag-rows.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { __opts } from "./utils.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export function* zigzagRows2d(opts) {
|
|
12
|
-
const { cols, rows, tx } = __opts(opts);
|
|
13
|
-
const num = cols * rows;
|
|
14
|
-
for (let i = 0; i < num; i++) {
|
|
15
|
-
let x = i % cols;
|
|
16
|
-
const y = (i / cols) | 0;
|
|
17
|
-
y & 1 && (x = cols - 1 - x);
|
|
18
|
-
yield tx(x, y);
|
|
19
|
-
}
|
|
2
|
+
function* zigzagRows2d(opts) {
|
|
3
|
+
const { cols, rows, tx } = __opts(opts);
|
|
4
|
+
const num = cols * rows;
|
|
5
|
+
for (let i = 0; i < num; i++) {
|
|
6
|
+
let x = i % cols;
|
|
7
|
+
const y = i / cols | 0;
|
|
8
|
+
y & 1 && (x = cols - 1 - x);
|
|
9
|
+
yield tx(x, y);
|
|
10
|
+
}
|
|
20
11
|
}
|
|
12
|
+
export {
|
|
13
|
+
zigzagRows2d
|
|
14
|
+
};
|