Math.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREMATH_H
  28. #define ROCKETCOREMATH_H
  29. #include <Rocket/Core/Header.h>
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Math {
  33. // The constant PI / 2.
  34. extern ROCKETCORE_API const float PI_BY_TWO;
  35. // The constant PI.
  36. extern ROCKETCORE_API const float PI;
  37. // The constant 2 * PI / 3.
  38. extern ROCKETCORE_API const float TWO_PI_BY_THREE;
  39. // The constant 2 * PI.
  40. extern ROCKETCORE_API const float TWO_PI;
  41. template < typename Type >
  42. Type Max(Type a, Type b)
  43. {
  44. return (a > b) ? a : b;
  45. }
  46. template< typename Type >
  47. Type Min(Type a, Type b)
  48. {
  49. return (a < b) ? a : b;
  50. }
  51. template < typename Type >
  52. Type ClampLower(Type value, Type min)
  53. {
  54. return (value < min) ? min : value;
  55. }
  56. template < typename Type >
  57. Type ClampUpper(Type value, Type max)
  58. {
  59. return (value > max) ? max: value;
  60. }
  61. template< typename Type >
  62. Type Clamp(Type value, Type min, Type max)
  63. {
  64. return (value < min) ? min : (value > max) ? max : value;
  65. }
  66. /// Evaluates if a number is, or close to, zero.
  67. /// @param[in] value The number to compare to zero.
  68. /// @return True if the number if zero or close to it, false otherwise.
  69. ROCKETCORE_API bool IsZero(float value);
  70. /// Evaluates if two floating-point numbers are equal, or so similar that they could be considered
  71. /// so.
  72. /// @param[in] value_0 The first number to compare.
  73. /// @param[in] value_1 The second number to compare.
  74. /// @return True if the numbers are similar or equal.
  75. ROCKETCORE_API bool AreEqual(float value_0, float value_1);
  76. /// Calculates the absolute value of a number.
  77. /// @param[in] value The number of get the absolute value of.
  78. /// @return The absolute value of the number.
  79. ROCKETCORE_API float AbsoluteValue(float value);
  80. /// Calculates the cosine of an angle.
  81. /// @param[in] angle The angle to calculate the cosine of, in radians.
  82. /// @return The cosine of the angle.
  83. ROCKETCORE_API float Cos(float angle);
  84. /// Calculates the arc-cosine of an value.
  85. /// @param[in] angle The value to calculate the arc-cosine of.
  86. /// @return The angle, in radians.
  87. ROCKETCORE_API float ACos(float value);
  88. /// Calculates the sine of an angle.
  89. /// @param[in] angle The angle to calculate the sine of, in radians.
  90. /// @return The sine of the angle.
  91. ROCKETCORE_API float Sin(float angle);
  92. /// Calculates the arc-sine of an value.
  93. /// @param[in] angle The value to calculate the arc-sine of.
  94. /// @return The angle, in radians.
  95. ROCKETCORE_API float ASin(float angle);
  96. /// Calculates the tangent of an angle.
  97. /// @param[in] angle The angle to calculate the tangent of, in radians.
  98. /// @return The tanget of the angle.
  99. ROCKETCORE_API float Tan(float angle);
  100. /// Calculates the angle of a two-dimensional line.
  101. /// @param[in] y The y-component of the line.
  102. /// @param[in] x The x-component of the line.
  103. /// @return The angle of the line in radians.
  104. ROCKETCORE_API float ATan2(float y, float x);
  105. /// Converts an angle from radians to degrees.
  106. /// @param[in] The angle, in radians.
  107. /// @return The angle converted to degrees.
  108. ROCKETCORE_API float RadiansToDegrees(float angle);
  109. /// Converts an angle from degrees to radians.
  110. /// @param[in] The angle, in degrees.
  111. /// @return The angle converted to radians.
  112. ROCKETCORE_API float DegreesToRadians(float angle);
  113. /// Normalises and angle in radians
  114. /// @param[in] The angle, in randians
  115. /// @return The normalised angle
  116. ROCKETCORE_API float NormaliseAngle(float angle);
  117. /// Calculates the square root of a value.
  118. /// @param[in] value The value to calculate the square root of. This must be above zero.
  119. /// @return The square root of the value.
  120. ROCKETCORE_API float SquareRoot(float value);
  121. /// Rounds a floating-point value to the nearest integer.
  122. /// @param[in] value The value to round.
  123. /// @return The rounded integer.
  124. ROCKETCORE_API int Round(float value);
  125. /// Rounds a floating-point value up to the nearest integer.
  126. /// @param[in] value The value to round.
  127. /// @return The rounded integer.
  128. ROCKETCORE_API int RoundUp(float value);
  129. /// Rounds a floating-point value down to the nearest integer.
  130. /// @param[in] value The value to round.
  131. /// @return The rounded integer.
  132. ROCKETCORE_API int RoundDown(float value);
  133. /// Efficiently truncates a floating-point value into an integer.
  134. /// @param[in] value The value to truncate.
  135. /// @return The truncated value as a signed integer.
  136. ROCKETCORE_API int RealToInteger(float value);
  137. /// Converts a number to the nearest power of two, rounding up if necessary.
  138. /// @param[in] value The value to convert to a power-of-two.
  139. /// @return The smallest power of two that is as least as big as the input value.
  140. ROCKETCORE_API int ToPowerOfTwo(int value);
  141. /// Converts from the ASCII-representation of a hexadecimal digit to decimal.
  142. /// @param[in] hex_digit The hexadecimal digit to convert to decimal.
  143. /// @return The digit in decimal.
  144. ROCKETCORE_API int HexToDecimal(char hex_digit);
  145. /// Generates a random floating-point value between 0 and a user-specified value.
  146. /// @param[in] max_value The limit to random value. The generated value will be guaranteed to be below this limit.
  147. /// @return The random value.
  148. ROCKETCORE_API float RandomReal(float max_value);
  149. /// Generates a random integer value between 0 and a user-specified value.
  150. /// @param[in] max_value The limit to random value. The generated value will be guaranteed to be below this limit.
  151. /// @return The random value.
  152. ROCKETCORE_API int RandomInteger(int max_value);
  153. /// Generates a random boolean value, with equal chance of true or false.
  154. /// @return The random value.
  155. ROCKETCORE_API bool RandomBool();
  156. }
  157. }
  158. }
  159. #endif