11local current = (... ):gsub (' %.[^%.]+$' , ' ' );
2+
3+ -- ------------------------------------------------
4+ -- Required Modules
5+ -- ------------------------------------------------
6+
27local Node = require (current .. ' .Node' );
38local Edge = require (current .. ' .Edge' );
49
10+ -- ------------------------------------------------
11+ -- Module
12+ -- ------------------------------------------------
13+
514local Graph = {};
615
716function Graph .new ()
817 local self = {};
918
10- local nodes = {};
11- local edges = {};
12- local edgeIDs = 0 ;
19+ local nodes = {}; -- Contains all nodes in the graph.
20+ local edges = {}; -- Contains all edges in the graph.
21+ local edgeIDs = 0 ; -- Used to create a unique ID for new edges.
1322
14- local minX , maxX , minY , maxY ;
23+ local minX , maxX , minY , maxY ; -- The boundaries of the graph.
1524
1625 -- ------------------------------------------------
1726 -- Local Functions
1827 -- ------------------------------------------------
1928
2029 ---
21- -- Sets the graph's boundaries to nil.
30+ -- (Re-) Sets the graph's boundaries to nil.
2231 --
2332 local function resetBoundaries ()
2433 minX , maxX , minY , maxY = nil , nil , nil , nil ;
2534 end
2635
2736 ---
37+ -- Updates the boundaries of the graph.
38+ -- This represents the rectangular area in which all nodes are contained.
2839 -- @param minX - The current minimum x position.
2940 -- @param maxX - The current maximum y position.
3041 -- @param minY - The current minimum x position.
@@ -58,11 +69,12 @@ function Graph.new()
5869 -- ------------------------------------------------
5970
6071 ---
61- -- Add a node to the graph.
72+ -- Adds a node to the graph.
6273 -- @param id - The ID will be used to reference the Node inside of the graph.
6374 -- @param x - The x coordinate the Node should be spawned at (optional).
6475 -- @param y - The y coordinate the Node should be spawned at (optional).
6576 -- @param anchor - Wether the node should be locked in place or not (optional).
77+ -- @param ... - Additional parameters (useful when a custom Node class is used).
6678 --
6779 function self :addNode ( id , x , y , anchor , ... )
6880 assert ( not nodes [id ], " Node IDs must be unique." );
@@ -71,8 +83,9 @@ function Graph.new()
7183 end
7284
7385 ---
74- -- Remove a node from the graph.
75- -- This will also remove all edges pointing to or originating from this node.
86+ -- Removes a node from the graph.
87+ -- This will also remove all edges pointing to, or originating from this
88+ -- node.
7689 -- @param node - The node to remove from the graph.
7790 --
7891 function self :removeNode ( node )
@@ -102,7 +115,7 @@ function Graph.new()
102115 end
103116
104117 ---
105- -- Removes all edges leading to or originating from a node.
118+ -- Removes all edges leading to, or originating from a node.
106119 -- @param node - The node to remove all edges from.
107120 --
108121 function self :removeEdges ( node )
@@ -116,8 +129,8 @@ function Graph.new()
116129 ---
117130 -- Updates the graph.
118131 -- @param dt - The delta time between frames.
119- -- @param nodeCallback - A callback called on every node.
120- -- @param edgeCallback - A callback called on every edge.
132+ -- @param nodeCallback - A callback called on every node (optional) .
133+ -- @param edgeCallback - A callback called on every edge (optional) .
121134 --
122135 function self :update ( dt , nodeCallback , edgeCallback )
123136 for _ , edge in pairs ( edges ) do
@@ -220,7 +233,7 @@ function Graph.new()
220233 -- forces.
221234 -- @param id - The node's id.
222235 -- @param x - The x coordinate to anchor the node to.
223- -- @param y - The x coordinate to anchor the node to.
236+ -- @param y - The y coordinate to anchor the node to.
224237 --
225238 function self :setAnchor ( id , x , y )
226239 nodes [id ]:setPosition ( x , y );
0 commit comments