mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Add documentation for math-util functions.
This commit is contained in:
parent
798368b6c5
commit
2e01caa8a6
1 changed files with 28 additions and 0 deletions
|
@ -1,17 +1,45 @@
|
|||
function MathUtil () {}
|
||||
|
||||
/**
|
||||
* Convert a value from degrees to radians.
|
||||
* @param {!number} deg Value in degrees.
|
||||
* @return {!number} Equivalent value in radians.
|
||||
*/
|
||||
MathUtil.degToRad = function (deg) {
|
||||
return (Math.PI * (90 - deg)) / 180;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert a value from radians to degrees.
|
||||
* @param {!number} rad Value in radians.
|
||||
* @return {!number} Equivalent value in degrees.
|
||||
*/
|
||||
MathUtil.radToDeg = function (rad) {
|
||||
return rad * 180 / Math.PI;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
MathUtil.clamp = function (n, min, max) {
|
||||
return Math.min(Math.max(n, min), max);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
MathUtil.wrapClamp = function (n, min, max) {
|
||||
var range = (max - min) + 1;
|
||||
return n - Math.floor((n - min) / range) * range;
|
||||
|
|
Loading…
Reference in a new issue