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