TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_angle.hpp
1 
9 #ifndef TurtleBrains_Angle_hpp
10 #define TurtleBrains_Angle_hpp
11 
12 #include <turtle_brains/math/tb_constants.hpp>
13 #include <turtle_brains/math/tb_math.hpp>
14 
15 namespace TurtleBrains
16 {
17  namespace Math
18  {
19 
23  enum class AngleUnit
24  {
25  Degrees,
26  Radians
27  };
28 
34  template<typename Type> class AngleType
35  {
36  public:
40  inline static AngleType Degrees(const Type angleInDegrees)
41  {
42  return AngleType(angleInDegrees, AngleUnit::Degrees);
43  }
44 
48  inline static AngleType Radians(const Type angleInRadians)
49  {
50  return AngleType(angleInRadians, AngleUnit::Radians);
51  }
52 
54 
58  inline AngleType(void) :
59  mAngle(0)
60  {
61  }
62 
64 
71  inline explicit AngleType(const Type& angle, const AngleUnit units) :
72  mAngle((AngleUnit::Radians == units) ? angle : tbMath::Convert::DegreesToRadians(angle))
73  {
74  }
75 
79  inline Type AsRadians(void) const { return mAngle; }
80 
84  inline Type AsDegrees(void) const { return tbMath::Convert::RadiansToDegrees(mAngle); }
85 
90  bool operator==(const AngleType& other) const
91  {
92  return tbMath::IsEqual(mAngle, other.mAngle);
93  }
94 
99  bool operator!=(const AngleType& other) const
100  {
101  return !tbMath::IsEqual(mAngle, other.mAngle);
102  }
103 
105  bool operator<(const AngleType& other) const
106  {
107  return mAngle < other.mAngle;
108  }
109 
110  bool operator<=(const AngleType& other) const
111  {
112  return mAngle <= other.mAngle;
113  }
114 
115  bool operator>(const AngleType& other) const
116  {
117  return mAngle > other.mAngle;
118  }
119 
120  bool operator>=(const AngleType& other) const
121  {
122  return mAngle >= other.mAngle;
123  }
125 
129  AngleType operator-() const { return Radians(-mAngle); }
130 
131 
133  AngleType operator+(const AngleType& other) const { return Radians(mAngle + other.mAngle); }
134  AngleType& operator+=(const AngleType& other) { mAngle += other.mAngle; return *this; }
135  AngleType operator-(const AngleType& other) const { return Radians(mAngle - other.mAngle); }
136  AngleType& operator-=(const AngleType& other) { mAngle -= other.mAngle; return *this; }
137 
138  AngleType operator*(const Type& scalar) const { return Radians(mAngle * scalar); }
139  friend AngleType operator*(const Type& scalar, const AngleType& rhs) { return Radians(scalar * rhs.mAngle); }
140  AngleType& operator*=(const Type& scalar) { mAngle *= scalar; return *this; }
141 
142  AngleType operator/(const Type& scalar) const { return Radians(mAngle / scalar); }
143  //Left out the scalar / Angle because it doesn't seem like a useful construct. 5 / 4radians ... ???
144  AngleType& operator/=(const Type& scalar) { mAngle /= scalar; return *this; }
146 
147  private:
148  Type mAngle; //Stored as radians.
149  };
150 
155 
156  }; /* namespace Math */
157 }; /* namespace TurtleBrains */
158 
159 namespace tbMath = TurtleBrains::Math;
160 
161 //
162 // @note Visual Studio 2015 (at least) requires the literal operator to be either a 'long double' or 'const char*', this defeats the ability
163 // to use the templated versions above, and thus may lose some precision - but at least we get it working for floats?
164 //
165 //template<typename Type> tbMath::AngleType<Type> operator "" _radians (Type value) { return tbMath::AngleType<Type>(value, tbMath::AngleUnit::Radians); }
166 //template<typename Type> tbMath::AngleType<Type> operator "" _degrees (Type value) { return tbMath::AngleType<Type>(value, tbMath::AngleUnit::Degrees); }
167 //
168 
172 inline tbMath::Angle operator ""_radians(long double value) { return tbMath::Angle(static_cast<float>(value), tbMath::AngleUnit::Radians); }
173 
177 inline tbMath::Angle operator ""_degrees(long double value) { return tbMath::Angle(static_cast<float>(value), tbMath::AngleUnit::Degrees); }
178 
179 
180 #endif /* TurtleBrains_Angle_hpp */
AngleType< float > Angle
Definition: tb_angle.hpp:154
Type AsRadians(void) const
Definition: tb_angle.hpp:79
static AngleType Degrees(const Type angleInDegrees)
Definition: tb_angle.hpp:40
Contains objects and functions for dealing with Vector and Matrix math.
AngleUnit
Definition: tb_angle.hpp:23
AngleType(void)
Definition: tb_angle.hpp:58
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
bool operator!=(const AngleType &other) const
Definition: tb_angle.hpp:99
bool operator==(const AngleType &other) const
Definition: tb_angle.hpp:90
static AngleType Radians(const Type angleInRadians)
Definition: tb_angle.hpp:48
Specifies the angle input value is in units of Radians.
Specifies the angle input value is in units of Degrees.
Type AsDegrees(void) const
Definition: tb_angle.hpp:84
AngleType(const Type &angle, const AngleUnit units)
Definition: tb_angle.hpp:71
Definition: tb_angle.hpp:34
AngleType operator-() const
Definition: tb_angle.hpp:129