Math.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 ).toUpperCase();
  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. return 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. };
  24. } )(),
  25. clamp: function ( value, min, max ) {
  26. return Math.max( min, Math.min( max, value ) );
  27. },
  28. // compute euclidian modulo of m % n
  29. // https://en.wikipedia.org/wiki/Modulo_operation
  30. euclideanModulo: function ( n, m ) {
  31. return ( ( n % m ) + m ) % m;
  32. },
  33. // Linear mapping from range <a1, a2> to range <b1, b2>
  34. mapLinear: function ( x, a1, a2, b1, b2 ) {
  35. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  36. },
  37. // https://en.wikipedia.org/wiki/Linear_interpolation
  38. lerp: function ( x, y, t ) {
  39. return ( 1 - t ) * x + t * y;
  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. // Random integer from <low, high> interval
  55. randInt: function ( low, high ) {
  56. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  57. },
  58. // Random float from <low, high> interval
  59. randFloat: function ( low, high ) {
  60. return low + Math.random() * ( high - low );
  61. },
  62. // Random float from <-range/2, range/2> interval
  63. randFloatSpread: function ( range ) {
  64. return range * ( 0.5 - Math.random() );
  65. },
  66. degToRad: function ( degrees ) {
  67. return degrees * _Math.DEG2RAD;
  68. },
  69. radToDeg: function ( radians ) {
  70. return radians * _Math.RAD2DEG;
  71. },
  72. isPowerOfTwo: function ( value ) {
  73. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  74. },
  75. ceilPowerOfTwo: function ( value ) {
  76. return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
  77. },
  78. floorPowerOfTwo: function ( value ) {
  79. return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
  80. }
  81. };
  82. export { _Math };
粤ICP备19079148号