|
1 | | -function getOutline(grid) { |
| 1 | +/*export function getOutline(grid) { |
2 | 2 | const outline: number[][] = []; |
3 | 3 | const rows = grid.length; |
4 | 4 | const cols = grid[0].length; |
@@ -35,5 +35,129 @@ function getOutline(grid) { |
35 | 35 | } |
36 | 36 | } |
37 | 37 |
|
| 38 | + return outline; |
| 39 | +} */ |
| 40 | + |
| 41 | +export function getOutline(grid) { |
| 42 | + const rows = grid.length; |
| 43 | + const cols = grid[0].length; |
| 44 | + |
| 45 | + // ------------------------------------------------------------ |
| 46 | + // 1. Flood-fill background reachable from outside |
| 47 | + // ------------------------------------------------------------ |
| 48 | + const outside = Array.from({ length: rows }, () => Array(cols).fill(false)); |
| 49 | + const queue: number[][] = []; |
| 50 | + |
| 51 | + // Add border 0-cells |
| 52 | + for (let x = 0; x < cols; x++) { |
| 53 | + if (grid[0][x] === 0) queue.push([x, 0]); |
| 54 | + if (grid[rows - 1][x] === 0) queue.push([x, rows - 1]); |
| 55 | + } |
| 56 | + for (let y = 0; y < rows; y++) { |
| 57 | + if (grid[y][0] === 0) queue.push([0, y]); |
| 58 | + if (grid[y][cols - 1] === 0) queue.push([cols - 1, y]); |
| 59 | + } |
| 60 | + |
| 61 | + // BFS flood fill |
| 62 | + while (queue.length) { |
| 63 | + const [x, y] = queue.shift() as number[]; |
| 64 | + if (outside[y][x]) continue; |
| 65 | + outside[y][x] = true; |
| 66 | + |
| 67 | + const dirs = [[1,0],[-1,0],[0,1],[0,-1]]; |
| 68 | + for (const [dx, dy] of dirs) { |
| 69 | + const nx = x + dx, ny = y + dy; |
| 70 | + if ( |
| 71 | + nx >= 0 && nx < cols && |
| 72 | + ny >= 0 && ny < rows && |
| 73 | + grid[ny][nx] === 0 && |
| 74 | + !outside[ny][nx] |
| 75 | + ) { |
| 76 | + queue.push([nx, ny]); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // ------------------------------------------------------------ |
| 82 | + // 2. Collect boundary cells (unordered) |
| 83 | + // ------------------------------------------------------------ |
| 84 | + const boundary = new Set(); |
| 85 | + const key = (x, y) => `${x},${y}`; |
| 86 | + |
| 87 | + for (let y = 0; y < rows; y++) { |
| 88 | + for (let x = 0; x < cols; x++) { |
| 89 | + if (grid[y][x] !== 1) continue; |
| 90 | + |
| 91 | + const dirs = [[1,0],[-1,0],[0,1],[0,-1]]; |
| 92 | + for (const [dx, dy] of dirs) { |
| 93 | + const nx = x + dx, ny = y + dy; |
| 94 | + |
| 95 | + if ( |
| 96 | + nx < 0 || nx >= cols || |
| 97 | + ny < 0 || ny >= rows || |
| 98 | + (grid[ny][nx] === 0 && outside[ny][nx]) |
| 99 | + ) { |
| 100 | + boundary.add(key(x, y)); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (boundary.size === 0) return []; |
| 108 | + |
| 109 | + // ------------------------------------------------------------ |
| 110 | + // 3. SAFE OUTLINE WALK (no infinite loops) |
| 111 | + // ------------------------------------------------------------ |
| 112 | + |
| 113 | + // Convert boundary set to a fast lookup |
| 114 | + const isBoundary = (x, y) => boundary.has(key(x, y)); |
| 115 | + |
| 116 | + // Find a starting boundary cell |
| 117 | + const [startKey] = boundary; |
| 118 | + // @ts-ignore |
| 119 | + const [sx, sy] = startKey.split(',').map(Number); |
| 120 | + |
| 121 | + const outline: number[][] = []; |
| 122 | + const visited = new Set(); |
| 123 | + let current = { x: sx, y: sy }; |
| 124 | + |
| 125 | + // Moore-neighborhood directions (8 directions) |
| 126 | + const dirs8 = [ |
| 127 | + [1,0], [1,1], [0,1], [-1,1], |
| 128 | + [-1,0], [-1,-1], [0,-1], [1,-1] |
| 129 | + ]; |
| 130 | + |
| 131 | + const maxSteps = rows * cols * 8; |
| 132 | + |
| 133 | + for (let step = 0; step < maxSteps; step++) { |
| 134 | + outline.push([current.x, current.y]); |
| 135 | + visited.add(key(current.x, current.y)); |
| 136 | + |
| 137 | + // Try to find next boundary neighbor |
| 138 | + let next = null; |
| 139 | + for (const [dx, dy] of dirs8) { |
| 140 | + const nx = current.x + dx; |
| 141 | + const ny = current.y + dy; |
| 142 | + if (isBoundary(nx, ny) && !visited.has(key(nx, ny))) { |
| 143 | + // @ts-ignore |
| 144 | + next = { x: nx, y: ny }; |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // If no next step → open contour (C-shape) |
| 150 | + if (!next) break; |
| 151 | + |
| 152 | + // If next is start → closed loop |
| 153 | + // @ts-ignore |
| 154 | + if (next.x === sx && next.y === sy) { |
| 155 | + outline.push([sx, sy]); |
| 156 | + break; |
| 157 | + } |
| 158 | + |
| 159 | + current = next; |
| 160 | + } |
| 161 | + |
38 | 162 | return outline; |
39 | 163 | } |
0 commit comments