summaryrefslogtreecommitdiff
path: root/math.lua
blob: a60ca0cb9d81294867e9c97633e019cdbf17495f (plain)
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
local M = {}

function M.lerp(a,b,t) return (1-t)*a + t*b end
function M.smoothstep(x)
	if x<0 then return 0 end
	if x>1 then return 1 end
	return x*x*(3 - 2*x)
end
function M.slerp(a,b,t) return M.lerp(a,b,M.smoothstep(t)) end


function M.sign(x)
	if x == 0 then return 0
	elseif x < 0 then return -1
	elseif x > 0 then return 1
	end
end

function M.clamp(x,minv,maxv)
	return math.min(math.max(x,minv),maxv)
end

function M.clerp(a,b,t) return M.lerp(a,b,M.clamp(t,0,1)) end
function M.round(x) return math.floor(x+0.5) end

-- [-1,1] -> [0,1]
function M.squish(x) return (x/2)+0.5 end
-- [0,1] -> [-1,1]
function M.unsquish(x) return (x*2)-1 end
function M.sqlerp(a,b,t) return M.lerp(a,b,M.squish(t)) end

return M