UNPKG

@rickosborne/hexgrid

Version:

Rick Osborne's collection of hexagonal grid-related code.

37 lines (36 loc) 1.32 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { memoizeBinary, memoizeUnary } from "@rickosborne/foundation"; const hexReachable = /* @__PURE__ */ __name((start, steps, directions, adder, identity, isBlocked, onPoint) => { const visited = /* @__PURE__ */ new Set(); visited.add(identity(start)); const reachable = []; reachable.push({ distance: 0, point: start }); const fringes = []; fringes.push([start]); const memoizedIsBlocked = memoizeBinary(isBlocked); const memoizedIdentity = memoizeUnary(identity); for (let distance = 1; distance <= steps; distance++) { const out = []; fringes.push(out); for (let prior of fringes[distance - 1]) { for (let direction of directions) { const point = adder(prior, direction); const id = memoizedIdentity(point); if (!visited.has(id) && !memoizedIsBlocked(point, id)) { visited.add(id); out.push(point); reachable.push({ direction, distance, point, prior }); if (onPoint != null) { onPoint(point, distance, prior, direction); } } } } } return reachable; }, "hexReachable"); export { hexReachable }; //# sourceMappingURL=hex-reachable.mjs.map