Skip to content

Commit 18e7811

Browse files
Fixed animation
1 parent 6ebd76a commit 18e7811

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

script.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ canvas.addEventListener("click", function(event) {
2727
const newNode = {
2828
id: nodes.length + 1,
2929
x: x,
30-
y: y
30+
y: y,
31+
visited: false // Add visited property
3132
};
3233

3334
nodes.push(newNode);
@@ -58,7 +59,14 @@ function drawGraph() {
5859
nodes.forEach(node => {
5960
ctx.beginPath();
6061
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
61-
ctx.fillStyle = "#87CEEB"; // Light blue color for nodes
62+
63+
// Set node color based on its visited state
64+
if (node.visited) {
65+
ctx.fillStyle = node.color || "yellow"; // Use the node's color if set
66+
} else {
67+
ctx.fillStyle = "#87CEEB"; // Default light blue color for unvisited nodes
68+
}
69+
6270
ctx.fill();
6371
ctx.strokeStyle = "#000"; // Black border for nodes
6472
ctx.lineWidth = 2;
@@ -87,17 +95,15 @@ function startBFS() {
8795
let visited = new Set();
8896
logDiv.innerHTML = "<strong>BFS Execution:</strong><br>";
8997
function step() {
90-
console.log("Queue:", queue);
9198
if (queue.length === 0) return;
9299
let node = queue.shift();
93100
if (visited.has(node)) return step();
94101
visited.add(node);
102+
node.visited = true; // Mark the node as visited
103+
node.color = "yellow"; // Set the node's color
95104
logMessage(`Visiting Node ${node.id}`);
96-
ctx.fillStyle = "yellow";
97-
ctx.beginPath();
98-
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
99-
ctx.fill();
100-
// Fix edge filtering by comparing node IDs
105+
drawGraph(); // Redraw the graph to reflect the color change
106+
101107
edges.filter(e => e.from.id === node.id).forEach(edge => {
102108
if (!visited.has(edge.to)) queue.push(edge.to);
103109
});
@@ -117,16 +123,15 @@ function startDFS() {
117123
let visited = new Set();
118124
logDiv.innerHTML = "<strong>DFS Execution:</strong><br>";
119125
function step() {
120-
console.log("Stack:", stack);
121126
if (stack.length === 0) return;
122127
let node = stack.pop();
123128
if (visited.has(node)) return step();
124129
visited.add(node);
130+
node.visited = true; // Mark the node as visited
131+
node.color = "green"; // Set the node's color
125132
logMessage(`Visiting Node ${node.id}`);
126-
ctx.fillStyle = "green";
127-
ctx.beginPath();
128-
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
129-
ctx.fill();
133+
drawGraph(); // Redraw the graph to reflect the color change
134+
130135
edges.filter(e => e.from.id === node.id).forEach(edge => {
131136
if (!visited.has(edge.to)) stack.push(edge.to);
132137
});
@@ -148,18 +153,16 @@ function startDijkstra() {
148153
distances[nodes[0].id] = 0;
149154
logDiv.innerHTML = "<strong>Dijkstra Execution:</strong><br>";
150155
function step() {
151-
console.log("Queue:", queue);
152-
console.log("Visited:", visited);
153156
if (queue.length === 0) return;
154157
queue.sort((a, b) => a.cost - b.cost);
155158
let { node, cost } = queue.shift();
156159
if (visited.has(node)) return step();
157160
visited.add(node);
161+
node.visited = true; // Mark the node as visited
162+
node.color = "orange"; // Set the node's color
158163
logMessage(`Visiting Node ${node.id} with cost ${cost}`);
159-
ctx.fillStyle = "orange";
160-
ctx.beginPath();
161-
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
162-
ctx.fill();
164+
drawGraph(); // Redraw the graph to reflect the color change
165+
163166
edges.filter(e => e.from.id === node.id).forEach(edge => {
164167
let newCost = cost + edge.weight;
165168
if (newCost < distances[edge.to.id]) {
@@ -168,9 +171,6 @@ function startDijkstra() {
168171
logMessage(`Updating distance of Node ${edge.to.id} to ${newCost}`);
169172
}
170173
});
171-
// Ensure canvas updates correctly
172-
ctx.clearRect(0, 0, canvas.width, canvas.height);
173-
drawGraph();
174174
setTimeout(step, 500);
175175
}
176176
step();

0 commit comments

Comments
 (0)