There are several ways to do this but the double loop makes it easier when calculating the x,y coordinates of the node. You also need to work out how you are going to allocate node IDs because you need this information to add the edges.
So you want a 600x600 pixel screen with a node at the centre of cells in a 24x24 grid. This means that you are going to have 578 nodes. Starting from the top left corner we will number the cells starting from 0 and go left-to-right and top-to-bottom
So in the top row the nodes are numbered 0-23, the next row 24-47 and the bottom row 552-575.
For each horizontal row we need to connect adjacent nodes so
1 <-> 0
2 <-> 1
3 <-> 2
…
23 <-> 22
The next row would be
25 <-> 24
24 <-> 23
23 <-> 22
…
47 <-> 46
Notice that they are the same as the numbers for the first row with 24 added. Continue this for each row.
The vertical edges are similar if we consider the first 2 rows we need to connect
24 <-> 0
25 <-> 1
26 <-> 2
…
47 <-> 23
I have modified the example above to demonstrate how this works.
/**
An exaample of using the PathFinder library to find a route using the
A* algorithm.
In this example all edges are bidirectional and have weights proportional
to their lengths. The A* algorithm uses the 'As the Crow Flies' distance
between nodes as its heuristic.
Created by Peter Lager 2018
*/
import pathfinder.*;
// PathFinding_01
Graph graph;
// These next 2 are only needed to display
// the nodes and edges.
GraphEdge[] edges;
GraphNode[] nodes;
GraphNode[] route;
// Pathfinder algorithm
IGraphSearch pathFinder;
// Used to indicate the start and end nodes as selected by the user.
GraphNode startNode, endNode;
void setup() {
size(600, 600);
textSize(8);
// Create graph
createGraph();
// Get nodes and edges
nodes = graph.getNodeArray();
edges = graph.getAllEdgeArray();
// Now get a path finder object
pathFinder = new GraphSearch_Astar(graph);
// Now get a route between 2 nodes
// You can change the parameter values but they must be valid IDs
pathFinder.search(2, 517);
route = pathFinder.getRoute();
}
void draw() {
background(0);
drawGraph();
drawPath();
noLoop();
}
void drawGraph() {
// Edges first
strokeWeight(2);
stroke(180, 180, 200);
for (int i = 0; i < edges.length; i++) {
GraphNode from = edges[i].from();
GraphNode to = edges[i].to();
line(from.xf(), from.yf(), to.xf(), to.yf());
}
// Nodes next
noStroke();
fill(240, 255, 240);
for (int i = 0; i < nodes.length; i++) {
GraphNode node = nodes[i];
ellipse(node.xf(), node.yf(), 6, 6);
text(node.id(), node.xf() + 4, node.yf() - 2);
}
}
void drawPath() {
strokeWeight(10);
stroke(200, 255, 200, 160);
for (int i = 1; i < route.length; i++) {
GraphNode from = route[i-1];
GraphNode to = route[i];
line(from.xf(), from.yf(), to.xf(), to.yf());
}
}
public void createGraph() {
graph = new Graph();
// Create and add node
GraphNode node;
int node_id = 0;
for (int v = 0; v < 24; v++) {
for (int h = 0; h < 24; h++) {
node = new GraphNode(node_id, 12 + h * 25, 12 + v * 24);
graph.addNode(node);
node_id++;
}
}
// Create horizontal edges
for (int v = 0; v < 24; v++) {
for (int h = 1; h < 24; h++) {
graph.addEdge(v * 24 + h-1, v * 24 + h, 0, 0);
}
}
// Create vertical edges
for (int h = 0; h < 24; h++) {
for (int v = 1; v < 24; v++) {
graph.addEdge(v * 24 - 24 + h, v * 24 + h, 0, 0);
}
}
}