mirror of
https://github.com/rm-code/Graphoon.git
synced 2024-11-16 18:24:22 +00:00
Improve function to create a new graph based on data
The random function is now seeded properly and uses the x, y, w, and h parameters passed to the function to determine in what range to randomly place the nodes.
This commit is contained in:
parent
fd957a3881
commit
1ad54dd376
18
Graph.lua
18
Graph.lua
@ -14,15 +14,25 @@ function Graph.new()
|
|||||||
|
|
||||||
local center = Node.new( 'center ', love.graphics.getWidth() * 0.5, love.graphics.getHeight() * 0.5);
|
local center = Node.new( 'center ', love.graphics.getWidth() * 0.5, love.graphics.getHeight() * 0.5);
|
||||||
|
|
||||||
function self:init( table )
|
---
|
||||||
math.randomseed(120123091239581834213143141);
|
-- Creates a new graph based on the information passed via the table
|
||||||
|
-- parameter. It creates all nodes and connects them via edges.
|
||||||
|
-- @param table - A table containing information about nodes and edges.
|
||||||
|
-- @param x - The minimum x value to use when randomly spawning nodes.
|
||||||
|
-- @param y - The minimum y value to use when randomly spawning nodes.
|
||||||
|
-- @param w - The maximum x value to use when randomly spawning nodes.
|
||||||
|
-- @param h - The maximum y value to use when randomly spawning nodes.
|
||||||
|
--
|
||||||
|
function self:create( table, x, y, w, h )
|
||||||
|
math.randomseed( os.time() );
|
||||||
|
|
||||||
for _, id in pairs( table.nodes ) do
|
for _, id in pairs( table.nodes ) do
|
||||||
self:addNode( Node.new( id, math.random(100, 800), math.random(100, 400) ) );
|
local rx, ry = math.random( x, w ), math.random( y, h );
|
||||||
|
self:addNode( Node.new( id, rx, ry ));
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, edge in pairs( table.edges ) do
|
for _, edge in pairs( table.edges ) do
|
||||||
self:addEdge( nodes[edge[1]], nodes[edge[2]] );
|
self:addEdge( nodes[edge.origin], nodes[edge.target] );
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user