//Create a graph of N nodes with each node having k nearest neighbors public Graph(int n, int k, Collider[] Obstacles) { start = new Node(GameObject.Find("Start").transform.position, k); goal = new Node(GameObject.Find("Goal").transform.position, k); contents = new Node[n]; for(int i = 0; i < n; i++){ contents[i] = new Node(minimum, maximum, k); } // in our contents of the graph, we find the k-nearest neighbors for each node // we first go through each node for(int i = 0; i < n; i++){ // for each node we go through each other node and add if it is one of our nearest neighbors for (int j = 0; j < n; j ++){ if (i != j) { contents[i].addNeighbor(contents[j], Obstacles); } } contents[i].addNeighbor(goal, Obstacles); } // Find neighbors for starting node for (int i = 0; i < n; i++) { start.addNeighbor(contents[i], Obstacles); } start.addNeighbor(goal, Obstacles); for (int i = 0; i < n; i++){ //Instantiate the node so we have a visual representation Instantiate(NodePrefab, contents[i].position, Quaternion.identity); Node[] neighbors = contents[i].neighbors.Values.ToArray(); for (int j = 0; j < k; j++){ //Create lines from the node to each nearest neighbor //Total of i*j*2 elements Lines.GetComponent<LineRenderer>().SetPosition((2*(i*neighbors.Length+j)), contents[i].position); Lines.GetComponent<LineRenderer>().SetPosition((2*(i*neighbors.Length+j)+1), neighbors[j].position); //Debug.DrawLine(contents[i].position, neighbors[j].position); } } Node[] startNeighbors = start.neighbors.Values.ToArray(); for (int i = 0; i < startNeighbors.Length; i++) { Lines.GetComponent<LineRenderer>().SetPosition((2*n*k)+2*i, startNeighbors[i].position); Lines.GetComponent<LineRenderer>().SetPosition((2*n*k)+2*i+1, start.position); } Solve soln = new Solve(start, goal); solnDijkstra = soln.Dijkstra(); solnAStar = soln.AStar(); }
//Create a graph of N nodes with each node having k nearest neighbors public Graph(int n, int k, Collider[] Obstacles) { start = new Node(GameObject.Find("Start").transform.position, k); goal = new Node(GameObject.Find("Goal").transform.position, k); contents = new Node[n]; for(int i = 0; i < n; i++){ contents[i] = new Node(minimum, maximum, k); } // in our contents of the graph, we find the k-nearest neighbors for each node // we first go through each node for(int i = 0; i < n; i++){ // for each node we go through each other node and add if it is one of our nearest neighbors for (int j = 0; j < n; j ++){ if (i != j) { contents[i].addNeighbor(contents[j], Obstacles); } } contents[i].addNeighbor(goal, Obstacles); } // Find neighbors for starting node for (int i = 0; i < n; i++) { start.addNeighbor(contents[i], Obstacles); } start.addNeighbor(goal, Obstacles); foreach (Node neighbor in start.neighbors.Values) { Debug.DrawLine(start.position, neighbor.position); } Solve soln = new Solve(start, goal); solnDijkstra = soln.Dijkstra(); solnAStar = soln.AStar(); }