TurtleBrains  0.3.1
High quality, portable, C++ framework for rapid 2D game development.
tb_math.h
1 
9 #ifndef _TurtleBrains_Math_h_
10 #define _TurtleBrains_Math_h_
11 
12 #include "../core/tb_configuration.h"
13 #include "tb_constants.h"
14 
15 #include <cmath>
16 
17 namespace TurtleBrains
18 {
19  namespace Math
20  {
30  inline bool IsEqual(const float leftValue, const float rightValue, const float tolerance = tbMath::kTolerance)
31  {
32  return fabsf(leftValue - rightValue) <= tolerance;
33  }
34 
35 
36  inline bool IsEqual(const double leftValue, const double rightValue, const double tolerance = tbMath::kTolerance)
37  {
38  return fabs(leftValue - rightValue) <= tolerance;
39  }
40 
49  inline bool IsZero(const float value, const float tolerance = tbMath::kTolerance)
50  {
51  return (fabsf(value)) <= tolerance;
52  }
53 
54 
55  inline bool IsZero(const double value, const double tolerance = tbMath::kTolerance)
56  {
57  return (fabs(value)) <= tolerance;
58  }
59 
68  template <typename T> constexpr const T& Maximum(const T& leftValue, const T& rightValue) noexcept
69  {
70  return (leftValue < rightValue) ? rightValue : leftValue;
71  }
72 
81  template <typename T> constexpr const T& Minimum(const T& leftValue, const T& rightValue) noexcept
82  {
83  return (leftValue < rightValue) ? leftValue : rightValue;
84  }
85 
98  template <typename T> constexpr const T& Clamp(const T& value, const T& minimumValue, const T& maximumValue) noexcept
99  {
100  return (value < minimumValue) ? minimumValue : (maximumValue < value) ? maximumValue : value;
101  }
102 
103  }; /* namespace Math */
104 }; /* namespace TurtleBrains */
105 
106 namespace tbMath = TurtleBrains::Math;
107 
108 #endif /* _TurtleBrains_Math_h_ */
Contains objects and functions for dealing with Vector and Matrix math.
Here is some information about the primary namespace.
Definition: tb_application_dialog.h:21
constexpr const T & Clamp(const T &value, const T &minimumValue, const T &maximumValue) noexcept
Definition: tb_math.h:98
bool IsZero(const float value, const float tolerance=tbMath::kTolerance)
Definition: tb_math.h:49
constexpr const T & Maximum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.h:68
bool IsEqual(const float leftValue, const float rightValue, const float tolerance=tbMath::kTolerance)
Definition: tb_math.h:30
constexpr const T & Minimum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.h:81