Пример #1
0
        public static IDijkstraMap Dijkstra(IEnumerable <Point> start, IEnumerable <Point> end, int width, int height, Rectangle activeArea, double maxDist, ICostMap costMap, IEnumerable <Point> neighbors)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            bool            hasEnds = end.Any();
            HashSet <Point> ends    = new HashSet <Point>(end);
            FibonacciHeap <DijkstraTile, double> heap = new FibonacciHeap <DijkstraTile, double>(0);

            if (activeArea.X < 0)
            {
                activeArea.X = 0;
            }
            if (activeArea.Y < 0)
            {
                activeArea.Y = 0;
            }
            if (activeArea.Width > width - 1)
            {
                activeArea.Width = width - 1;
            }
            if (activeArea.Height > height - 1)
            {
                activeArea.Height = height - 1;
            }

            IDijkstraMap dijkstraMap = new DijkstraMap(width, height, heap, start);

            int i = 0;

            while (!heap.IsEmpty() && (!hasEnds || ends.Count > 0))
            {
                var node  = heap.RemoveMin();
                var dTile = node.Data;

                if (dTile.Distance >= maxDist)
                {
                    break;
                }

                if (ends.Contains(dTile.Tile))
                {
                    ends.Remove(dTile.Tile);
                }

                i++;

                foreach (var neighbor in neighbors.Select(o => dTile.Tile + o))
                {
                    if (!activeArea.Contains(neighbor.X, neighbor.Y) /*neighbor.X < 0 || neighbor.Y < 0 || neighbor.X >= width || neighbor.Y >= height*/)
                    {
                        continue;
                    }
                    var    nodeNeighbor = dijkstraMap.GetNode(neighbor.X, neighbor.Y);
                    var    dNeighbor    = nodeNeighbor.Data;
                    double newDist      = dTile.Distance + costMap.GetCost(dNeighbor.Tile);

                    if (newDist < dNeighbor.Distance)
                    {
                        dNeighbor.Distance     = newDist;
                        dNeighbor.Previous     = dTile;
                        dNeighbor.MoveDistance = dTile.MoveDistance + 1;
                        heap.DecreaseKey(nodeNeighbor, dNeighbor.Distance);
                    }
                }
            }

            Console.WriteLine($"Dijkstra ({i} iterations) took: {stopwatch.ElapsedTicks} ({(float)stopwatch.ElapsedTicks / i})");

            return(dijkstraMap);
        }
Пример #2
0
 public static IDijkstraMap Dijkstra(Point start, Point end, int width, int height, Rectangle activeArea, double maxDist, ICostMap costMap, IEnumerable <Point> neighbors)
 {
     return(Dijkstra(new[] { start }, new[] { end }, width, height, activeArea, maxDist, costMap, neighbors));
 }