From 59a055da990cb2e2cc425da845126ae1ad059116 Mon Sep 17 00:00:00 2001 From: Robert Machmer Date: Thu, 24 Dec 2015 16:56:32 +0100 Subject: [PATCH] Add optional name parameter for Nodes This parameter can be accessed via the getName getter. If no value is passed to the constructor the name variable will default to the id. --- fd/Graph.lua | 4 ++-- fd/Node.lua | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fd/Graph.lua b/fd/Graph.lua index 74368fc..f56c4b3 100644 --- a/fd/Graph.lua +++ b/fd/Graph.lua @@ -70,9 +70,9 @@ function Graph.new() -- @param x - The x-coordinate at which to place the new node. -- @param y - The y-coordinate at which to place the new node. -- - function self:addNode( id, x, y ) + function self:addNode( id, name, x, y ) assert( not nodes[id], "Node IDs must be unique." ); - nodes[id] = Node.new( id, x, y ); + nodes[id] = Node.new( id, name, x, y ); end --- diff --git a/fd/Node.lua b/fd/Node.lua index 85d60ed..2af1235 100644 --- a/fd/Node.lua +++ b/fd/Node.lua @@ -12,9 +12,10 @@ local DEFAULT_MASS = 0.05; --- -- @param id - A unique id which will be used to reference this node. -- -function Node.new( id, x, y ) +function Node.new( id, name, x, y ) local self = {}; + local name = name or id; local px, py = x or 0, y or 0; local ax, ay = 0, 0; local vx, vy = 0, 0; @@ -79,6 +80,10 @@ function Node.new( id, x, y ) return id; end + function self:getName() + return name; + end + function self:getX() return px; end