コード例 #1
0
ファイル: HpaTestForm.cs プロジェクト: DmitryMI/Diploma
        private void DrawMap()
        {
            //pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            int scale = (int)Math.Ceiling((double)pictureBox1.Width / _cellMap.Width);

            LayeredCellMap layeredCellMap = new LayeredCellMap(_cellMap);

            foreach (var obstacle in _obstacles)
            {
                layeredCellMap.AddFragment(obstacle);
            }

            for (int x = 0; x < layeredCellMap.Width; x++)
            {
                for (int y = 0; y < layeredCellMap.Height; y++)
                {
                    if (!layeredCellMap.IsPassable(x, y))
                    {
                        int xScaled = x * scale;
                        int yScaled = y * scale;
                        int xDraw   = xScaled;
                        int yDraw   = yScaled;
                        _graphics.FillRectangle(_mapBrush, xDraw, yDraw, scale, scale);
                    }
                }
            }

            _scale = scale;
        }
コード例 #2
0
ファイル: BestFirstSearch.cs プロジェクト: DmitryMI/Diploma
        public IList <Vector2Int> GetPath(ICellMap mapBase, Vector2Int start, Vector2Int stop, NeighbourMode neighbourMode)
        {
            if (_layeredCellMap == null)
            {
                _layeredCellMap = new LayeredCellMap(mapBase);
            }

            bool StartNodeFilter(IWeightedGraphNode <double> node)
            {
                return(node != null && node.Position == start);
            }

            bool StopNodeFilter(IWeightedGraphNode <double> node)
            {
                return(node != null && node.Position == stop);
            }

            IWeightedGraph <double> graph = GraphGenerator.GetWeightedGraph(_layeredCellMap, neighbourMode);
            var nodes = graph.GetWeightedGraphNodes();

            IWeightedGraphNode <double>[] keyNodes = Utils.GetItemsByFilters(nodes, StartNodeFilter, StopNodeFilter);
            var startNode = keyNodes[0];
            var stopNode  = keyNodes[1];

            foreach (var node in nodes)
            {
                if (node == null)
                {
                    continue;
                }
                node.Data = new GraphData()
                {
                    DistanceToStop = GetDistance(node.Position, stop)
                };
            }

            var nodePath = GetPath(graph, startNode, stopNode);

            if (nodePath == null)
            {
                return(null);
            }
            List <Vector2Int> path = new List <Vector2Int>(nodePath.Count);

            foreach (var node in nodePath)
            {
                path.Add(node.Position);
            }

            return(path);
        }
コード例 #3
0
ファイル: BestFirstSearch.cs プロジェクト: DmitryMI/Diploma
        public IList <Vector2Int> GetSmoothedPath(ICellMap map, Vector2Int start, Vector2Int stop, NeighbourMode neighbourMode)
        {
            if (_layeredCellMap == null)
            {
                _layeredCellMap = new LayeredCellMap(map);
            }

            map = _layeredCellMap;

            var rawPath = GetPath(map, start, stop, neighbourMode);

            if (rawPath == null)
            {
                return(null);
            }
            PathSmoother smoother = new PathSmoother();
            var          path     = smoother.GetSmoothedPath(map, rawPath);

            return(path);
        }
コード例 #4
0
 private void CreateLayeredMap(ICellMap mapBase)
 {
     _layeredCellMap = new LayeredCellMap(mapBase);
 }
コード例 #5
0
ファイル: BestFirstSearch.cs プロジェクト: DmitryMI/Diploma
 public void Initialize(ICellMap mapBase)
 {
     _layeredCellMap = new LayeredCellMap(mapBase);
 }
コード例 #6
0
ファイル: LeeAlgorithm.cs プロジェクト: DmitryMI/Diploma
        public IList <Vector2Int> GetPath(ICellMap mapBase, Vector2Int start, Vector2Int stop, NeighbourMode neighbourMode)
        {
            if (_layeredCellMap == null)
            {
                _layeredCellMap = new LayeredCellMap(mapBase);
            }

            int[,] matrix = new int[_layeredCellMap.Width, _layeredCellMap.Height];

            for (int i = 0; i < _layeredCellMap.Width; i++)
            {
                for (int j = 0; j < _layeredCellMap.Height; j++)
                {
                    matrix[i, j] = -1;
                }
            }

            matrix[start.X, start.Y] = 0;

            OnCellViewedEvent?.Invoke(this, start.X, start.Y, 0);

            int  d         = 0;
            bool goFurther = true;

            while (goFurther)
            {
                goFurther = false;

                for (int i = 0; i < _layeredCellMap.Width; i++)
                {
                    if (matrix[stop.X, stop.Y] != -1)
                    {
                        break;
                    }

                    for (int j = 0; j < _layeredCellMap.Height; j++)
                    {
                        if (matrix[stop.X, stop.Y] != -1)
                        {
                            break;
                        }
                        if (matrix[i, j] == d)
                        {
                            UpdateCellSurroundings(_layeredCellMap, matrix, i, j, d + 1, neighbourMode);
                            goFurther = true;
                        }
                    }
                }

                d++;
            }

            if (matrix[stop.X, stop.Y] == -1)
            {
                return(null);
            }

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

            Vector2Int current = stop;

            path.Add(current);
            while (current != start)
            {
                current = FindDecrement(matrix, current.X, current.Y, neighbourMode);
                path.Add(current);
            }

            path.Reverse();

            return(path);
        }