Math.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. var _Math = {
  6. DEG2RAD: Math.PI / 180,
  7. RAD2DEG: 180 / Math.PI,
  8. generateUUID: ( function () {
  9. // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  10. var lut = [];
  11. for ( var i = 0; i < 256; i ++ ) {
  12. lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
  13. }
  14. return function generateUUID() {
  15. var d0 = Math.random() * 0xffffffff | 0;
  16. var d1 = Math.random() * 0xffffffff | 0;
  17. var d2 = Math.random() * 0xffffffff | 0;
  18. var d3 = Math.random() * 0xffffffff | 0;
  19. var uuid = lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' +
  20. lut[ d1 & 0xff ] + lut[ d1 >> 8 & 0xff ] + '-' + lut[ d1 >> 16 & 0x0f | 0x40 ] + lut[ d1 >> 24 & 0xff ] + '-' +
  21. lut[ d2 & 0x3f | 0x80 ] + lut[ d2 >> 8 & 0xff ] + '-' + lut[ d2 >> 16 & 0xff ] + lut[ d2 >> 24 & 0xff ] +
  22. lut[ d3 & 0xff ] + lut[ d3 >> 8 & 0xff ] + lut[ d3 >> 16 & 0xff ] + lut[ d3 >> 24 & 0xff ];
  23. // .toUpperCase() here flattens concatenated strings to save heap memory space.
  24. return uuid.toUpperCase();
  25. };
  26. } )(),
  27. clamp: function ( value, min, max ) {
  28. return Math.max( min, Math.min( max, value ) );
  29. },
  30. // compute euclidian modulo of m % n
  31. // https://en.wikipedia.org/wiki/Modulo_operation
  32. euclideanModulo: function ( n, m ) {
  33. return ( ( n % m ) + m ) % m;
  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. // https://en.wikipedia.org/wiki/Linear_interpolation
  40. lerp: function ( x, y, t ) {
  41. return ( 1 - t ) * x + t * y;
  42. },
  43. // http://en.wikipedia.org/wiki/Smoothstep
  44. smoothstep: function ( x, min, max ) {
  45. if ( x <= min ) return 0;
  46. if ( x >= max ) return 1;
  47. x = ( x - min ) / ( max - min );
  48. return x * x * ( 3 - 2 * x );
  49. },
  50. smootherstep: function ( x, min, max ) {
  51. if ( x <= min ) return 0;
  52. if ( x >= max ) return 1;
  53. x = ( x - min ) / ( max - min );
  54. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  55. },
  56. // Random integer from <low, high> interval
  57. randInt: function ( low, high ) {
  58. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  59. },
  60. // Random float from <low, high> interval
  61. randFloat: function ( low, high ) {
  62. return low + Math.random() * ( high - low );
  63. },
  64. // Random float from <-range/2, range/2> interval
  65. randFloatSpread: function ( range ) {
  66. return range * ( 0.5 - Math.random() );
  67. },
  68. degToRad: function ( degrees ) {
  69. return degrees * _Math.DEG2RAD;
  70. },
  71. radToDeg: function ( radians ) {
  72. return radians * _Math.RAD2DEG;
  73. },
  74. isPowerOfTwo: function ( value ) {
  75. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  76. },
  77. ceilPowerOfTwo: function ( value ) {
  78. return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
  79. },
  80. floorPowerOfTwo: function ( value ) {
  81. return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
  82. }
  83. };
  84. export { _Math };