Skip to content

Instantly share code, notes, and snippets.

@riebschlager
Created December 22, 2016 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riebschlager/ff4127f9ffa9e5b6d06918162b4d5e79 to your computer and use it in GitHub Desktop.
Save riebschlager/ff4127f9ffa9e5b6d06918162b4d5e79 to your computer and use it in GitHub Desktop.
function Util() {}
Util.prototype.map = function(input, oldMin, oldMax, newMin, newMax) {
return ((input - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin;
};
Util.prototype.random = function(min, max) {
var rand = Math.random();
if (arguments.length === 0) {
return rand;
} else if (arguments.length === 1) {
if (arguments[0] instanceof Array) {
return arguments[0][Math.floor(rand * arguments[0].length)];
} else {
return rand * min;
}
} else {
if (min > max) {
var tmp = min;
min = max;
max = tmp;
}
return rand * (max - min) + min;
}
};
Util.prototype.dist = function(x1, y1, z1, x2, y2, z2) {
if (arguments.length === 4) {
return Math.sqrt((z1 - x1) * (z1 - x1) + (x2 - y1) * (x2 - y1));
} else if (arguments.length === 6) {
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment