Пример #1
0
        public List<__Point> GetPath(int x0, int y0, int x1, int y1)
        {
            List<__Point> Path = new List<__Point>();

            List<AStarNode> OpenList = new List<AStarNode>();
            List<AStarNode> CloseList = new List<AStarNode>();

            AStarNode FirstNode = new AStarNode(x0, y0);
            FirstNode.BuildGHF(0.0f, x1, y1);
            OpenList.Add(FirstNode);

            while (OpenList.Count > 0)
            {
                int MinIndex = 0;
                float MinF = float.MaxValue;

                AStarNode MinNode = null;

                int i = 0;
                foreach (var Node in OpenList)
                {
                    if (Node.F < MinF)
                    {
                        MinNode = Node;
                        MinIndex = i;
                        MinF = Node.F;
                    }

                    i++;
                }

                int x = MinNode.x;
                int y = MinNode.y;

                if (x == x1 && y == y1)
                {
                    while (MinNode != null)
                    {
                        __Point pp = new __Point();
                        pp.x = MinNode.x;
                        pp.y = MinNode.y;
                        Path.Add(pp);

                        MinNode = MinNode.ParentNode;
                    }

                    Console.WriteLine("Find.");
                    return Path;
                }

                OpenList.RemoveAt(MinIndex);
                CloseList.Add(MinNode);

                if (x - 1 >= 0)
                    DoANode(x - 1, y, x1, y1, MinNode.G + 1.0f, MinNode, OpenList, CloseList);
                if (x + 1 < 50)
                    DoANode(x + 1, y, x1, y1, MinNode.G + 1.0f, MinNode, OpenList, CloseList);
                if (y - 1 >= 0)
                    DoANode(x, y - 1, x1, y1, MinNode.G + 1.0f, MinNode, OpenList, CloseList);
                if (y + 1 < 50)
                    DoANode(x, y + 1, x1, y1, MinNode.G + 1.0f, MinNode, OpenList, CloseList);
            }

            Console.WriteLine("Can\'t Find.");

            return Path;
        }
Пример #2
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);

            int ix = e.X / CellSize;
            int iy = e.Y / CellSize;

            if (e.Button == MouseButtons.Right)
            {
                if (__Index_Static == 0)
                {
                    Start.x = ix;
                    Start.y = iy;

                    Graphics g = CreateGraphics();
                    PaintCell(g, ix, iy, Color.FromArgb(0, 230, 0));
                }
                else if (__Index_Static == 1)
                {
                    AStar Star = new AStar(Data);
                    Path = Star.GetPath(Start.x, Start.y, ix, iy);

                    PaintPath();
                }

                __Index_Static += 1;
            }

            if (e.Button == MouseButtons.Left)
            {
                Data[ix, iy] = 1;

                __Point pp = new __Point();
                pp.x = ix;
                pp.y = iy;
                ColoredCells.Add(pp);

                PaintColoredCell();
            }
        }