Math.js 2.8 KB

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