TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_math.hpp
1 
9 #ifndef TurtleBrains_Math_hpp
10 #define TurtleBrains_Math_hpp
11 
12 #include <turtle_brains/core/tb_configuration.hpp>
13 #include <turtle_brains/math/tb_constants.hpp>
14 
15 #include <cmath>
16 
17 namespace TurtleBrains
18 {
19  namespace Math
20  {
30  template<typename Type> bool IsEqual(const Type& leftValue, const Type& rightValue)
31  {
32  return leftValue == rightValue;
33  }
34 
35  inline bool IsEqual(const float leftValue, const float rightValue, const float tolerance = tbMath::kTolerance)
36  {
37  return fabsf(leftValue - rightValue) <= tolerance;
38  }
39 
40  inline bool IsEqual(const double leftValue, const double rightValue, const double tolerance = tbMath::kTolerance)
41  {
42  return fabs(leftValue - rightValue) <= tolerance;
43  }
44 
53  template<typename Type> bool IsZero(const Type& value)
54  {
55  return value == 0;
56  }
57 
58  inline bool IsZero(const float value, const float tolerance = tbMath::kTolerance)
59  {
60  return (fabsf(value)) <= tolerance;
61  }
62 
63  inline bool IsZero(const double value, const double tolerance = tbMath::kTolerance)
64  {
65  return (fabs(value)) <= tolerance;
66  }
67 
76  template <typename T> constexpr const T& Maximum(const T& leftValue, const T& rightValue) noexcept
77  {
78  return (leftValue < rightValue) ? rightValue : leftValue;
79  }
80 
89  template <typename T> constexpr const T& Minimum(const T& leftValue, const T& rightValue) noexcept
90  {
91  return (leftValue < rightValue) ? leftValue : rightValue;
92  }
93 
106  template <typename T> constexpr const T& Clamp(const T& value, const T& minimumValue, const T& maximumValue) noexcept
107  {
108  return (value < minimumValue) ? minimumValue : (maximumValue < value) ? maximumValue : value;
109  }
110 
111  }; /* namespace Math */
112 }; /* namespace TurtleBrains */
113 
114 namespace tbMath = TurtleBrains::Math;
115 
116 #endif /* TurtleBrains_Math_hpp */
Contains objects and functions for dealing with Vector and Matrix math.
bool IsZero(const Type &value)
Definition: tb_math.hpp:53
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
constexpr const T & Clamp(const T &value, const T &minimumValue, const T &maximumValue) noexcept
Definition: tb_math.hpp:106
bool IsEqual(const Type &leftValue, const Type &rightValue)
Definition: tb_math.hpp:30
constexpr const T & Maximum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.hpp:76
constexpr const T & Minimum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.hpp:89