Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local oldmath = math;
- local newmath = {};
- for k,v in pairs(oldmath) do
- newmath[k] = v;
- end
- local function CheckGlobalFunctionExists(name,func)
- if not _G[name] then
- _G[name] = func;
- end
- end
- function newmath.clamp(val,min,max)
- return (val <= min) and min or (val >= max) and max or val;
- end
- function newmath.frac(num)
- return num - math2.trunc(num);
- end
- function newmath.mod(a,b)
- return a - math.floor(a/b) * b;
- end
- function newmath.round(num,ndp)
- local multi = 10 ^ (ndp or 0);
- if num > 0 then
- return math.floor(num * multi + 0.5) / multi;
- else
- return math.ceil(num * multi - 0.5) / multi;
- end
- end
- function newmath.sign(num)
- return num / math.abs(num);
- end
- function newmath.trunc(num,ndp)
- local multi = 10 ^ (ndp or 0);
- if num > 0 then
- return math.floor(num * multi) / multi;
- else
- return math.ceil(num * multi) / multi;
- end
- end
- local function approach(val,other_val,to_move)
- if val ~= other_val then
- local delta = other_val - val;
- local sign = delta / math.abs(delta);
- local tomove = sign * to_move;
- if math.abs(tomove) > math.abs(delta) then
- tomove = delta;
- end
- val = val + tomove;
- end
- return val;
- end
- local function lerp(x,l,h)
- return x * (h - l) + l;
- end
- local function scale(x,l1,h1,l2,h2)
- return (x - l1) * (h2 - l2) / (h1 - l1) + l2;
- end
- CheckGlobalFunctionExists('approach',approach);
- CheckGlobalFunctionExists('lerp',lerp);
- CheckGlobalFunctionExists('scale',scale);
- return newmath;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement