Esempio n. 1
0
        private List <FindPathMethods.PathFinderNode> GetWayAStar(byte[,] frame, Point source, Point target)
        {
            var rect = new Rectangle(0, 0, frame.GetLength(0), frame.GetLength(1));

            if (!rect.Contains(source) || !rect.Contains(target))
            {
                //точка начала или окончания маршрута не входит в массив;
                return(null);
            }

            FindPathMethods.IPathFinder finder;
            double wlog = Math.Log(frame.GetLength(0), 2), hlog = Math.Log(frame.GetLength(1), 2);

            if (wlog == (int)wlog && hlog == (int)hlog)
            {
                finder = new FindPathMethods.PathFinderFast(frame);
            }
            else
            {
                finder = new FindPathMethods.PathFinder(frame);
            }

            finder.Diagonals         = true;
            finder.SearchLimit       = Constants.SEARCH_LIMIT;
            finder.HeuristicEstimate = 3;

            try
            {
                var path = finder.FindPath(source, target);
                if (path != null)
                {
                    path.Reverse();
                }
                return(path);
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine(string.Format("Ошибка алгоритма A* ({0},{1}) - ({2},{3})", source.X, source.Y, target.X, target.Y));
                return(null);
            }
        }
Esempio n. 2
0
        private List<FindPathMethods.PathFinderNode> GetWayAStar(byte[,] frame, Point source, Point target)
        {
            var rect = new Rectangle(0, 0, frame.GetLength(0), frame.GetLength(1));
            if (!rect.Contains(source) || !rect.Contains(target))
            {
                //точка начала или окончания маршрута не входит в массив;
                return null;
            }

            FindPathMethods.IPathFinder finder;
            double wlog = Math.Log(frame.GetLength(0), 2), hlog = Math.Log(frame.GetLength(1), 2);
            if (wlog == (int)wlog && hlog == (int)hlog)
            {
                finder = new FindPathMethods.PathFinderFast(frame);
            }
            else
            {
                finder = new FindPathMethods.PathFinder(frame);
            }

            finder.Diagonals = true;
            finder.SearchLimit = Constants.SEARCH_LIMIT;
            finder.HeuristicEstimate = 3;

            try
            {
                var path = finder.FindPath(source, target);
                if (path != null)
                {
                    path.Reverse();
                }
                return path;
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine(string.Format("Ошибка алгоритма A* ({0},{1}) - ({2},{3})", source.X, source.Y, target.X, target.Y));
                return null;
            }
        }