Math.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. var _Math;
  2. /**
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author mrdoob / http://mrdoob.com/
  5. */
  6. _Math = {
  7. DEG2RAD: Math.PI / 180,
  8. RAD2DEG: 180 / Math.PI,
  9. generateUUID: function () {
  10. // http://www.broofa.com/Tools/Math.uuid.htm
  11. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split( '' );
  12. var uuid = new Array( 36 );
  13. var rnd = 0, r;
  14. return function generateUUID() {
  15. for ( var i = 0; i < 36; i ++ ) {
  16. if ( i === 8 || i === 13 || i === 18 || i === 23 ) {
  17. uuid[ i ] = '-';
  18. } else if ( i === 14 ) {
  19. uuid[ i ] = '4';
  20. } else {
  21. if ( rnd <= 0x02 ) rnd = 0x2000000 + ( Math.random() * 0x1000000 ) | 0;
  22. r = rnd & 0xf;
  23. rnd = rnd >> 4;
  24. uuid[ i ] = chars[ ( i === 19 ) ? ( r & 0x3 ) | 0x8 : r ];
  25. }
  26. }
  27. return uuid.join( '' );
  28. };
  29. }(),
  30. clamp: function ( value, min, max ) {
  31. return Math.max( min, Math.min( max, value ) );
  32. },
  33. // compute euclidian modulo of m % n
  34. // https://en.wikipedia.org/wiki/Modulo_operation
  35. euclideanModulo: function ( n, m ) {
  36. return ( ( n % m ) + m ) % m;
  37. },
  38. // Linear mapping from range <a1, a2> to range <b1, b2>
  39. mapLinear: function ( x, a1, a2, b1, b2 ) {
  40. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  41. },
  42. // https://en.wikipedia.org/wiki/Linear_interpolation
  43. lerp: function ( x, y, t ) {
  44. return ( 1 - t ) * x + t * y;
  45. },
  46. // http://en.wikipedia.org/wiki/Smoothstep
  47. smoothstep: function ( x, min, max ) {
  48. if ( x <= min ) return 0;
  49. if ( x >= max ) return 1;
  50. x = ( x - min ) / ( max - min );
  51. return x * x * ( 3 - 2 * x );
  52. },
  53. smootherstep: function ( x, min, max ) {
  54. if ( x <= min ) return 0;
  55. if ( x >= max ) return 1;
  56. x = ( x - min ) / ( max - min );
  57. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  58. },
  59. random16: function () {
  60. console.warn( 'THREE.Math.random16() has been deprecated. Use Math.random() instead.' );
  61. return Math.random();
  62. },
  63. // Random integer from <low, high> interval
  64. randInt: function ( low, high ) {
  65. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  66. },
  67. // Random float from <low, high> interval
  68. randFloat: function ( low, high ) {
  69. return low + Math.random() * ( high - low );
  70. },
  71. // Random float from <-range/2, range/2> interval
  72. randFloatSpread: function ( range ) {
  73. return range * ( 0.5 - Math.random() );
  74. },
  75. degToRad: function ( degrees ) {
  76. return degrees * _Math.DEG2RAD;
  77. },
  78. radToDeg: function ( radians ) {
  79. return radians * _Math.RAD2DEG;
  80. },
  81. isPowerOfTwo: function ( value ) {
  82. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  83. },
  84. nearestPowerOfTwo: function ( value ) {
  85. return Math.pow( 2, Math.round( Math.log( value ) / Math.LN2 ) );
  86. },
  87. nextPowerOfTwo: function ( value ) {
  88. value --;
  89. value |= value >> 1;
  90. value |= value >> 2;
  91. value |= value >> 4;
  92. value |= value >> 8;
  93. value |= value >> 16;
  94. value ++;
  95. return value;
  96. }
  97. };
  98. export { _Math };
粤ICP备19079148号