From e6b9c84a6c45030b2704c0411e5f1576d56d4f36 Mon Sep 17 00:00:00 2001 From: Colby Klein Date: Sat, 15 Aug 2015 01:19:32 -0700 Subject: [PATCH] Add utils.deadzone and threshold + tests --- modules/utils.lua | 9 +++++++++ spec/utils_spec.lua | 23 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/modules/utils.lua b/modules/utils.lua index 68494a3..683fac6 100644 --- a/modules/utils.lua +++ b/modules/utils.lua @@ -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 diff --git a/spec/utils_spec.lua b/spec/utils_spec.lua index 846710d..dd68444 100644 --- a/spec/utils_spec.lua +++ b/spec/utils_spec.lua @@ -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)