Пример #1
0
        public AStarSearch(TiledGraphGenerator tiledGraphSystem)
        {
            Source = Destination = 0;
            this.tiledGraphSystem = tiledGraphSystem;

            GraphNodesNum = tiledGraphSystem.TotalNumNodes;

            EdgeOfSquare = (int)Math.Sqrt(GraphNodesNum);

            GCost = new float[GraphNodesNum];
            FCost = new float[GraphNodesNum];

            int combine2_n = (GraphNodesNum * (GraphNodesNum - 1)) / 2;

            ShortestPathTree = new Edge[combine2_n];  //new List<Edge>(32);
            SearchFrontier   = new Edge[combine2_n];  //new List<Edge>(32);

            Nodes = tiledGraphSystem.getNodes();
        }
        private void btnGenGraph_Click(object sender, EventArgs e)
        {
            Image image = Image.FromFile(imageFilePath);

            TiledGraph = new TiledGraphGenerator(new PointF(20, 20), new PointF(800 - 20, 610), edgeOfSquare, image);

            TiledGraphNode[] nodes = TiledGraph.getNodes();

            Graphics g = this.CreateGraphics();

            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i].isValid)
                {
                    DrawCrossHair(g, nodes[i].Position, 3, Pens.Black);
                    nodes[i].setNeighbours(nodes, TiledGraph.GraphHeightNodeCount, TiledGraph.GraphWidthNodeCount, edgeOfSquare);
                    //  g.DrawString(nodes[i].Index.ToString(), System.Drawing.SystemFonts.DefaultFont, Brushes.Red, nodes[i].Position);
                }
            }

            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i].isValid)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        if (nodes[i].AdjacentEdges[j] != null && nodes[nodes[i].AdjacentEdges[j].Destination].isValid)
                        {
                            g.DrawLine(Pens.DarkBlue, nodes[i].Position, nodes[nodes[i].AdjacentEdges[j].Destination].Position);
                        }
                    }
                }
            }

            g.Dispose();
        }