Skip to content

Instantly share code, notes, and snippets.

@y-nk
Created November 18, 2015 11:04
Show Gist options
  • Save y-nk/f993cea0a6adf4fdeda3 to your computer and use it in GitHub Desktop.
Save y-nk/f993cea0a6adf4fdeda3 to your computer and use it in GitHub Desktop.
;(function(m)
{
m.abs = function(n)
{ return n > 0 ? n : -n; };
m.clamp = function(n, min, max)
{ return n < min ? min : n > max ? max : n; };
m.lerp = function(n, n1, n2)
{ return (n1 * (1 - n)) + (n2 * n); };
m.normalize = function (n, min, max)
{ return (n - min) / (max - min); };
m.between = function(n, min, max)
{ return (min <= n) && (n <= max); };
m.map = function(n, min1, max1, min2, max2)
{ return m.lerp(m.normalize(m.clamp(n, min1, max1), min1, max1), min2, max2); };
m.loop = function loop(n, min, max)
{
var p = m.floor(max - min + 1),
q = (n - min) % p;
if(q < 0) { q += p; }
return min + q;
};
m.round = function(n)
{ return (n + 0.5) | 0; };
var a = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000];
m.trunc = function(n, d)
{
d = m.clamp(d, 0, 10);
return m.round(n * a[d]) / a[d];
};
})(Math)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment