Add utils.deadzone and threshold + tests

This commit is contained in:
Colby Klein 2015-08-15 01:19:32 -07:00
parent e7db18aa3f
commit e6b9c84a6c
2 changed files with 31 additions and 1 deletions

View File

@ -4,6 +4,15 @@ function utils.clamp(v, min, max)
return math.max(math.min(v, max), min)
end
function utils.deadzone(value, size)
return math.abs(value) >= size and value or 0
end
-- I know, it barely saves any typing at all.
function utils.threshold(value, threshold)
return math.abs(value) >= threshold
end
function utils.map(v, min_in, max_in, min_out, max_out)
return ((v) - (min_in)) * ((max_out) - (min_out)) / ((max_in) - (min_in)) + (min_out)
end

View File

@ -35,5 +35,26 @@ describe("utils:", function()
assert.is.is_true(math.abs(utils.round(5.5555, 0.1) - 5.6) < constants.FLT_EPSILON)
end)
pending "lerp"
it("testing deadzone", function()
assert.is.equal(utils.deadzone(0.5, 0.05), 0.5)
assert.is.equal(utils.deadzone(0.02, 0.05), 0.0)
assert.is.equal(utils.deadzone(-0.1, 0.02), -0.1)
end)
it("testing threshold", function()
assert.is_true(utils.threshold(0.5, 0.25))
assert.is_true(utils.threshold(0.25, 0.25))
assert.is_false(utils.threshold(-0.1, 0.5))
assert.is_true(utils.threshold(-0.25, 0.1))
end)
it("testing lerp", function()
local l = 0.5
local h = 20.0
assert.is.equal(utils.lerp(0, l, h), l)
assert.is.equal(utils.lerp(1, l, h), h)
assert.is.equal(utils.lerp(0.5, l, h), (l+h)/2)
end)
pending "smoothstep"
end)