Exemple #1
0
        public PathFinder(byte[,] grid, PathFinderOptions pathFinderOptions = null)
        {
            if (grid == null)
            {
                throw new Exception("Grid cannot be null");
            }

            _grid = grid;

            if (_mCalcGrid == null || _mCalcGrid.GetLength(0) != _grid.GetLength(0) || _mCalcGrid.GetLength(1) != _grid.GetLength(1))
            {
                _mCalcGrid = new PathFinderNodeFast[_gridX, _gridY];
            }

            _open = new PriorityQueueB <Point>(new ComparePfNodeMatrix(_mCalcGrid));

            _options = pathFinderOptions ?? new PathFinderOptions();

            _direction = _options.Diagonals
                    ? new sbyte[, ] {
                { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 1, -1 }, { 1, 1 }, { -1, 1 }, { -1, -1 }
            }
                    : new sbyte[, ] {
                { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
            };
        }
Exemple #2
0
        public PathFinder(byte[,] grid, PathFinderOptions pathFinderOptions = null)
        {
            if (grid == null)
            {
                throw new Exception("Grid cannot be null");
            }

            _grid  = grid;
            _gridX = (ushort)(_grid.GetUpperBound(0) + 1);
            _gridY = (ushort)(_grid.GetUpperBound(1) + 1);

            // ReSharper disable CompareOfFloatsByEqualityOperator
            if (Math.Log(_gridX, 2) != (int)Math.Log(_gridX, 2) || Math.Log(_gridY, 2) != (int)Math.Log(_gridY, 2))
            {
                throw new Exception("Invalid Grid, size in X and Y must be power of 2");
            }
            // ReSharper restore CompareOfFloatsByEqualityOperator

            _gridXMinus1 = (ushort)(_gridX - 1);
            _gridYLog2   = (ushort)Math.Log(_gridY, 2);

            if (_mCalcGrid == null || _mCalcGrid.Length != (_gridX * _gridY))
            {
                _mCalcGrid = new PathFinderNodeFast[_gridX * _gridY];
            }

            _open = new PriorityQueueB <int>(new ComparePfNodeMatrix(_mCalcGrid));



            //set options
            if (pathFinderOptions == null)
            {
                pathFinderOptions = new PathFinderOptions();
            }

            _formula               = pathFinderOptions.Formula;
            _heavyDiagonals        = pathFinderOptions.HeavyDiagonals;
            _heuristicEstimate     = pathFinderOptions.HeuristicEstimate;
            _punishChangeDirection = pathFinderOptions.PunishChangeDirection;
            _tieBreaker            = pathFinderOptions.TieBreaker;
            _searchLimit           = pathFinderOptions.SearchLimit;
            _diagonals             = pathFinderOptions.Diagonals;
            _direction             = pathFinderOptions.Diagonals
                    ? new sbyte[, ] {
                { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { 1, -1 }, { 1, 1 }, { -1, 1 }, { -1, -1 }
            }
                    : new sbyte[, ] {
                { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }
            };
        }