Math.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. */
  4. THREE.Math = {
  5. // Clamp value to range <a, b>
  6. clamp: function ( x, a, b ) {
  7. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  8. },
  9. // Clamp value to range <a, inf)
  10. clampBottom: function ( x, a ) {
  11. return x < a ? a : x;
  12. },
  13. // Linear mapping from range <a1, a2> to range <b1, b2>
  14. mapLinear: function ( x, a1, a2, b1, b2 ) {
  15. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  16. },
  17. // Random float from <0, 1> with 16 bits of randomness
  18. // (standard Math.random() creates repetitive patterns when applied over larger space)
  19. random16: function () {
  20. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  21. },
  22. // Random integer from <low, high> interval
  23. randInt: function ( low, high ) {
  24. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  25. },
  26. // Random float from <low, high> interval
  27. randFloat: function ( low, high ) {
  28. return low + Math.random() * ( high - low );
  29. },
  30. // Random float from <-range/2, range/2> interval
  31. randFloatSpread: function ( range ) {
  32. return range * ( 0.5 - Math.random() );
  33. },
  34. sign: function ( x ) {
  35. return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  36. },
  37. degToRad: function() {
  38. var degreeToRadiansFactor = Math.PI / 180;
  39. return function ( degrees ) {
  40. return degrees * degreeToRadiansFactor;
  41. };
  42. }(),
  43. radToDeg: function() {
  44. var radianToDegreesFactor = 180 / Math.PI;
  45. return function ( radians ) {
  46. return radians * radianToDegreesFactor;
  47. };
  48. }()
  49. };