Skip to content

Commit

Permalink
feat(grid-iterators): add zigzagDiagonal(), update readme, rename files
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 26, 2019
1 parent b834722 commit 5630055
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 6 deletions.
Binary file added assets/grid-iterators/zigzagDiagonal2d-small.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 17 additions & 3 deletions packages/grid-iterators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project is part of the
- [Outward spiral](#outward-spiral)
- [Z-curve](#z-curve)
- [Zigzag columns](#zigzag-columns)
- [Zigzag diagonal](#zigzag-diagonal)
- [Zigzag rows](#zigzag-rows)
- [Installation](#installation)
- [Dependencies](#dependencies)
Expand All @@ -29,7 +30,7 @@ This project is part of the

## About

Collection of 2D grid iterators, providing the following orderings:
Collection of 2D grid iterators, providing the 10 following orderings:

### Diagonal

Expand All @@ -49,18 +50,25 @@ Collection of 2D grid iterators, providing the following orderings:

[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/interleave.ts)

Supports custom strides... example uses `step = 4`

### Interleave rows

![anim](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/grid-iterators/interleaverows2d-small.gif)

[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/interleave.ts)

Supports custom strides... example uses `step = 4`

### Random

![anim](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/grid-iterators/random2d-small.gif)

[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/random.ts)

Supports custom PRNG implementations via `IRandom` interface defined in
[@thi.ng/random](https://github.com/thi-ng/umbrella/tree/master/packages/random)

### Outward spiral

![anim](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/grid-iterators/spiral2d-small.gif)
Expand All @@ -77,13 +85,19 @@ Collection of 2D grid iterators, providing the following orderings:

![anim](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/grid-iterators/zigzagcolumns2d-small.gif)

[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/columns.ts)
[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/zigzag-columns.ts)

### Zigzag diagonal

![anim](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/grid-iterators/zigzagdiagonal2d-small.gif)

[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/zigzag-diagonal.ts)

### Zigzag rows

![anim](https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/grid-iterators/zigzagrows2d-small.gif)

[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/rows.ts)
[Source](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators/src/zigzag-rows.ts)

Some functions have been ported from [Christopher
Kulla](https://fpsunflower.github.io/ckulla/)'s Java-based [Sunflow
Expand Down
5 changes: 3 additions & 2 deletions packages/grid-iterators/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export * from "./columns";
export * from "./diagonal";
export * from "./hilbert";
export * from "./interleave";
export * from "./random";
export * from "./rows";
export * from "./spiral";
export * from "./zcurve";
export * from "./zigzag-columns";
export * from "./zigzag-diagonal";
export * from "./zigzag-rows";
File renamed without changes.
37 changes: 37 additions & 0 deletions packages/grid-iterators/src/zigzag-diagonal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Similar to `diagonal2d`, but yields 2D grid coordinates in zigzag
* diagonal order starting at [0,0] and using given `cols` and `rows`.
*
* @param cols
* @param rows
*/
export function* zigzagDiagonal2d(cols: number, rows = cols) {
const num = cols * rows - 1;
for (
let x = 0, y = 0, ny = 0, dx = -1, dy = 1, d = 0, down = true, i = 0;
i <= num;
i++
) {
yield [x, y];
if (i !== num) {
do {
if (y === ny) {
if (down) {
y++;
d++;
ny = 0;
} else {
x++;
ny = ++d;
}
down = !down;
dx *= -1;
dy *= -1;
} else {
x += dx;
y += dy;
}
} while (x >= cols || y >= rows);
}
}
}
File renamed without changes.
4 changes: 3 additions & 1 deletion packages/grid-iterators/tools/build-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ try {
[
gi.diagonal2d,
gi.hilbert2d,
gi.interleaveColumns2d,
gi.interleaveRows2d,
gi.random2d,
gi.spiral2d,
gi.zcurve2d,
gi.zigzagColumns2d,
// gi.zigzagDiagonal2d,
gi.zigzagDiagonal2d,
gi.zigzagRows2d
].forEach((fn) => {
console.log(`generating ${fn.name}...`);
Expand Down

0 comments on commit 5630055

Please sign in to comment.