diff --git a/fd/Graph.lua b/fd/Graph.lua index cd59105..ab8f89a 100644 --- a/fd/Graph.lua +++ b/fd/Graph.lua @@ -68,9 +68,10 @@ function Graph.new() --- -- Add a node to the graph. - -- @param node - The node to add to the graph. - -- @param x - The x-coordinate at which to place the new node. - -- @param y - The y-coordinate at which to place the new node. + -- @param id - The ID will be used to reference the Node inside of the graph. + -- @param x - The x coordinate the Node should be spawned at (optional). + -- @param y - The y coordinate the Node should be spawned at (optional). + -- @param anchor - Wether the node should be locked in place or not (optional). -- function self:addNode( id, x, y, anchor ) assert( not nodes[id], "Node IDs must be unique." ); @@ -79,8 +80,8 @@ function Graph.new() end --- - -- Remove a node from the graph. This will also remove all edges pointing to - -- or originating from this node. + -- Remove a node from the graph. + -- This will also remove all edges pointing to or originating from this node. -- @param node - The node to remove from the graph. -- function self:removeNode( node ) @@ -164,7 +165,8 @@ function Graph.new() --- -- Returns the node the id is pointing to. - -- param id - The id to check for. + -- @param id - The id to check for. + -- function self:getNode( id ) return nodes[id]; end @@ -198,6 +200,14 @@ function Graph.new() return ( ( maxX - minX ) * 0.5 ) + minX, ( ( maxY - minY ) * 0.5 ) + minY; end + --- + -- Turn a node into an anchor. + -- Anchored nodes have fixed positions and can't be moved by the physical + -- forces. + -- @param id - The node's id. + -- @param x - The x coordinate to anchor the node to. + -- @param y - The x coordinate to anchor the node to. + -- function self:setAnchor( id, x, y ) nodes[id]:setPosition( x, y ); nodes[id]:setAnchor( true ); diff --git a/fd/Node.lua b/fd/Node.lua index dd7ce0e..a9ca7d8 100644 --- a/fd/Node.lua +++ b/fd/Node.lua @@ -11,6 +11,9 @@ local DEFAULT_MASS = 0.05; --- -- @param id - A unique id which will be used to reference this node. +-- @param x - The x coordinate the Node should be spawned at (optional). +-- @param y - The y coordinate the Node should be spawned at (optional). +-- @param anchor - Wether the node should be locked in place or not (optional). -- function Node.new( id, x, y, anchor ) local self = {}; @@ -40,6 +43,10 @@ function Node.new( id, x, y, anchor ) ay = clamp( -FORCE_MAX, ay + fy, FORCE_MAX ); end + --- + -- Attract this node to another node. + -- @param node - The node to use for force calculation. + -- function self:attractTo( node ) local dx, dy = px - node:getX(), py - node:getY(); local distance = math.sqrt(dx * dx + dy * dy); @@ -50,6 +57,10 @@ function Node.new( id, x, y, anchor ) applyForce( dx * strength, dy * strength ); end + --- + -- Repel this node from another node. + -- @param node - The node to use for force calculation. + -- function self:repelFrom( node ) local dx, dy = px - node:getX(), py - node:getY(); local distance = math.sqrt(dx * dx + dy * dy); @@ -63,6 +74,7 @@ function Node.new( id, x, y, anchor ) --- -- Update the node's position based on the calculated velocity and -- acceleration. + -- @param dt - The delta time between frames. -- function self:move( dt ) vx = (vx + ax * dt * NODE_SPEED) * DAMPING_FACTOR; @@ -72,9 +84,6 @@ function Node.new( id, x, y, anchor ) ax, ay = 0, 0; end - --- - -- Returns the node's unique identifier. - -- function self:getID() return id; end