UNPKG

@thi.ng/grid-iterators

Version:

2D grid and shape iterators w/ multiple orderings

55 lines (54 loc) 1.23 kB
import { defBitField } from "@thi.ng/bitfield/bitfield"; function* diamondSquare(exp) { const size = 1 << exp; const size1 = size + 1; const s2 = size >> 1; let res = size; const idx = defBitField(size1 * size1); const acc = []; const $ = (x, y) => { !idx.setAt(x + y * size1) && acc.push([x, y]); }; $(0, 0); $(res, 0); $(res, res); $(0, res); yield* acc; while (res > 1) { const r2 = res >> 1; for (let y = 0; y <= s2; y += res) { const y1 = y + r2; const y2 = y + res; for (let x = r2; x <= s2; x += res) { acc.length = 0; const x1 = x - r2; const x2 = x + r2; $(x, y); $(size - x, y); $(size - x, size - y); $(x, size - y); $(x2, y1); $(size - x2, y1); $(size - x2, size - y1); $(x2, size - y1); $(x, y2); $(size - x, y2); $(size - x, size - y2); $(x, size - y2); $(x1, y1); $(size - x1, y1); $(size - x1, size - y1); $(x1, size - y1); $(x, y1); $(size - x, y1); $(size - x, size - y1); $(x, size - y1); yield* acc; } } res = r2; } } export { diamondSquare };