Two deceptively tricky geometry problems come up when building client-side puzzle and craft pattern studios in the browser:
- Nonogram (Picross) Uniqueness: Converting an arbitrary silhouette or photo into a grid often produces ambiguous
2×2"switching components" where players are forced to guess. How do you prove a10×10or15×15puzzle is 100% logically line-solvable in under1ms? - Scroll-Saw & Stencil Dropout Islands: Thresholding a portrait into a cut template creates interior wood or mylar islands (like the center of an
Oor an eye highlight) that fall onto the floor once cut unless connected to the outer frame by structural bridges.
Here are both zero-dependency TypeScript algorithms.
1. Sub-Millisecond Nonogram Line Propagation With Bitmasks
For boards up to 25×25, every row or column fits inside a single 32-bit integer. Given clue runs [r_1, r_2, ...] and two constraint masks (knownFilled where bit i = 1 means cell i is definitely filled, and knownEmpty where bit i = 1 means cell i is definitely crossed out), we recursively enumerate all valid run placements along the line and accumulate:
-
mustFill = placement_1 & placement_2 & ...(bits set in every valid placement), and -
mustEmpty = (~placement_1) & (~placement_2) & ...(bits clear in every valid placement).
export interface LineDeduction { contradiction: boolean; mustFillMask: number; mustEmptyMask: number; } export function deduceNonogramLine( length: number, runs: number[], knownFilledMask: number, knownEmptyMask: number ): LineDeduction { const fullMask = (1 << length) - 1; let validCount = 0; let andFilled = fullMask; let andEmpty = fullMask; const cleanRuns = runs.filter((r) => r > 0); if (cleanRuns.length === 0) { const ok = (knownFilledMask & fullMask) === 0; return { contradiction: !ok, mustFillMask: 0, mustEmptyMask: ok ? fullMask : 0, }; } // Remaining minimum cells needed from run index idx..end (including 1-cell gaps) const minTail = new Int32Array(cleanRuns.length); for (let i = cleanRuns.length - 1; i >= 0; i--) { minTail[i] = cleanRuns[i] + (i + 1 < cleanRuns.length ? 1 + minTail[i + 1] : 0); } function dfs(runIdx: number, startPos: number, currentMask: number) { if (runIdx === cleanRuns.length) { // No known-filled cells may appear after the last placed run const trailingFilled = knownFilledMask & (~((1 << startPos) - 1) & fullMask); if (trailingFilled === 0) { validCount++; andFilled &= currentMask; andEmpty &= ~currentMask & fullMask; } return; } const len = cleanRuns[runIdx]; const maxStart = length - minTail[runIdx]; for (let pos = startPos; pos <= maxStart; pos++) { // Gap [startPos .. pos - 1] cannot cover a known-filled cell if (pos > startPos) { const gapMask = ((1 << pos) - 1) ^ ((1 << startPos) - 1); if ((gapMask & knownFilledMask) !== 0) break; } const runMask = ((1 << len) - 1) << pos; // Run cannot overlap a known-empty cell if ((runMask & knownEmptyMask) !== 0) continue; // Mandatory 1-cell separator right after this run (unless at line end) const sepPos = pos + len; if (sepPos < length && (knownFilledMask & (1 << sepPos)) !== 0) { continue; } dfs(runIdx + 1, sepPos + 1, currentMask | runMask); } } dfs(0, 0, 0); return { contradiction: validCount === 0, mustFillMask: validCount > 0 ? andFilled : 0, mustEmptyMask: validCount > 0 ? andEmpty : 0, }; } Iterating deduceNonogramLine across dirty rows and columns solves pure line-solvable 15×15 boards in ~0.3–0.6 ms directly on the main thread. You can test the live bitmask line-hint engine, photo-to-grid uniqueness stabilizer, and step-by-step deduction log at NonogramGrid (Photo Nonogram Generator · Step-by-Step Nonogram Solver).
2. Detecting & Auto-Bridging Floating Stencil / Fretwork Islands (2D BFS)
In woodworking scroll-saw fretwork and mylar stencil cutting, keep = 1 pixels represent solid wood (or stencil material) and keep = 0 pixels represent sawn-out waste holes. Any connected component of keep = 1 pixels that never touches the outer border frame is an unanchored dropout island that will fall loose as soon as the surrounding loop is cut.
A two-pass 4-connected BFS identifies safe border-anchored material (label = 1) versus each floating island (label >= 2), and then connects each island to the nearest anchored pixel with a minimum-length structural tab of radius bridgeRadiusPx:
export function findAndBridgeFloatingIslands( keepMask: Uint8Array, // 1 = keep wood/mylar, 0 = cut waste width: number, height: number, bridgeRadiusPx = 3 ) { const labels = new Int32Array(width * height); const queue = new Int32Array(width * height); const dirs = [1, -1, width, -width]; // Pass 1: Flood-fill all border-anchored "keep" pixels with label = 1 let head = 0; let tail = 0; for (let x = 0; x < width; x++) { for (const y of [0, height - 1]) { const idx = y * width + x; if (keepMask[idx] === 1 && labels[idx] === 0) { labels[idx] = 1; queue[tail++] = idx; } } } for (let y = 0; y < height; y++) { for (const x of [0, width - 1]) { const idx = y * width + x; if (keepMask[idx] === 1 && labels[idx] === 0) { labels[idx] = 1; queue[tail++] = idx; } } } while (head < tail) { const cur = queue[head++]; const cx = cur % width; for (const d of dirs) { if ((d === 1 && cx === width - 1) || (d === -1 && cx === 0)) continue; const nxt = cur + d; if (nxt >= 0 && nxt < width * height && keepMask[nxt] === 1 && labels[nxt] === 0) { labels[nxt] = 1; queue[tail++] = nxt; } } } // Pass 2: Collect unanchored islands (keepMask === 1 && labels === 0) let nextLabel = 2; const islands: number[][] = []; for (let i = 0; i < width * height; i++) { if (keepMask[i] === 1 && labels[i] === 0) { const pixels: number[] = []; head = 0; tail = 0; labels[i] = nextLabel; queue[tail++] = i; while (head < tail) { const cur = queue[head++]; pixels.push(cur); const cx = cur % width; for (const d of dirs) { if ((d === 1 && cx === width - 1) || (d === -1 && cx === 0)) continue; const nxt = cur + d; if (nxt >= 0 && nxt < width * height && keepMask[nxt] === 1 && labels[nxt] === 0) { labels[nxt] = nextLabel; queue[tail++] = nxt; } } } islands.push(pixels); nextLabel++; } } return { floatingIslandCount: islands.length, islands, labels }; } Once boundary pixels of each floating island are paired with the closest label === 1 pixel (processing outer islands inward so nested islands chain cleanly into newly anchored parents), stamping a capsule of radius bridgeRadiusPx (~2.5–4.0 mm at 1:1 print scale) turns fragile photo silhouettes into single-piece fretwork plaques.
You can try the interactive island highlighter, manual bridge brush, #58 pilot drill crosshair placer, and 1:1 tiled poster exporter in the live Scroll Saw Pattern Generator (Connected Name Plaque Maker · Auto-Bridge Photo Stencil Maker · Scroll Saw Blade & TPI Chart).

