math.scalar.functions.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Extract int value
  3. * @param value number value
  4. * @returns int value
  5. */
  6. export function ExtractAsInt(value) {
  7. return parseInt(value.toString().replace(/\W/g, ""));
  8. }
  9. /**
  10. * Boolean : true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
  11. * @param a number
  12. * @param b number
  13. * @param epsilon (default = 1.401298E-45)
  14. * @returns true if the absolute difference between a and b is lower than epsilon (default = 1.401298E-45)
  15. */
  16. export function WithinEpsilon(a, b, epsilon = 1.401298e-45) {
  17. return Math.abs(a - b) <= epsilon;
  18. }
  19. /**
  20. * Returns a random float number between and min and max values
  21. * @param min min value of random
  22. * @param max max value of random
  23. * @returns random value
  24. */
  25. export function RandomRange(min, max) {
  26. if (min === max) {
  27. return min;
  28. }
  29. return Math.random() * (max - min) + min;
  30. }
  31. /**
  32. * Creates a new scalar with values linearly interpolated of "amount" between the start scalar and the end scalar.
  33. * @param start start value
  34. * @param end target value
  35. * @param amount amount to lerp between
  36. * @returns the lerped value
  37. */
  38. export function Lerp(start, end, amount) {
  39. return start + (end - start) * amount;
  40. }
  41. /**
  42. * Returns the value itself if it's between min and max.
  43. * Returns min if the value is lower than min.
  44. * Returns max if the value is greater than max.
  45. * @param value the value to clmap
  46. * @param min the min value to clamp to (default: 0)
  47. * @param max the max value to clamp to (default: 1)
  48. * @returns the clamped value
  49. */
  50. export function Clamp(value, min = 0, max = 1) {
  51. return Math.min(max, Math.max(min, value));
  52. }
  53. /**
  54. * Returns the angle converted to equivalent value between -Math.PI and Math.PI radians.
  55. * @param angle The angle to normalize in radian.
  56. * @returns The converted angle.
  57. */
  58. export function NormalizeRadians(angle) {
  59. // More precise but slower version kept for reference.
  60. // angle = angle % Tools.TwoPi;
  61. // angle = (angle + Tools.TwoPi) % Tools.TwoPi;
  62. //if (angle > Math.PI) {
  63. // angle -= Tools.TwoPi;
  64. //}
  65. angle -= Math.PI * 2 * Math.floor((angle + Math.PI) / (Math.PI * 2));
  66. return angle;
  67. }
  68. /**
  69. * Returns a string : the upper case translation of the number i to hexadecimal.
  70. * @param i number
  71. * @returns the upper case translation of the number i to hexadecimal.
  72. */
  73. export function ToHex(i) {
  74. const str = i.toString(16);
  75. if (i <= 15) {
  76. return ("0" + str).toUpperCase();
  77. }
  78. return str.toUpperCase();
  79. }
  80. //# sourceMappingURL=math.scalar.functions.js.map