// // use to add he eight neighboring edges of a graph node that // is positioned in a grid layout public static void AddAllNeighboursToGridNode(NavGraph graph, int row, int col, int numCellsX, int numCellsY) { for (int i = -1; i <= +1; ++i) { for (int j = -1; j <= +1; ++j) { //skip if equal to this node if (i == 0 && j == 0) { continue; } int nodeX = col + j; int nodeY = row + i; //check to see if this is a valid neighbour if (ValidNeighbour(nodeX, nodeY, numCellsX, numCellsY)) { //calculate the distance to this node Vector2 posNode = graph.GetNode(row * numCellsX + col).Position; Vector2 posNeighbour = graph.GetNode(nodeY * numCellsX + nodeX).Position; float dist = Vector2.Length(posNeighbour - posNode); GraphEdge newEdge; newEdge = new GraphEdge(row * numCellsX + col, nodeY * numCellsX + nodeX, dist); graph.AddEdge(newEdge); //if graph is not a diagraph then an edge needs to be added going //in the other direction if (!graph.IsDigraph()) { newEdge = new GraphEdge(nodeY * numCellsX + nodeX, row * numCellsX + col, dist); graph.AddEdge(newEdge); } } } } }