math.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_MATH_H
  21. #define LOVE_MATH_H
  22. #include <climits> // for CHAR_BIT
  23. #include <cstdlib> // for rand() and RAND_MAX
  24. /* Definitions of useful mathematical constants
  25. * M_E - e
  26. * M_LOG2E - log2(e)
  27. * M_LOG10E - log10(e)
  28. * M_LN2 - ln(2)
  29. * M_LN10 - ln(10)
  30. * M_PI - pi
  31. * M_PI_2 - pi/2
  32. * M_PI_4 - pi/4
  33. * M_1_PI - 1/pi
  34. * M_2_PI - 2/pi
  35. * M_2_SQRTPI - 2/sqrt(pi)
  36. * M_SQRT2 - sqrt(2)
  37. * M_SQRT1_2 - 1/sqrt(2)
  38. */
  39. #define LOVE_M_E 2.71828182845904523536
  40. #define LOVE_M_LOG2E 1.44269504088896340736
  41. #define LOVE_M_LOG10E 0.434294481903251827651
  42. #define LOVE_M_LN2 0.693147180559945309417
  43. #define LOVE_M_LN10 2.30258509299404568402
  44. #define LOVE_M_PI 3.14159265358979323846
  45. #define LOVE_M_PI_2 1.57079632679489661923
  46. #define LOVE_M_PI_4 0.785398163397448309616
  47. #define LOVE_M_1_PI 0.318309886183790671538
  48. #define LOVE_M_2_PI 0.636619772367581343076
  49. #define LOVE_M_2_SQRTPI 1.12837916709551257390
  50. #define LOVE_M_SQRT2 1.41421356237309504880
  51. #define LOVE_M_SQRT1_2 0.707106781186547524401
  52. #define LOVE_M_TORAD (float)(LOVE_M_PI/180.0)
  53. #define LOVE_M_TODEG (float)(180.0/LOVE_M_PI)
  54. #define LOVE_TORAD(x) (float)(x*LOVE_M_TORAD)
  55. #define LOVE_TODEG(x) (float)(x*LOVE_M_TODEG)
  56. namespace love
  57. {
  58. struct Rect
  59. {
  60. int x, y;
  61. int w, h;
  62. bool operator == (const Rect &rhs) const
  63. {
  64. return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h;
  65. }
  66. };
  67. inline int nextP2(int x)
  68. {
  69. x += (x == 0);
  70. x--;
  71. for (unsigned int i = 1; i < sizeof(int)*CHAR_BIT; i <<= 1) x |= x >> i;
  72. return ++x;
  73. }
  74. inline float nextP2(float x)
  75. {
  76. return (float) nextP2((int) x);
  77. }
  78. } // love
  79. #endif // LOVE_MATH_H