-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.lua
66 lines (52 loc) · 1.28 KB
/
math.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local lmath = setmetatable({}, {__index = math})
lmath.tau = math.pi * 2
local function lerp(t, a, b)
return a * (1 - t) + b * t
end
lmath.lerp = lerp
function lmath.damp(smoothing, dt, a, b)
return lerp(1 - smoothing ^ dt, a, b)
end
function lmath.root(x, rpow)
return x ^ (1 / rpow)
end
function lmath.cbrt(x)
return x ^ (1 / 3)
end
function lmath.ternary(condition, ifTrue, ifFalse)
if condition then return ifTrue end
return ifFalse
end
function lmath.isnan(x)
return x ~= x
end
function lmath.sign(x)
if x > 0 then return 1
elseif x < 0 then return -1 end
return 0
end
function lmath.clamp(value, vmin, vmax)
if vmin == nil then vmin = value end
if vmax == nil then vmax = value end
if value < vmin then return vmin
elseif value > vmax then return vmax end
return value
end
function lmath.wrap(value, vmin, vmax)
if vmax == nil then vmax, vmin = vmin, nil end
vmin = vmin or 0
vmax = vmax or 1
return (value - vmin) % (vmax - vmin) + vmin
end
function lmath.map(value, fromMin, fromMax, toMin, toMax)
return (value - fromMin) / (fromMax - fromMin) * (toMax - toMin) + toMin
end
local floor = math.floor
function lmath.round(x)
return floor(x + 0.5)
end
function lmath.roundStep(x, step)
if step == nil then step = 1 end
return floor(x / step + 0.5) * step
end
return lmath