From fc7641eb268b26acb4b7cd1e3f7e3dfa96b9075d Mon Sep 17 00:00:00 2001 From: Stefans Mezulis Date: Mon, 28 Mar 2016 18:18:54 +0100 Subject: [PATCH 1/3] Update Path class to allow different distances A heuristic function can now be passed to the `getLength` method to specify the distance metric to be used. --- jumper/core/path.lua | 7 +++++-- specs/path_specs.lua | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/jumper/core/path.lua b/jumper/core/path.lua index 826c5e4..dd954fa 100644 --- a/jumper/core/path.lua +++ b/jumper/core/path.lua @@ -67,12 +67,15 @@ if (...) then --- Evaluates the `path` length -- @class function + -- @tparam function heuristic Optional heuristic function for determining. + -- distance between nodes. Default: @{Heuristic.EUCLIDIAN}. -- @treturn number the `path` length -- @usage local len = p:getLength() - function Path:getLength() + function Path:getLength(heuristic) + local heuristic = heuristic or Heuristic.EUCLIDIAN local len = 0 for i = 2,#self._nodes do - len = len + Heuristic.EUCLIDIAN(self._nodes[i], self._nodes[i-1]) + len = len + heuristic(self._nodes[i], self._nodes[i-1]) end return len end diff --git a/specs/path_specs.lua b/specs/path_specs.lua index cb880fe..51f7925 100644 --- a/specs/path_specs.lua +++ b/specs/path_specs.lua @@ -43,6 +43,12 @@ context('Module Path', function() p = Path() for i = 1,10 do p._nodes[#p._nodes+1] = Node(i,i) end assert_less_than(p:getLength()-9*math.sqrt(2),1e-6) + + --Test with diagonal distance + local Heuristics = require ('jumper.core.heuristics') + p = Path() + for i = 1,10 do p._nodes[#p._nodes+1] = Node(i,i) end + assert_equal(p:getLength(Heuristics.DIAGONAL), 9) end) test('Path:fill() interpolates a path', function() From 351f743c99ce482719a68d7f9ff282cb5b353a53 Mon Sep 17 00:00:00 2001 From: Stefans Mezulis Date: Mon, 28 Mar 2016 18:29:45 +0100 Subject: [PATCH 2/3] Remove invalid string escape. --- specs/pathfinder_specs.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/pathfinder_specs.lua b/specs/pathfinder_specs.lua index bbba5b3..954dd14 100644 --- a/specs/pathfinder_specs.lua +++ b/specs/pathfinder_specs.lua @@ -140,7 +140,7 @@ context('Module Pathfinder', function() assert_equal(pf:getFinder(), 'ASTAR') end) - test('Passing nil sets \'ASTAR\` as the finder if no previous finder was set, is \'ASTAR\'', function() + test('Passing nil sets \'ASTAR\' as the finder if no previous finder was set, is \'ASTAR\'', function() local pf = PF(grid) pf:setFinder() assert_equal(pf:getFinder(), 'ASTAR') From a02e4666a2d395aa212fd10eb403a6731515cc74 Mon Sep 17 00:00:00 2001 From: Stefans Mezulis Date: Tue, 29 Mar 2016 19:36:03 +0100 Subject: [PATCH 3/3] Add "simplify" method to Path class. --- jumper/core/path.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/jumper/core/path.lua b/jumper/core/path.lua index dd954fa..2819af3 100644 --- a/jumper/core/path.lua +++ b/jumper/core/path.lua @@ -196,6 +196,18 @@ if (...) then return self end + + --- Return a simplified form of the path. + -- @treturn table Array of `{x, y}` values in the path. + -- @usage x1, y1 = unpack(path:simplify()[1]) + function Path:simplify() + local simple = {} + for node in self:iter() do + simple[#simple + 1] = {node._x, node._y} + end + return simple + end + return setmetatable(Path, {__call = function(self,...) return Path:new(...)