Hi!
I’m trying to figure out the best solution to calculate neighbouring nodes in a 2 dimensional grid.
Let’s say that i have a grid of 5x5, where i iterate through the rows and colums and push a node into a list of nodes.
class Node extends Sprite
{
public var id: Int;
public var neighbours: List<Node>;
public function new(id: Int)
{
super();
this.id = id;
this.neighbours = new List();
}
}
var nodes: List = new List<Node>();
var id = 0;
for (row in 0...map.length) {
for (cell in 0...map[row].length) {
id++;
var node = new Node(id);
nodes.push(node);
}
}
How would you approach finding the vertical and horizontal node and add it to the node’s neighbour?
My initial thought was something like this, but i still have to iterate through the grid i Imagine.
for (node in nodes) {
for (n in nodes) {
// if n is vertically | | horizontally adjacent in the grid relative to node, add n to node.neighbours
node.neighbours.add(n)
}
}
Thank you!