math.js 616 B

12345678910111213141516171819202122232425262728
  1. export const math = (function() {
  2. return {
  3. rand_range: function (a, b) {
  4. return Math.random() * (b - a) + a;
  5. },
  6. rand_normalish: function () {
  7. const r = Math.random() + Math.random() + Math.random() + Math.random();
  8. return (r / 4.0) * 2.0 - 1;
  9. },
  10. rand_int: function(a, b) {
  11. return Math.round(Math.random() * (b - a) + a);
  12. },
  13. lerp: function (x, a, b) {
  14. return x * (b - a) + a;
  15. },
  16. clamp: function (x, a, b) {
  17. return Math.min(Math.max(x, a), b);
  18. },
  19. sat: function (x) {
  20. return Math.min(Math.max(x, 0.0), 1.0);
  21. },
  22. };
  23. })();