From 7db38e8422b8512e80a3959e0d8243bc344c023c Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Wed, 29 Jun 2016 23:07:23 -0400 Subject: [PATCH] Implement a few math utilities --- src/util/math-util.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/util/math-util.js diff --git a/src/util/math-util.js b/src/util/math-util.js new file mode 100644 index 000000000..2dfd62a6e --- /dev/null +++ b/src/util/math-util.js @@ -0,0 +1,20 @@ +function MathUtil () {} + +MathUtil.degToRad = function (deg) { + return (Math.PI * (90 - deg)) / 180; +}; + +MathUtil.radToDeg = function (rad) { + return rad * 180 / Math.PI; +}; + +MathUtil.clamp = function (n, min, max) { + return Math.min(Math.max(n, min), max); +}; + +MathUtil.wrapClamp = function (n, min, max) { + var range = (max - min) + 1; + return n - Math.floor((n - min) / range) * range; +}; + +module.exports = MathUtil;