|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { floodFill } from "@/algorithms/Recursion/FloodFill"; |
| 3 | +export default function FloodFillVisualizer() { |
| 4 | + const rows = 20; |
| 5 | + const cols = 20; |
| 6 | + |
| 7 | + const generateGrid = () => { |
| 8 | + const grid = []; |
| 9 | + for (let i = 0; i < rows; i++) { |
| 10 | + const row = []; |
| 11 | + for (let j = 0; j < cols; j++) { |
| 12 | + row.push(Math.random() > 0.3 ? 1 : 0); |
| 13 | + } |
| 14 | + grid.push(row); |
| 15 | + } |
| 16 | + return grid; |
| 17 | + }; |
| 18 | + |
| 19 | + const [grid, setGrid] = useState(generateGrid()); |
| 20 | + const [speed, setSpeed] = useState(200); |
| 21 | + const [isRunning, setIsRunning] = useState(false); |
| 22 | + |
| 23 | + const source = { x: 0, y: 0 }; |
| 24 | + |
| 25 | + const startFloodFill = () => { |
| 26 | + if (isRunning) return; |
| 27 | + |
| 28 | + const mat = grid.map((row) => [...row]); |
| 29 | + const steps = []; |
| 30 | + floodFill(grid.map((row) => [...row]), source.x, source.y, 1, 2, steps); |
| 31 | + |
| 32 | + animateFill(steps); |
| 33 | + }; |
| 34 | + |
| 35 | + const animateFill = (steps) => { |
| 36 | + let i = 0; |
| 37 | + setIsRunning(true); |
| 38 | + |
| 39 | + const interval = setInterval(() => { |
| 40 | + if (i >= steps.length) { |
| 41 | + clearInterval(interval); |
| 42 | + setIsRunning(false); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const { x, y } = steps[i]; |
| 47 | + setGrid((prev) => { |
| 48 | + const newGrid = prev.map((row) => [...row]); |
| 49 | + newGrid[x][y] = 2; |
| 50 | + return newGrid; |
| 51 | + }); |
| 52 | + |
| 53 | + i++; |
| 54 | + }, speed); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div style={{ textAlign: "center", marginTop: "30px" }}> |
| 59 | + <h2>Recursive Flood Fill Visualization</h2> |
| 60 | + <div style={{ margin: "20px 0" }}> |
| 61 | + <label style={{ fontWeight: "bold" }}>Speed: </label> |
| 62 | + <input |
| 63 | + type="range" |
| 64 | + min="50" |
| 65 | + max="800" |
| 66 | + value={speed} |
| 67 | + onChange={(e) => setSpeed(Number(e.target.value))} |
| 68 | + style={{ width: "300px" }} |
| 69 | + /> |
| 70 | + <div>{speed} ms per step</div> |
| 71 | + </div> |
| 72 | + <div |
| 73 | + style={{ |
| 74 | + display: "inline-grid", |
| 75 | + gridTemplateColumns: `repeat(${cols}, 25px)`, |
| 76 | + gap: "2px", |
| 77 | + marginTop: "20px", |
| 78 | + }} |
| 79 | + > |
| 80 | + {grid.map((row, r) => |
| 81 | + row.map((cell, c) => { |
| 82 | + let color = "#dfe6e9"; |
| 83 | + |
| 84 | + if (r === source.x && c === source.y) { |
| 85 | + color = "#ff7675"; |
| 86 | + } else if (cell === 2) { |
| 87 | + color = "#3498db"; |
| 88 | + } else if (cell === 1) { |
| 89 | + color = "#b2bec3"; |
| 90 | + } |
| 91 | + |
| 92 | + return ( |
| 93 | + <div |
| 94 | + key={`${r}-${c}`} |
| 95 | + style={{ |
| 96 | + width: 25, |
| 97 | + height: 25, |
| 98 | + borderRadius: 4, |
| 99 | + border: "1px solid #ccc", |
| 100 | + backgroundColor: color, |
| 101 | + transition: "background-color 0.25s", |
| 102 | + }} |
| 103 | + /> |
| 104 | + ); |
| 105 | + }) |
| 106 | + )} |
| 107 | + </div> |
| 108 | + <div style={{ marginTop: "30px" }}> |
| 109 | + <button |
| 110 | + disabled={isRunning} |
| 111 | + onClick={startFloodFill} |
| 112 | + style={{ |
| 113 | + padding: "10px 20px", |
| 114 | + background: isRunning ? "#b2bec3" : "#0984e3", |
| 115 | + color: "white", |
| 116 | + border: "none", |
| 117 | + cursor: isRunning ? "not-allowed" : "pointer", |
| 118 | + borderRadius: "5px", |
| 119 | + marginRight: "10px", |
| 120 | + }} |
| 121 | + > |
| 122 | + Start Flood Fill |
| 123 | + </button> |
| 124 | + |
| 125 | + <button |
| 126 | + disabled={isRunning} |
| 127 | + onClick={() => setGrid(generateGrid())} |
| 128 | + style={{ |
| 129 | + padding: "10px 20px", |
| 130 | + background: "#6c5ce7", |
| 131 | + color: "white", |
| 132 | + border: "none", |
| 133 | + cursor: "pointer", |
| 134 | + borderRadius: "5px", |
| 135 | + }} |
| 136 | + > |
| 137 | + Reset Grid |
| 138 | + </button> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
0 commit comments