mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-26 15:44:35 +00:00
[fixed] mathx.wrap over-complicated impl, mathx.round (thanks @MikuAuahDark/@sharpobject)
This commit is contained in:
parent
7d3b568ac5
commit
5230e8f9d6
12
mathx.lua
12
mathx.lua
@ -8,12 +8,7 @@ local mathx = setmetatable({}, {
|
|||||||
|
|
||||||
--wrap v around range [lo, hi)
|
--wrap v around range [lo, hi)
|
||||||
function mathx.wrap(v, lo, hi)
|
function mathx.wrap(v, lo, hi)
|
||||||
local range = hi - lo
|
return (v - lo) % (hi - lo) + lo
|
||||||
local relative = v - lo
|
|
||||||
local relative_wrapped = relative % range
|
|
||||||
local relative_add = relative_wrapped + range
|
|
||||||
local final_wrap = relative_add % range
|
|
||||||
return lo + final_wrap
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--wrap i around the indices of t
|
--wrap i around the indices of t
|
||||||
@ -31,8 +26,11 @@ function mathx.clamp01(v)
|
|||||||
return mathx.clamp(v, 0, 1)
|
return mathx.clamp(v, 0, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
--round v to nearest whole
|
--round v to nearest whole, away from zero
|
||||||
function mathx.round(v)
|
function mathx.round(v)
|
||||||
|
if v < 0 then
|
||||||
|
return math.ceil(v - 0.5)
|
||||||
|
end
|
||||||
return math.floor(v + 0.5)
|
return math.floor(v + 0.5)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user