コード例 #1
0
        //--------------------------- CreateSimpleGrid --------------------------
        //
        //  creates a graph based on a grid layout. This function requires the
        //  dimensions of the environment and the number of cells required horizontally
        //  and vertically
        //-----------------------------------------------------------------------------
        public static void Helper_CreateGrid(SparseGraph graph,
                                             int cySize,
                                             int cxSize,
                                             int NumCellsY,
                                             int NumCellsX)
        {
            //need some temporaries to help calculate each node center
            double CellWidth  = (double)cySize / (double)NumCellsX;
            double CellHeight = (double)cxSize / (double)NumCellsY;

            double midX = CellWidth / 2;
            double midY = CellHeight / 2;

            //first create all the nodes
            for (int row = 0; row < NumCellsY; ++row)
            {
                for (int col = 0; col < NumCellsX; ++col)
                {
                    graph.AddNode(new NavGraphNode(graph.GetNextFreeNodeIndex(),
                                                   new Vector2D(midX + (col * CellWidth),
                                                                midY + (row * CellHeight))));
                }
            }
            //now to calculate the edges. (A position in a 2d array [x][y] is the
            //same as [y*NumCellsX + x] in a 1d array). Each cell has up to eight
            //neighbours.
            for (int row = 0; row < NumCellsY; ++row)
            {
                for (int col = 0; col < NumCellsX; ++col)
                {
                    Helper_AddAllNeighboursToGridNode(graph, row, col, NumCellsX, NumCellsY);
                }
            }
        }