diff --git a/src/App.css b/src/App.css index 6697151..c42aba3 100644 --- a/src/App.css +++ b/src/App.css @@ -26,7 +26,7 @@ body { z-index: 999; } -.features--algos { +.featured--algos { display: block; } @@ -50,6 +50,46 @@ body { position: absolute; display: block; } + +.speed--settings { + display: block; +} + +.speed--settings:hover { + .speeds--list { + position: absolute; + display: block; + margin: 0; + } +} + +.speed--settings h3 { + line-height: 50px; +} + +.speeds--list { + display: none; +} + +.wall--patterns { + display: block; +} + +.wall--patterns:hover { + .patterns--list { + position: absolute; + display: block; + margin: 0; + } +} + +.wall--patterns h3 { + line-height: 50px; +} + +.patterns--list { + display: none; +} /* nav section ends here */ .canvas { diff --git a/src/App.js b/src/App.js index d8a3f28..98114bf 100644 --- a/src/App.js +++ b/src/App.js @@ -9,6 +9,8 @@ import dfsAlgorithm from "./algorithms/dfs.js"; export default function App() { let [featured_algorithm, setFeaturedAlgorithm] = React.useState("Dijkstra"); + let [animationSpeed, setAnimationSpeed] = React.useState(25); // in ms (fast by default) + const rows = (window.innerHeight / 20) / (1.5); const columns = ((window.screen.width - 150) / 20); @@ -83,7 +85,7 @@ export default function App() { } newList[path_node.row][path_node.column] = newNode; setNodes(newList); - }, (25 * i)); + }, (animationSpeed * i)); } } @@ -92,7 +94,7 @@ export default function App() { if (i === path.length) { setTimeout(() => { animatePath(createPath(endNode), nodes, setNodes); - }, 25 * i); + }, animationSpeed * i); } else { setTimeout(() => { @@ -105,7 +107,7 @@ export default function App() { newList[node.row][node.column] = newNode; setNodes(newList); //console.log(newList); - }, (25 * i)); + }, (animationSpeed * i)); } }; }; @@ -132,8 +134,8 @@ export default function App() { } function visualizeDFS(nodes, setNodes) { - const startNode = nodes[8][40]; - const endNode = nodes[8][10]; + const startNode = nodes[8][10]; + const endNode = nodes[8][40]; const path = dfsAlgorithm(nodes, startNode, endNode); animateAlgorithm(path, nodes, endNode, setNodes); } @@ -144,6 +146,21 @@ export default function App() { console.log(featured_algorithm); }; + function selectAnimationSpeed(value) { + if (value === "fast") { + setAnimationSpeed(25); + } + else if (value === "average") { + setAnimationSpeed(50); + } + else if (value === "slow") { + setAnimationSpeed(100); + } + else { + console.log("Error: unknown animation speed parameter"); + } + } + function visualizeSelectedAlgorithm() { if (featured_algorithm === "Dijkstra") { visualizeDijkstra(nodes, setNodes); @@ -182,6 +199,8 @@ export default function App() {
visualizeSelectedAlgorithm()} clearGrid={clearGrid} clearPath={clearPath} diff --git a/src/algorithms/bfs.js b/src/algorithms/bfs.js index b9a4b8c..b86a737 100644 --- a/src/algorithms/bfs.js +++ b/src/algorithms/bfs.js @@ -22,19 +22,19 @@ function getUnvisitedNeighbors(node, list) { } export default function bfsAlgorithm(nodes, startNode, endNode) { - startNode.visited = true; const queue = [startNode]; const res = []; while (queue.length > 0) { const current = queue[0]; + current.isVisited = true; - if (current == endNode) { + if (current === endNode) { return res; } const children = getUnvisitedNeighbors(current, nodes); - for (let child of children) { + for (const child of children) { child.isVisited = true; child.parent = current; queue.push(child); diff --git a/src/algorithms/dfs.js b/src/algorithms/dfs.js index c2515ff..f80f5aa 100644 --- a/src/algorithms/dfs.js +++ b/src/algorithms/dfs.js @@ -35,9 +35,9 @@ export default function dfsAlgorithm(nodes, startNode, endNode) { } const children = getUnvisitedNeighbors(current, nodes); - // for (const child of children) { - // child.isVisited = true; - // } + for (const child of children) { + child.parent = current; + } unvisitedStack = children.concat(unvisitedStack); visited.push(current); diff --git a/src/components/NavBar.js b/src/components/NavBar.js index c7022f5..79bbc60 100644 --- a/src/components/NavBar.js +++ b/src/components/NavBar.js @@ -1,4 +1,15 @@ export default function NavBar(props) { + let speed; + if (props.currentAnimationSpeed === 25) { + speed = "Fast"; + } + else if (props.currentAnimationSpeed === 50) { + speed = "Average"; + } + else if (props.currentAnimationSpeed === 100) { + speed = "Slow"; + } + return (

PAV

@@ -14,7 +25,8 @@ export default function NavBar(props) {

Wall Patterns

    -
  • +
  • +
@@ -22,11 +34,11 @@ export default function NavBar(props) {
-

Speed:

+

Speed: {speed}

    -
  • Fast
  • -
  • Average
  • -
  • Slow
  • +
  • +
  • +