Math.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.Math = {
  6. PI2: Math.PI * 2,
  7. generateUUID: function () {
  8. // http://www.broofa.com/Tools/Math.uuid.htm
  9. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  10. var uuid = new Array(36);
  11. var rnd = 0, r;
  12. return function () {
  13. for ( var i = 0; i < 36; i ++ ) {
  14. if ( i == 8 || i == 13 || i == 18 || i == 23 ) {
  15. uuid[ i ] = '-';
  16. } else if ( i == 14 ) {
  17. uuid[ i ] = '4';
  18. } else {
  19. if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
  20. r = rnd & 0xf;
  21. rnd = rnd >> 4;
  22. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  23. }
  24. }
  25. return uuid.join('');
  26. };
  27. }(),
  28. // Clamp value to range <a, b>
  29. clamp: function ( x, a, b ) {
  30. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  31. },
  32. // Clamp value to range <a, inf)
  33. clampBottom: function ( x, a ) {
  34. return x < a ? a : x;
  35. },
  36. // Linear mapping from range <a1, a2> to range <b1, b2>
  37. mapLinear: function ( x, a1, a2, b1, b2 ) {
  38. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  39. },
  40. // http://en.wikipedia.org/wiki/Smoothstep
  41. smoothstep: function ( x, min, max ) {
  42. if ( x <= min ) return 0;
  43. if ( x >= max ) return 1;
  44. x = ( x - min )/( max - min );
  45. return x*x*(3 - 2*x);
  46. },
  47. smootherstep: 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*x*(x*(x*6 - 15) + 10);
  52. },
  53. // Random float from <0, 1> with 16 bits of randomness
  54. // (standard Math.random() creates repetitive patterns when applied over larger space)
  55. random16: function () {
  56. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  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. sign: function ( x ) {
  71. return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  72. },
  73. degToRad: function() {
  74. var degreeToRadiansFactor = Math.PI / 180;
  75. return function ( degrees ) {
  76. return degrees * degreeToRadiansFactor;
  77. };
  78. }(),
  79. radToDeg: function() {
  80. var radianToDegreesFactor = 180 / Math.PI;
  81. return function ( radians ) {
  82. return radians * radianToDegreesFactor;
  83. };
  84. }(),
  85. isPowerOfTwo: function ( value ) {
  86. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  87. }
  88. };