Created
December 22, 2016 02:08
-
-
Save riebschlager/ff4127f9ffa9e5b6d06918162b4d5e79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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