Update pathfinding.component.ts
This commit is contained in:
@@ -4,7 +4,6 @@ import { MatButtonModule } from '@angular/material/button';
|
|||||||
import {GRID_COLS, GRID_ROWS, NODE_SIZE, Node} from './pathfinding.models';
|
import {GRID_COLS, GRID_ROWS, NODE_SIZE, Node} from './pathfinding.models';
|
||||||
import {MatButtonToggleModule} from '@angular/material/button-toggle';
|
import {MatButtonToggleModule} from '@angular/material/button-toggle';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {NgIf} from '@angular/common';
|
|
||||||
import { PathfindingService } from './service/pathfinding.service';
|
import { PathfindingService } from './service/pathfinding.service';
|
||||||
import { TranslateModule, TranslateService } from '@ngx-translate/core';
|
import { TranslateModule, TranslateService } from '@ngx-translate/core';
|
||||||
|
|
||||||
@@ -19,7 +18,7 @@ enum NodeType {
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'app-pathfinding',
|
selector: 'app-pathfinding',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [CommonModule, MatButtonModule, MatButtonToggleModule, FormsModule, NgIf, TranslateModule],
|
imports: [CommonModule, MatButtonModule, MatButtonToggleModule, FormsModule, TranslateModule],
|
||||||
templateUrl: './pathfinding.component.html',
|
templateUrl: './pathfinding.component.html',
|
||||||
styleUrls: ['./pathfinding.component.scss']
|
styleUrls: ['./pathfinding.component.scss']
|
||||||
})
|
})
|
||||||
@@ -35,7 +34,7 @@ export class PathfindingComponent implements AfterViewInit {
|
|||||||
|
|
||||||
isDrawing: boolean = false;
|
isDrawing: boolean = false;
|
||||||
selectedNodeType: NodeType = NodeType.None; // Default to no selection
|
selectedNodeType: NodeType = NodeType.None; // Default to no selection
|
||||||
animationSpeed: number = 10; // milliseconds
|
animationSpeed: number = 1; // milliseconds
|
||||||
|
|
||||||
readonly NodeType = NodeType; // Expose enum to template
|
readonly NodeType = NodeType; // Expose enum to template
|
||||||
|
|
||||||
@@ -192,10 +191,9 @@ export class PathfindingComponent implements AfterViewInit {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.clearPath();
|
this.clearPath();
|
||||||
const gridCopy = this.getCleanGrid();
|
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.dijkstra(this.grid,
|
||||||
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.dijkstra(gridCopy,
|
this.grid[this.startNode.row][this.startNode.col],
|
||||||
gridCopy[this.startNode.row][this.startNode.col],
|
this.grid[this.endNode.row][this.endNode.col]
|
||||||
gridCopy[this.endNode.row][this.endNode.col]
|
|
||||||
);
|
);
|
||||||
this.animateAlgorithm(visitedNodesInOrder, nodesInShortestPathOrder);
|
this.animateAlgorithm(visitedNodesInOrder, nodesInShortestPathOrder);
|
||||||
}
|
}
|
||||||
@@ -206,10 +204,9 @@ export class PathfindingComponent implements AfterViewInit {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.clearPath();
|
this.clearPath();
|
||||||
const gridCopy = this.getCleanGrid();
|
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.aStar(this.grid,
|
||||||
const { visitedNodesInOrder, nodesInShortestPathOrder } = this.pathfindingService.aStar(gridCopy,
|
this.grid[this.startNode.row][this.startNode.col],
|
||||||
gridCopy[this.startNode.row][this.startNode.col],
|
this.grid[this.endNode.row][this.endNode.col]
|
||||||
gridCopy[this.endNode.row][this.endNode.col]
|
|
||||||
);
|
);
|
||||||
this.animateAlgorithm(visitedNodesInOrder, nodesInShortestPathOrder);
|
this.animateAlgorithm(visitedNodesInOrder, nodesInShortestPathOrder);
|
||||||
}
|
}
|
||||||
@@ -262,23 +259,4 @@ export class PathfindingComponent implements AfterViewInit {
|
|||||||
this.drawGrid();
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user