示例#1
0
        /// <summary>
        /// 逆序输出路径列表
        /// </summary>
        /// <returns>List"AStarPoint"</returns>
        public List <MyAStarPoint> FindRoad()
        {
            List <MyAStarPoint> roadList = null;
            MyAStarPoint        nowPoint = null;

            startList.Clear();
            startList.Add(start);
            do
            {
                int minF = -1;
                for (int i = startList.Count - 1; i >= 0; i--)
                {
                    MyAStarPoint p = startList[i];
                    if (minF == -1 || p.F < minF)
                    {
                        minF     = p.F;
                        nowPoint = p;
                    }
                }
                AddStartList(nowPoint);
            } while (!(startList.Count == 0 || (nowPoint.ParentPoint != null && nowPoint.H == 0)));

            roadList = new List <MyAStarPoint>();
            if (nowPoint != null && nowPoint.H == 0)
            {
                roadList.Add(nowPoint);
                while (nowPoint.ParentPoint != null)
                {
                    nowPoint = nowPoint.ParentPoint;
                    roadList.Add(nowPoint);
                }
            }
            return(roadList);
        }
示例#2
0
 public MyAStarPoint(int x, int y, int g, int h, MyAStarPoint parentPoint)
 {
     this.x           = x;
     this.y           = y;
     this.g           = g;
     this.h           = h;
     this.f           = g + h;
     this.parentPoint = parentPoint;
 }
示例#3
0
 public MyAStar(int width, int height, MyAStarPoint start, MyAStarPoint end)
 {
     startList = new List <MyAStarPoint>();
     startList.Add(start);
     this.wallList = new List <MyAStarPoint>();
     this.start    = start;
     this.end      = end;
     this.width    = width;
     this.height   = height;
 }
示例#4
0
        private void AddStartList(MyAStarPoint point)
        {
            int x, y;

            x = point.X - 1;
            y = point.Y - 1;
            //上左
            if (x >= 0 && y >= 0)
            {
                if (IsClose(x, y) == null)                                      //检查是否在关闭列表
                {
                    if (IsClose(x, y + 1) == null && IsClose(x + 1, y) == null) //检查是否有障碍物
                    {
                        MyAStarPoint s = IsStart(x, y);
                        if (s == null)//检查是否已存在于开始列表
                        {
                            int g = point.G + 14;
                            int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                            startList.Add(new MyAStarPoint(x, y, g, h, point));
                        }
                        else
                        {
                            if (point.G + 14 < s.G)//更新最短路径
                            {
                                s.ParentPoint = point;
                                s.G           = point.G + 14;
                            }
                        }
                    }
                }
            }
            x = point.X;
            y = point.Y - 1;
            //上中
            if (y >= 0)
            {
                if (IsClose(x, y) == null)//检查是否在关闭列表
                {
                    MyAStarPoint s = IsStart(x, y);
                    if (s == null)//检查是否已存在于开始列表
                    {
                        int g = point.G + 10;
                        int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                        startList.Add(new MyAStarPoint(x, y, g, h, point));
                    }
                    else
                    {
                        if (point.G + 10 < s.G)//更新最短路径
                        {
                            s.ParentPoint = point;
                            s.G           = point.G + 10;
                        }
                    }
                }
            }
            x = point.X + 1;
            y = point.Y - 1;
            //上右
            if (x < width && y >= 0)
            {
                if (IsClose(x, y) == null)                                      //检查是否在关闭列表
                {
                    if (IsClose(x, y + 1) == null && IsClose(x - 1, y) == null) //检查是否有障碍物
                    {
                        MyAStarPoint s = IsStart(x, y);
                        if (s == null)//检查是否已存在于开始列表
                        {
                            int g = point.G + 14;
                            int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                            startList.Add(new MyAStarPoint(x, y, g, h, point));
                        }
                        else
                        {
                            if (point.G + 14 < s.G)//更新最短路径
                            {
                                s.ParentPoint = point;
                                s.G           = point.G + 14;
                            }
                        }
                    }
                }
            }
            x = point.X - 1;
            y = point.Y;
            //中左
            if (x >= 0)
            {
                if (IsClose(x, y) == null)//检查是否在关闭列表
                {
                    MyAStarPoint s = IsStart(x, y);
                    if (s == null)//检查是否已存在于开始列表
                    {
                        int g = point.G + 10;
                        int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                        startList.Add(new MyAStarPoint(x, y, g, h, point));
                    }
                    else
                    {
                        if (point.G + 10 < s.G)//更新最短路径
                        {
                            s.ParentPoint = point;
                            s.G           = point.G + 10;
                        }
                    }
                }
            }
            x = point.X + 1;
            y = point.Y;
            //中右
            if (x < width)
            {
                if (IsClose(x, y) == null)//检查是否在关闭列表
                {
                    MyAStarPoint s = IsStart(x, y);
                    if (s == null)//检查是否已存在于开始列表
                    {
                        int g = point.G + 10;
                        int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                        startList.Add(new MyAStarPoint(x, y, g, h, point));
                    }
                    else
                    {
                        if (point.G + 10 < s.G)//更新最短路径
                        {
                            s.ParentPoint = point;
                            s.G           = point.G + 10;
                        }
                    }
                }
            }
            x = point.X - 1;
            y = point.Y + 1;
            //下左
            if (x >= 0 && y < height)
            {
                if (IsClose(x, y) == null)                                      //检查是否在关闭列表
                {
                    if (IsClose(x, y - 1) == null && IsClose(x + 1, y) == null) //检查是否有障碍物
                    {
                        MyAStarPoint s = IsStart(x, y);
                        if (s == null)//检查是否已存在于开始列表
                        {
                            int g = point.G + 14;
                            int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                            startList.Add(new MyAStarPoint(x, y, g, h, point));
                        }
                        else
                        {
                            if (point.G + 14 < s.G)//更新最短路径
                            {
                                s.ParentPoint = point;
                                s.G           = point.G + 14;
                            }
                        }
                    }
                }
            }
            x = point.X;
            y = point.Y + 1;
            //下中
            if (y < height)
            {
                if (IsClose(x, y) == null)//检查是否在关闭列表
                {
                    MyAStarPoint s = IsStart(x, y);
                    if (s == null)//检查是否已存在于开始列表
                    {
                        int g = point.G + 10;
                        int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                        startList.Add(new MyAStarPoint(x, y, g, h, point));
                    }
                    else
                    {
                        if (point.G + 10 < s.G)//更新最短路径
                        {
                            s.ParentPoint = point;

                            s.G = point.G + 10;
                        }
                    }
                }
            }
            x = point.X + 1;
            y = point.Y + 1;
            //下右
            if (x < width && y < height)
            {
                if (IsClose(x, y) == null)                                      //检查是否在关闭列表
                {
                    if (IsClose(x, y - 1) == null && IsClose(x - 1, y) == null) //检查是否有障碍物
                    {
                        MyAStarPoint s = IsStart(x, y);
                        if (s == null)//检查是否已存在于开始列表
                        {
                            int g = point.G + 14;
                            int h = (Math.Abs(x - end.X) + Math.Abs(y - end.Y)) * 10;
                            startList.Add(new MyAStarPoint(x, y, g, h, point));
                        }
                        else
                        {
                            if (point.G + 14 < s.G)//更新最短路径
                            {
                                s.ParentPoint = point;

                                s.G = point.G + 14;
                            }
                        }
                    }
                }
            }
            if (startList.Remove(point))
            {
                wallList.Add(point);
            }
        }