Math.js 2.7 KB

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