///// <summary> ///// Find the shortest path using Dijkstra's algorithm ///// </summary> //public TilePath Dijkstra(TilePosition start, TileRect end) //{ // heap.Clear(); // searchedTilesOverlay.Clear(); // var startNode = heap.NodeAt(start); // startNode.Predecessor = null; // heap.DecreaseKey(startNode, 0, 0); // var currentNode = startNode; // while (!heap.IsEmpty && !end.Contains((currentNode = heap.ExtractMin()).Position)) // { // searchedTilesOverlay.Add(currentNode.Position); // foreach (var n in currentNode.Neighbors) // { // float newDistanceFromStart = currentNode.DistanceFromStart // + TilePosition.EuclideanDistance(currentNode.Position, n.Position); // if (n.DistanceFromStart > newDistanceFromStart) // { // n.Predecessor = currentNode; // heap.DecreaseKey(n, newDistanceFromStart, newDistanceFromStart); // } // } // } // heap.SetOverlayToComputedPath(this.computedPathOverlay, currentNode); // if (!end.Contains(currentNode.Position)) // return null; // return this.MakePath(currentNode); //} /// <summary> /// Make a path object from the nodes from the search. /// Starts at the end node and tracks backward, following predecessor links, until it finds the start node. /// </summary> /// <param name="endNode">The ending node of the path</param> /// <returns>The reconstructed path</returns> private TilePath MakePath(TileHeap.Node endNode) { // Reconstruct the path to from the start to the end. var path = new TilePath(endNode.Position); while (endNode != null) { path.AddBefore(endNode.Position.TileCenter); endNode = endNode.Predecessor; } return(path); }
///// <summary> ///// Find the shortest path using Dijkstra's algorithm ///// </summary> //public TilePath Dijkstra(TilePosition start, TileRect end) //{ // heap.Clear(); // searchedTilesOverlay.Clear(); // var startNode = heap.NodeAt(start); // startNode.Predecessor = null; // heap.DecreaseKey(startNode, 0, 0); // var currentNode = startNode; // while (!heap.IsEmpty && !end.Contains((currentNode = heap.ExtractMin()).Position)) // { // searchedTilesOverlay.Add(currentNode.Position); // foreach (var n in currentNode.Neighbors) // { // float newDistanceFromStart = currentNode.DistanceFromStart // + TilePosition.EuclideanDistance(currentNode.Position, n.Position); // if (n.DistanceFromStart > newDistanceFromStart) // { // n.Predecessor = currentNode; // heap.DecreaseKey(n, newDistanceFromStart, newDistanceFromStart); // } // } // } // heap.SetOverlayToComputedPath(this.computedPathOverlay, currentNode); // if (!end.Contains(currentNode.Position)) // return null; // return this.MakePath(currentNode); //} /// <summary> /// Make a path object from the nodes from the search. /// Starts at the end node and tracks backward, following predecessor links, until it finds the start node. /// </summary> /// <param name="endNode">The ending node of the path</param> /// <returns>The reconstructed path</returns> private TilePath MakePath(TileHeap.Node endNode) { // Reconstruct the path to from the start to the end. var path = new TilePath(endNode.Position); while (endNode != null) { path.AddBefore(endNode.Position.TileCenter); endNode = endNode.Predecessor; } return path; }