Update pathfinding.component.ts

This commit is contained in:
2026-02-01 16:18:55 +01:00
parent 17db997398
commit b0ad2dc3d1

View File

@@ -4,7 +4,6 @@ import { MatButtonModule } from '@angular/material/button';
import {GRID_COLS, GRID_ROWS, NODE_SIZE, Node} from './pathfinding.models';
import {MatButtonToggleModule} from '@angular/material/button-toggle';
import {FormsModule} from '@angular/forms';
import {NgIf} from '@angular/common';
import { PathfindingService } from './service/pathfinding.service';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
@@ -19,7 +18,7 @@ enum NodeType {
@Component({
selector: 'app-pathfinding',
standalone: true,
imports: [CommonModule, MatButtonModule, MatButtonToggleModule, FormsModule, NgIf, TranslateModule],
imports: [CommonModule, MatButtonModule, MatButtonToggleModule, FormsModule, TranslateModule],
templateUrl: './pathfinding.component.html',
styleUrls: ['./pathfinding.component.scss']
})
@@ -35,7 +34,7 @@ export class PathfindingComponent implements AfterViewInit {
isDrawing: boolean = false;
selectedNodeType: NodeType = NodeType.None; // Default to no selection
animationSpeed: number = 10; // milliseconds
animationSpeed: number = 1; // milliseconds
readonly NodeType = NodeType; // Expose enum to template
@@ -192,10 +191,9 @@ export class PathfindingComponent implements AfterViewInit {
return;
}
this.clearPath();
const gridCopy = this.getCleanGrid();
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.dijkstra(gridCopy,
gridCopy[this.startNode.row][this.startNode.col],
gridCopy[this.endNode.row][this.endNode.col]
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.dijkstra(this.grid,
this.grid[this.startNode.row][this.startNode.col],
this.grid[this.endNode.row][this.endNode.col]
);
this.animateAlgorithm(visitedNodesInOrder, nodesInShortestPathOrder);
}
@@ -206,10 +204,9 @@ export class PathfindingComponent implements AfterViewInit {
return;
}
this.clearPath();
const gridCopy = this.getCleanGrid();
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.aStar(gridCopy,
gridCopy[this.startNode.row][this.startNode.col],
gridCopy[this.endNode.row][this.endNode.col]
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.aStar(this.grid,
this.grid[this.startNode.row][this.startNode.col],
this.grid[this.endNode.row][this.endNode.col]
);
this.animateAlgorithm(visitedNodesInOrder, nodesInShortestPathOrder);
}
@@ -262,23 +259,4 @@ export class PathfindingComponent implements AfterViewInit {
this.drawGrid();
}
// Helper to get a deep copy of the grid for algorithm execution
private getCleanGrid(): Node[][] {
const newGrid: Node[][] = [];
for (let row = 0; row < GRID_ROWS; row++) {
const currentRow: Node[] = [];
for (let col = 0; col < GRID_COLS; col++) {
const node = this.grid[row][col];
currentRow.push({
...node,
isVisited: false,
isPath: false,
distance: Infinity,
previousNode: null,
});
}
newGrid.push(currentRow);
}
return newGrid;
}
}