Beispiel #1
0
        private List <System> GetSystemPath(System start, System end)
        {
            SystemPathInfo systemPathInfo = GetSystemPaths(start);

            List <System> path = new List <System>();

            System last = end;

            while (last != null)
            {
                path.Insert(0, last);
                last = systemPathInfo.prev[last];
            }

            return(path);
        }
Beispiel #2
0
        private SystemPathInfo GetSystemPaths(System start)
        {
            HashSet <System> queue = new HashSet <System>();
            SystemPathInfo   info  = new SystemPathInfo();

            foreach (System system in System.Systems)
            {
                queue.Add(system);
                info.dist[system] = float.MaxValue;
                info.prev[system] = null;
            }
            info.dist[start] = 0;

            while (queue.Count > 0)
            {
                System closest = null;
                foreach (System system in queue)
                {
                    if (closest == null || info.dist[system] < info.dist[closest])
                    {
                        closest = system;
                    }
                }

                queue.Remove(closest);

                foreach (System neighbor in closest.Neighbors)
                {
                    if (queue.Contains(neighbor))
                    {
                        float weight = Vector2.Distance(closest.Position, neighbor.Position);
                        float alt    = info.dist[closest] + weight;
                        if (alt < info.dist[neighbor])
                        {
                            info.dist[neighbor] = alt;
                            info.prev[neighbor] = closest;
                        }
                    }
                }
            }

            return(info);
        }