Add anchor nodes

Nodes set as anchors won't be affected by the force directed layout.
They can be used to fixate the graph to a certain area.
This commit is contained in:
Robert Machmer 2016-01-02 15:17:20 +01:00
parent 47b7624261
commit 78050c9af7
2 changed files with 20 additions and 4 deletions

View File

@ -148,12 +148,14 @@ function Graph.new()
nodeA:attractTo( attractionPoint );
end
for _, nodeB in pairs( nodes ) do
if nodeA ~= nodeB then
nodeA:repelFrom( nodeB );
if not nodeA:isAnchor() then
for _, nodeB in pairs( nodes ) do
if nodeA ~= nodeB then
nodeA:repelFrom( nodeB );
end
end
nodeA:move( dt );
end
nodeA:move( dt );
minX, maxX, minY, maxY = updateBoundaries( minX, maxX, minY, maxY, nodeA:getPosition() );
end
@ -213,6 +215,11 @@ function Graph.new()
return ( ( maxX - minX ) * 0.5 ) + minX, ( ( maxY - minY ) * 0.5 ) + minY;
end
function self:setAnchor( id, x, y )
nodes[id]:setPosition( x, y );
nodes[id]:setAnchor( true );
end
return self;
end

View File

@ -16,6 +16,7 @@ function Node.new( id, name, x, y )
local self = {};
local name = name or id;
local anchor = false;
local px, py = x or 0, y or 0;
local ax, ay = 0, 0;
local vx, vy = 0, 0;
@ -100,6 +101,14 @@ function Node.new( id, name, x, y )
px, py = nx, ny;
end
function self:setAnchor( nanchor )
anchor = nanchor;
end
function self:isAnchor()
return anchor;
end
return self;
end