2016-10-23 17:55:31 -04:00
|
|
|
var MathUtil = function () {};
|
2016-06-29 23:07:23 -04:00
|
|
|
|
2016-07-06 14:04:36 -04:00
|
|
|
/**
|
|
|
|
* Convert a value from degrees to radians.
|
|
|
|
* @param {!number} deg Value in degrees.
|
|
|
|
* @return {!number} Equivalent value in radians.
|
|
|
|
*/
|
2016-06-29 23:07:23 -04:00
|
|
|
MathUtil.degToRad = function (deg) {
|
2016-10-17 11:52:02 -04:00
|
|
|
return deg * Math.PI / 180;
|
2016-06-29 23:07:23 -04:00
|
|
|
};
|
|
|
|
|
2016-07-06 14:04:36 -04:00
|
|
|
/**
|
|
|
|
* Convert a value from radians to degrees.
|
|
|
|
* @param {!number} rad Value in radians.
|
|
|
|
* @return {!number} Equivalent value in degrees.
|
|
|
|
*/
|
2016-06-29 23:07:23 -04:00
|
|
|
MathUtil.radToDeg = function (rad) {
|
|
|
|
return rad * 180 / Math.PI;
|
|
|
|
};
|
|
|
|
|
2016-07-06 14:04:36 -04:00
|
|
|
/**
|
|
|
|
* Clamp a number between two limits.
|
|
|
|
* If n < min, return min. If n > max, return max. Else, return n.
|
|
|
|
* @param {!number} n Number to clamp.
|
|
|
|
* @param {!number} min Minimum limit.
|
|
|
|
* @param {!number} max Maximum limit.
|
|
|
|
* @return {!number} Value of n clamped to min and max.
|
|
|
|
*/
|
2016-06-29 23:07:23 -04:00
|
|
|
MathUtil.clamp = function (n, min, max) {
|
|
|
|
return Math.min(Math.max(n, min), max);
|
|
|
|
};
|
|
|
|
|
2016-07-06 14:04:36 -04:00
|
|
|
/**
|
|
|
|
* Keep a number between two limits, wrapping "extra" into the range.
|
|
|
|
* e.g., wrapClamp(7, 1, 5) == 2
|
|
|
|
* wrapClamp(0, 1, 5) == 5
|
|
|
|
* wrapClamp(-11, -10, 6) == 6, etc.
|
|
|
|
* @param {!number} n Number to wrap.
|
|
|
|
* @param {!number} min Minimum limit.
|
|
|
|
* @param {!number} max Maximum limit.
|
|
|
|
* @return {!number} Value of n wrapped between min and max.
|
|
|
|
*/
|
2016-06-29 23:07:23 -04:00
|
|
|
MathUtil.wrapClamp = function (n, min, max) {
|
|
|
|
var range = (max - min) + 1;
|
2016-10-23 17:55:31 -04:00
|
|
|
return n - (Math.floor((n - min) / range) * range);
|
2016-06-29 23:07:23 -04:00
|
|
|
};
|
|
|
|
|
2017-01-31 19:05:54 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a value from tan function in degrees.
|
|
|
|
* @param {!number} angle in degrees
|
|
|
|
* @return {!number} Correct tan value
|
|
|
|
*/
|
|
|
|
MathUtil.tan = function (angle) {
|
|
|
|
angle = angle % 360;
|
|
|
|
switch (angle) {
|
|
|
|
case -270:
|
|
|
|
case 90:
|
|
|
|
return Infinity;
|
|
|
|
case -90:
|
|
|
|
case 270:
|
|
|
|
return -Infinity;
|
|
|
|
default:
|
|
|
|
return parseFloat(Math.tan((Math.PI * angle) / 180).toFixed(10));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-29 23:07:23 -04:00
|
|
|
module.exports = MathUtil;
|