Adjusted small things, norhting serious
This commit is contained in:
@@ -13,9 +13,23 @@
|
||||
<strong>A*</strong> {{ 'PATHFINDING.EXPLANATION.ASTAR_EXPLANATION' | translate}}
|
||||
<a href="{{UrlConstants.ASTAR_WIKI}}" target="_blank" rel="noopener noreferrer">Wikipedia</a>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>{{ 'PATHFINDING.EXPLANATION.NOTE' | translate}}</strong> {{ 'PATHFINDING.EXPLANATION.DISCLAIMER' | translate}}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="controls-container">
|
||||
<div class="controls">
|
||||
<button matButton="filled" (click)="visualizeDijkstra()">{{ 'PATHFINDING.DIJKSTRA' | translate }}</button>
|
||||
<button matButton="filled" (click)="visualizeAStar()">{{ 'PATHFINDING.ASTAR' | translate }}</button>
|
||||
</div>
|
||||
<div class="controls">
|
||||
<button matButton="filled" (click)="normalCase()">{{ 'PATHFINDING.NORMAL_CASE' | translate }}</button>
|
||||
<button matButton="filled" (click)="edgeCase()">{{ 'PATHFINDING.EDGE_CASE' | translate }}</button>
|
||||
<button matButton="filled" (click)="clearBoard()">{{ 'PATHFINDING.CLEAR_BOARD' | translate }}</button>
|
||||
</div>
|
||||
|
||||
<div class="controls">
|
||||
<mat-button-toggle-group [(ngModel)]="selectedNodeType" aria-label="Node Type Selection">
|
||||
<mat-button-toggle [value]="NodeType.Start">{{ 'PATHFINDING.START_NODE' | translate }}</mat-button-toggle>
|
||||
@@ -25,14 +39,6 @@
|
||||
</mat-button-toggle-group>
|
||||
</div>
|
||||
|
||||
<div class="controls">
|
||||
<button matButton="filled" (click)="visualizeDijkstra()">{{ 'PATHFINDING.DIJKSTRA' | translate }}</button>
|
||||
<button matButton="filled" (click)="visualizeAStar()">{{ 'PATHFINDING.ASTAR' | translate }}</button>
|
||||
<button matButton="filled" (click)="normalCase()">{{ 'PATHFINDING.NORMAL_CASE' | translate }}</button>
|
||||
<button matButton="filled" (click)="edgeCase()">{{ 'PATHFINDING.EDGE_CASE' | translate }}</button>
|
||||
<button matButton="filled" (click)="clearBoard()">{{ 'PATHFINDING.CLEAR_BOARD' | translate }}</button>
|
||||
</div>
|
||||
|
||||
<div class="controls">
|
||||
<div class="grid-size">
|
||||
<mat-form-field appearance="outline" class="grid-field">
|
||||
|
||||
@@ -163,6 +163,8 @@ export class PathfindingComponent implements AfterViewInit {
|
||||
|
||||
// Mouse interactions
|
||||
private onMouseDown(event: MouseEvent): void {
|
||||
this.stopAnimations();
|
||||
this.clearPath();
|
||||
const pos = this.getGridPosition(event);
|
||||
if (!pos) {
|
||||
return;
|
||||
@@ -263,7 +265,8 @@ export class PathfindingComponent implements AfterViewInit {
|
||||
isPath: false,
|
||||
distance: Infinity,
|
||||
previousNode: null,
|
||||
fScore: 0
|
||||
hScore: 0,
|
||||
fScore: Infinity,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface Node {
|
||||
distance: number;
|
||||
previousNode: Node | null;
|
||||
fScore: number;
|
||||
hScore: number;
|
||||
}
|
||||
|
||||
export const DEFAULT_GRID_ROWS = 50;
|
||||
|
||||
@@ -80,9 +80,9 @@ export class PathfindingService {
|
||||
aStar(grid: Node[][], startNode: Node, endNode: Node): { visitedNodesInOrder: Node[], nodesInShortestPathOrder: Node[] } {
|
||||
const visitedNodesInOrder: Node[] = [];
|
||||
startNode.distance = 0;
|
||||
startNode['distance'] = this.calculateHeuristic(startNode, endNode);
|
||||
startNode['hScore'] = this.calculateHeuristic(startNode, endNode);
|
||||
// fScore = gScore + hScore
|
||||
startNode['fScore'] = startNode.distance + startNode['distance'];
|
||||
startNode['fScore'] = startNode.distance + startNode['hScore'];
|
||||
|
||||
const openSet: Node[] = [startNode];
|
||||
const allNodes = this.getAllNodes(grid);
|
||||
@@ -90,7 +90,7 @@ export class PathfindingService {
|
||||
this.initNodesForAStar(allNodes, startNode);
|
||||
|
||||
while (openSet.length > 0) {
|
||||
openSet.sort((nodeA, nodeB) => nodeA['fScore'] - nodeB['fScore']);
|
||||
this.sortOpenSet(openSet);
|
||||
const currentNode = openSet.shift() as Node;
|
||||
|
||||
if (currentNode.isWall) {
|
||||
@@ -125,11 +125,22 @@ export class PathfindingService {
|
||||
return { visitedNodesInOrder, nodesInShortestPathOrder: [] };
|
||||
}
|
||||
|
||||
private sortOpenSet(openSet: Node[]) {
|
||||
openSet.sort((a, b) => {
|
||||
const f = a['fScore'] - b['fScore'];
|
||||
if (f !== 0) return f;
|
||||
|
||||
// tie-break: smaller heuristic first (more goal-directed)
|
||||
return (a['hScore'] ?? 0) - (b['hScore'] ?? 0);
|
||||
});
|
||||
}
|
||||
|
||||
private updateNeighborNode(neighbor: Node, currentNode: Node, tentativeGScore: number, endNode: Node, openSet: Node[]) {
|
||||
neighbor.previousNode = currentNode;
|
||||
neighbor.distance = tentativeGScore;
|
||||
neighbor['distance'] = this.calculateHeuristic(neighbor, endNode);
|
||||
neighbor['fScore'] = neighbor.distance + neighbor['distance'];
|
||||
neighbor['hScore'] = this.calculateHeuristic(neighbor, endNode);
|
||||
neighbor['fScore'] = neighbor.distance + neighbor['hScore'];
|
||||
|
||||
if (!openSet.includes(neighbor)) {
|
||||
openSet.push(neighbor);
|
||||
|
||||
Reference in New Issue
Block a user