-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grid-iterators): add new iterators
- add columns, rows - add columnEnds, diagonalEnds, rowEnds
- Loading branch information
1 parent
7baa3ba
commit e08985e
Showing
8 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Filtered version of {@link columns2d}, only including end points of | ||
* each column. | ||
* | ||
* @param cols - | ||
* @param rows - | ||
*/ | ||
export function* columnEnds2d(cols: number, rows = cols) { | ||
rows--; | ||
for (let x = 0; x < cols; x++) { | ||
yield [x, 0]; | ||
yield [x, rows]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { map, range2d } from "@thi.ng/transducers"; | ||
import { swapxy } from "./swap"; | ||
|
||
/** | ||
* Yields sequence of 2D grid coordinates in column-major order. | ||
* | ||
* @param cols | ||
* @param rows | ||
*/ | ||
export const columns2d = (cols: number, rows = cols) => | ||
map(swapxy, range2d(rows, cols)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { diagonal2d } from "./diagonal"; | ||
|
||
/** | ||
* Filtered version of {@link diagonal2d}, only including end points of | ||
* the diagonals, apart from the very first and last points: `[0,0]` and | ||
* `[cols-1, rows-1]`. | ||
* | ||
* @remarks | ||
* `cols` and `rows` MUST be both >= 2. | ||
* | ||
* @param cols - | ||
* @param rows - | ||
*/ | ||
export function* diagonalEnds2d(cols: number, rows = cols) { | ||
const num = cols * rows - 1; | ||
const maxX = cols - 1; | ||
const maxY = rows - 1; | ||
let i = 0; | ||
for (let p of diagonal2d(cols, rows)) { | ||
if (i > 0 && i < num) { | ||
const [x, y] = p; | ||
if (x === 0 || x === maxX || y === 0 || y === maxY) yield p; | ||
} | ||
i++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Filtered version of {@link rows2d}, only including end points of | ||
* each row. | ||
* | ||
* @param cols - | ||
* @param rows - | ||
*/ | ||
export function* rowEnds2d(cols: number, rows = cols) { | ||
cols--; | ||
for (let y = 0; y < rows; y++) { | ||
yield [0, y]; | ||
yield [cols, y]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { range2d } from "@thi.ng/transducers"; | ||
|
||
/** | ||
* Yields sequence of 2D grid coordinates in row-major order. Same as | ||
* {@link @thi.ng/transducers#range2d}. | ||
* | ||
* @param cols | ||
* @param rows | ||
*/ | ||
export const rows2d = (cols: number, rows = cols) => range2d(cols, rows); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Swaps XY in-place. | ||
* | ||
* @param p | ||
* | ||
* @internal | ||
*/ | ||
export const swapxy = (p: number[]) => { | ||
const t = p[0]; | ||
p[0] = p[1]; | ||
p[1] = t; | ||
return p; | ||
}; |