Skip to content

Commit

Permalink
feat(grid-iterators): make row args optional
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 25, 2019
1 parent 4eb9c7e commit 60dccfc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/grid-iterators/src/columns2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @param rows
*
*/
export function* zigzagColumns2d(cols: number, rows: number) {
export function* zigzagColumns2d(cols: number, rows = cols) {
const num = cols * rows;
for (let i = 0; i < num; i++) {
const x = (i / rows) | 0;
Expand Down
24 changes: 13 additions & 11 deletions packages/grid-iterators/src/diagonal2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ export function* diagonal2d(cols: number, rows: number) {
const num = cols * rows - 1;
for (let x = 0, y = 0, nx = 1, ny = 0, i = 0; i <= num; i++) {
yield [x, y];
do {
if (y === ny) {
y = 0;
x = nx;
ny++;
nx++;
} else {
x--;
y++;
}
} while ((y >= rows || x >= cols) && i != num);
if (i != num) {
do {
if (y === ny) {
y = 0;
x = nx;
ny++;
nx++;
} else {
x--;
y++;
}
} while (y >= rows || x >= cols);
}
}
}
2 changes: 1 addition & 1 deletion packages/grid-iterators/src/hilbert2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @param cols
* @param rows
*/
export function* hilbert2d(cols: number, rows: number) {
export function* hilbert2d(cols: number, rows = cols) {
let hIndex = 0; // hilbert curve index
let hOrder = 0; // hilbert curve order
// fit to number of buckets
Expand Down
2 changes: 1 addition & 1 deletion packages/grid-iterators/src/rows2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @param rows
*
*/
export function* zigzagRows2d(cols: number, rows: number) {
export function* zigzagRows2d(cols: number, rows = cols) {
const num = cols * rows;
for (let i = 0; i < num; i++) {
let x = i % cols;
Expand Down
2 changes: 1 addition & 1 deletion packages/grid-iterators/src/spiral2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @param cols
* @param rows
*/
export function* spiral2d(cols: number, rows: number) {
export function* spiral2d(cols: number, rows = cols) {
const num = cols * rows;
const center = (Math.min(cols, rows) - 1) >> 1;
for (let i = 0; i < num; i++) {
Expand Down

0 comments on commit 60dccfc

Please sign in to comment.