@thi.ng/grid-iterators
Version:
2D grid and shape iterators w/ multiple orderings
25 lines (24 loc) • 482 B
JavaScript
import { __opts } from "./utils.js";
function* diagonal2d(opts) {
const { cols, rows, tx } = __opts(opts);
const num = cols * rows - 1;
for (let x = 0, y = 0, nx = 1, ny = 0, i = 0; i <= num; i++) {
yield tx(x, y);
if (i !== num) {
do {
if (y === ny) {
y = 0;
x = nx;
ny++;
nx++;
} else {
x--;
y++;
}
} while (y >= rows || x >= cols);
}
}
}
export {
diagonal2d
};