From 78050c9af7a5b20b8b33486d1a04baa32f07f238 Mon Sep 17 00:00:00 2001 From: Robert Machmer Date: Sat, 2 Jan 2016 15:17:20 +0100 Subject: [PATCH] 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. --- fd/Graph.lua | 15 +++++++++++---- fd/Node.lua | 9 +++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/fd/Graph.lua b/fd/Graph.lua index 11d1ab0..0a2f024 100644 --- a/fd/Graph.lua +++ b/fd/Graph.lua @@ -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 diff --git a/fd/Node.lua b/fd/Node.lua index 2af1235..b1ae6e7 100644 --- a/fd/Node.lua +++ b/fd/Node.lua @@ -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