From b569915d3e1458164da50d080e37561f4091e4ec Mon Sep 17 00:00:00 2001 From: rxi Date: Mon, 19 Sep 2016 21:55:59 +0100 Subject: [PATCH] Added `lume.vector()`, updated README and tests --- README.md | 6 ++++++ lume.lua | 5 +++++ test/test.lua | 37 ++++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8b33747..144c9f5 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,12 @@ used when comparing distances. ### lume.angle(x1, y1, x2, y2) Returns the angle between the two points. +### lume.vector(angle, magnitude) +Given an `angle` and `magnitude`, returns a vector. +```lua +local x, y = lume.vector(0, 10) -- Returns 10, 0 +``` + ### lume.random([a [, b]]) Returns a random number between `a` and `b`. If only `a` is supplied a number between `0` and `a` is returned. If no arguments are supplied a random number diff --git a/lume.lua b/lume.lua index a5f0d9a..14f19df 100644 --- a/lume.lua +++ b/lume.lua @@ -111,6 +111,11 @@ function lume.angle(x1, y1, x2, y2) end +function lume.vector(angle, magnitude) + return math.cos(angle) * magnitude, math.sin(angle) * magnitude +end + + function lume.random(a, b) if not a then a, b = 0, 1 end if not b then b = 0 end diff --git a/test/test.lua b/test/test.lua index 9ed3ad3..43fc17e 100644 --- a/test/test.lua +++ b/test/test.lua @@ -20,7 +20,7 @@ end -- lume.round tests["lume.round"] = function() - testeq( lume.round(.5), 1 ) + testeq( lume.round(.5), 1 ) testeq( lume.round(-.5), -1 ) testeq( lume.round(2.4), 2 ) testeq( lume.round(123, 10), 120 ) @@ -80,6 +80,18 @@ tests["lume.angle"] = function() testeq( lume.angle(10, 10, 10, 30), math.rad(90) ) end +-- lume.vector +tests["lume.vector"] = function() + local function cmp(a, b) return math.abs(a - b) < 10e-6 end + local x, y + x, y = lume.vector(0, 10) + testeq( cmp(x, 10) and cmp(y, 0), true ) + x, y = lume.vector(math.pi, 100) + testeq( cmp(x, -100) and cmp(y, 0), true ) + x, y = lume.vector(math.pi * 0.25, 100) + testeq( cmp(x, 70.71067811865476) and cmp(y, 70.71067811865476), true ) +end + -- lume.random tests["lume.random"] = function() testeq( type(lume.random()), "number" ) @@ -258,9 +270,9 @@ end -- lume.filter tests["lume.filter"] = function() - local t = lume.filter({1, 2, 3, 4, 5}, function(x) return x % 2 == 0 end ) + local t = lume.filter({1, 2, 3, 4, 5}, function(x) return x % 2 == 0 end ) testeq( t, {2, 4} ) - local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true) + local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true) testeq( t, {b=2} ) local t = lume.filter({{ x=1, y=1 }, { x=2, y=2 }, { x=1, y=3 }}, { x = 1 }) testeq( t, {{ x=1, y=1 }, {x=1, y=3}} ) @@ -268,9 +280,9 @@ end -- lume.reject tests["lume.reject"] = function() - local t = lume.reject({1, 2, 3, 4, 5}, function(x) return x % 2 == 0 end ) + local t = lume.reject({1, 2, 3, 4, 5}, function(x) return x % 2 == 0 end ) testeq( t, {1, 3, 5} ) - local t = lume.reject({a=1, b=2, c=3}, function(x) return x == 2 end, true) + local t = lume.reject({a=1, b=2, c=3}, function(x) return x == 2 end, true) testeq( t, {a=1, c=3} ) local t = lume.reject({{ x=1, y=1 }, { x=2, y=2 }, { x=1, y=3 }}, { x = 1 }) testeq( t, {{ x=2, y=2 }} ) @@ -354,7 +366,7 @@ tests["lume.first"] = function() local t = { "a", "b", "c", "d", "e" } testeq( lume.first(t), "a" ) testeq( lume.first(t, 1), { "a" } ) - testeq( lume.first(t, 2), { "a", "b" } ) + testeq( lume.first(t, 2), { "a", "b" } ) end -- lume.last @@ -362,7 +374,7 @@ tests["lume.last"] = function() local t = { "a", "b", "c", "d", "e" } testeq( lume.last(t), "e" ) testeq( lume.last(t, 1), { "e" } ) - testeq( lume.last(t, 2), { "d", "e" } ) + testeq( lume.last(t, 2), { "d", "e" } ) end -- lume.invert @@ -385,9 +397,9 @@ end -- lume.keys tests["lume.keys"] = function() testeq( lume.keys({}), {} ) - local t = lume.keys({ aaa = 1, bbb = 2, ccc = 3 }) + local t = lume.keys({ aaa = 1, bbb = 2, ccc = 3 }) table.sort(t) - testeq( t, {"aaa", "bbb", "ccc"} ) + testeq( t, {"aaa", "bbb", "ccc"} ) local t = lume.keys({ "x", "x", "x" }) testeq( t, {1, 2, 3} ) end @@ -544,14 +556,14 @@ tests["lume.trace"] = function() lume.trace("Hi world", 123.456, 1, nil) print = oldprint testeq( file:match(".lua$"), ".lua" ) - testeq( tonumber(line) ~= nil, true ) + testeq( tonumber(line) ~= nil, true ) testeq( msg, "Hi world 123.46 1 nil" ) end -- lume.dostring tests["lume.dostring"] = function() testeq( lume.dostring([[return "hello!"]]), "hello!" ) - testeq( lume.dostring([[return 12345]]), 12345 ) + testeq( lume.dostring([[return 12345]]), 12345 ) end -- lume.uuid @@ -606,7 +618,7 @@ end -- lume.chain tests["lume.chain"] = function() - local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result() + local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result() testeq( t, { 2, 4 } ) testeq( lume.chain(10):result(), 10 ) local t = lume({1, 2}):map(function(x) return x * 2 end):result() @@ -617,4 +629,3 @@ end tester.dotests(tests) tester.test.global() tester.printresults() -