Example #1
0
        /// <summary>
        /// 寻径调用-主方法
        /// </summary>
        /// <param name="start">起始点</param>
        /// <param name="end">  终点  </param>
        /// <returns></returns>
        public List <Astar_Node> FindeWay(Astar_Node start, Astar_Node end)
        {
            List <Astar_Node> Path = new List <Astar_Node>();

            Open_List.Add(start);

            while (!(IsInOpenList(end.x, end.y) || //当终点不在Open_List时
                     Open_List.Count == 0))     //或者Open_List的数量为0循环(终点在list就停)
            {
                Astar_Node now = GetMinFFromOpenList();
                if (now == null)
                {
                    return(null);
                }
                Open_List.Remove(now);
                Close_List.Add(now);
                CheckP8(now, R, ref end);
            }

            Astar_Node p = GetPointFromOpenList(end.x, end.y);

            while (p.father != null)
            {
                Path.Add(p);
                p = p.father;
            }
            return(Path);
        }
Example #2
0
 //判断一个点是否为障碍物
 private bool IsBar(Astar_Node p, byte[,] map)
 {
     if (map[p.y, p.x] == 0)
     {
         return(true);
     }
     return(false);
 }
Example #3
0
 public Astar_Node(int x0, int y0, int G0, int H0, Astar_Node F)
 {
     x      = x0;
     y      = y0;
     G      = G0;
     H      = H0;
     father = F;
 }
Example #4
0
 void Init()
 {
     for (int i = 0; i < 20; i++)
     {
         for (int j = 0; j < 20; j++)
         {
             R[i, j] = 1;
             myBtn[i, j].BackColor = Color.Transparent;
             strPoint = new Astar_Node();
             desPoint = new Astar_Node();
         }
     }
 }
Example #5
0
        //从开启列表查找F值最小的节点
        private Astar_Node GetMinFFromOpenList()
        {
            Astar_Node Pmin = null;

            foreach (Astar_Node p in Open_List)
            {
                if (Pmin == null || Pmin.G + Pmin.H > p.G + p.H)
                {
                    Pmin = p;
                }
            }
            return(Pmin);
        }
Example #6
0
 //计算某个点的G值
 private int GetG(Astar_Node p)
 {
     if (p.father == null)
     {
         return(0);                  //该点为起始点
     }
     if (p.x == p.father.x || p.y == p.father.y)
     {
         return(p.father.G + 10);     //垂直走的距离是10
     }
     else
     {
         return(p.father.G + 14);     //斜着走的距离是14(即根号2)
     }
 }
Example #7
0
        /// <summary>
        /// 检查当前节点附近的节点
        /// </summary>
        /// <param name="now">   当前节点</param>
        /// <param name="map">  障碍</param>
        /// <param name="pa">   起点</param>
        /// <param name="pb">   终点</param>
        private void CheckP8(Astar_Node now, byte[,] map, ref Astar_Node destination)
        {
            for (int x = now.x - 1; x <= now.x + 1; x++)
            {
                for (int y = now.y - 1; y <= now.y + 1; y++)
                {
                    if ((x >= 0 && x < R.GetLength(0) && //排除超过边界的点
                         y >= 0 && y < R.GetLength(1)) &&
                        !(x == now.x && y == now.y) &&  //关闭自身的点(自身点总是最小)
                        map[x, y] == 1 &&               //排除障碍点
                        !IsInCloseList(x, y))           //关闭列表中的点
                    {
                        if (IsInOpenList(x, y))         //在搜寻列表中
                        {
                            Astar_Node tempPoint = GetPointFromOpenList(x, y);
                            int        G_new     = 0;
                            if (now.x == tempPoint.x || now.y == tempPoint.y)
                            {
                                G_new = now.G + 10;
                            }
                            else
                            {
                                G_new = now.G + 14;
                            }

                            if (G_new < tempPoint.G)
                            {
                                Open_List.Remove(tempPoint);
                                tempPoint.father = now;
                                tempPoint.G      = G_new;
                                Open_List.Add(tempPoint);
                            }
                        }
                        else                            //不在开启列表中
                        {
                            Astar_Node tempPoint = new Astar_Node();
                            tempPoint.x      = x;
                            tempPoint.y      = y;
                            tempPoint.father = now;
                            tempPoint.G      = GetG(tempPoint);
                            tempPoint.H      = GetH(tempPoint, destination);
                            Open_List.Add(tempPoint);
                        }
                    }
                }
            }
        }
Example #8
0
 int GetH(Astar_Node p)
 {
     return(Math.Abs(p.x - desPoint.x) * 10 + Math.Abs(p.y - desPoint.y) * 10);
 }
Example #9
0
 //计算某个点的H值
 private int GetH(Astar_Node p, Astar_Node pb)
 {
     return(Math.Abs(p.x - pb.x) * 10 + Math.Abs(p.y - pb.y) * 10);
 }