Added lume.vector(), updated README and tests

This commit is contained in:
rxi 2016-09-19 21:55:59 +01:00
parent 59f90934aa
commit b569915d3e
3 changed files with 35 additions and 13 deletions

View File

@ -52,6 +52,12 @@ used when comparing distances.
### lume.angle(x1, y1, x2, y2) ### lume.angle(x1, y1, x2, y2)
Returns the angle between the two points. 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]]) ### lume.random([a [, b]])
Returns a random number between `a` and `b`. If only `a` is supplied a number 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 between `0` and `a` is returned. If no arguments are supplied a random number

View File

@ -111,6 +111,11 @@ function lume.angle(x1, y1, x2, y2)
end end
function lume.vector(angle, magnitude)
return math.cos(angle) * magnitude, math.sin(angle) * magnitude
end
function lume.random(a, b) function lume.random(a, b)
if not a then a, b = 0, 1 end if not a then a, b = 0, 1 end
if not b then b = 0 end if not b then b = 0 end

View File

@ -80,6 +80,18 @@ tests["lume.angle"] = function()
testeq( lume.angle(10, 10, 10, 30), math.rad(90) ) testeq( lume.angle(10, 10, 10, 30), math.rad(90) )
end 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 -- lume.random
tests["lume.random"] = function() tests["lume.random"] = function()
testeq( type(lume.random()), "number" ) testeq( type(lume.random()), "number" )
@ -617,4 +629,3 @@ end
tester.dotests(tests) tester.dotests(tests)
tester.test.global() tester.test.global()
tester.printresults() tester.printresults()