|
1 | | -# libfdgraph |
| 1 | +# Graphoon |
| 2 | + |
| 3 | +A force directed algorithm written in Lua. |
| 4 | + |
| 5 | + |
| 6 | +## Basic Usage |
| 7 | + |
| 8 | +The basic idea is that you create a new graph object, to which you can then add nodes and edges. |
| 9 | + |
| 10 | +``` |
| 11 | +local GraphLibrary = require('Graphoon').Graph |
| 12 | +
|
| 13 | +graph = GraphLibrary.new() |
| 14 | +graph:addNode( "Ash Williams" ) |
| 15 | +graph:addNode( "Necronomicon" ) |
| 16 | +graph:connectIDs( "Ash Williams", "Necronomicon" ) |
| 17 | +``` |
| 18 | + |
| 19 | +By itself Graphoon only provides functionality for creating the graph and calculating the layout based on physical attraction and repulsion forces. |
| 20 | + |
| 21 | +It provides a ```draw``` and ```update``` function, which can be used to easily write your own rendering code. |
| 22 | + |
| 23 | +The ```draw``` function should be called with two callback functions. The first callback will be used for all nodes and the second one for all the edges. |
| 24 | + |
| 25 | +``` |
| 26 | +graph:draw( function( node ) |
| 27 | + local x, y = node:getPosition() |
| 28 | + drawCircle( 'fill', x, y, 10 ) |
| 29 | + end, |
| 30 | + function( edge ) |
| 31 | + local ox, oy = edge.origin:getPosition() |
| 32 | + local tx, ty = edge.target:getPosition() |
| 33 | + drawLine( ox, oy, tx, ty ) |
| 34 | + end) |
| 35 | +``` |
| 36 | + |
| 37 | +At its simplest the force calculations can be updated via ```graph:update( dt )```, but the ```update``` function also can receive optional callbacks for both nodes and edges. |
| 38 | + |
| 39 | +## Advanced usage |
| 40 | + |
| 41 | +### Using anchors |
| 42 | + |
| 43 | +Anchors can be used to attach a node to a certain position on the screen. This can be useful if you want to center a certain node for example. |
| 44 | + |
| 45 | +This can either be done directly via the constructor of the node: |
| 46 | + |
| 47 | +``` |
| 48 | +-- Anchor the node to the center of the screen. |
| 49 | +graph:addNode( "Ash Williams", screenX * 0.5, screenY * 0.5, true ) |
| 50 | +``` |
| 51 | + |
| 52 | +Or by using the ```setAnchor``` function: |
| 53 | + |
| 54 | +``` |
| 55 | +-- Invert anchor status |
| 56 | +node:setAnchor( not node:isAnchor() ) |
| 57 | +``` |
| 58 | + |
0 commit comments