math_helper.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Math helping functions
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <float.h>
  12. #include <math_helper.h>
  13. static double current_precision = FLT_EPSILON;
  14. void set_equal_precision(double v)
  15. {
  16. current_precision = v;
  17. }
  18. double getEpsilon()
  19. {
  20. return current_precision;
  21. }
  22. bool double_equal(double a, double b)
  23. {
  24. if (isinf(a) && isinf(b))
  25. return true;
  26. return fabs(a - b) < current_precision;
  27. }
  28. double deg_to_rad(double deg)
  29. {
  30. return deg * M_PI / 180.;
  31. }
  32. double min3(double a, double b, double c)
  33. {
  34. if (a <= b)
  35. {
  36. if (c < a) return c;
  37. return a;
  38. }
  39. if (b <= a)
  40. {
  41. if (c < b) return c;
  42. }
  43. return b;
  44. }
  45. double max3(double a, double b, double c)
  46. {
  47. if (a >= b)
  48. {
  49. if (c > a) return c;
  50. return a;
  51. }
  52. if (b >= a)
  53. {
  54. if (c > b) return c;
  55. }
  56. return b;
  57. }
  58. double frand()
  59. {
  60. return rand() / ((double) RAND_MAX);
  61. }
  62. double frandclip(double min, double max)
  63. {
  64. return (frand() * (max - min)) + min;
  65. }