Trigonometry.hpp
Go to the documentation of this file.
1 //
3 // Thor C++ Library
4 // Copyright (c) 2011-2015 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef THOR_TRIGONOMETRY_HPP
30 #define THOR_TRIGONOMETRY_HPP
31 
32 #include <Thor/Config.hpp>
33 
34 #include <cmath>
35 
36 
37 namespace thor
38 {
39 
42 
64 template <typename T>
66 {
67 };
68 
69 // Trigonometric traits: Specialization for float
70 template <>
71 struct TrigonometricTraits<float>
72 {
73  typedef float Type;
74 
75  static Type sin(Type deg) { return std::sin(degToRad(deg)); }
76  static Type cos(Type deg) { return std::cos(degToRad(deg)); }
77  static Type tan(Type deg) { return std::tan(degToRad(deg)); }
78  static Type arcSin(Type value) { return radToDeg(std::asin(value)); }
79  static Type arcCos(Type value) { return radToDeg(std::acos(value)); }
80  static Type arcTan2(Type valY, Type valX) { return radToDeg(std::atan2(valY, valX)); }
81  static Type sqrt(Type value) { return std::sqrt(value); }
82 
83  static Type pi() { return 3.141592653589793238462643383f; }
84  static Type radToDeg(Type rad) { return 180 / pi() * rad; }
85  static Type degToRad(Type deg) { return pi() / 180 * deg; }
86 };
87 
88 // Trigonometric traits: Specialization for double
89 template <>
90 struct TrigonometricTraits<double>
91 {
92  typedef double Type;
93 
94  static Type sin(Type deg) { return std::sin(degToRad(deg)); }
95  static Type cos(Type deg) { return std::cos(degToRad(deg)); }
96  static Type tan(Type deg) { return std::tan(degToRad(deg)); }
97  static Type arcSin(Type value) { return radToDeg(std::asin(value)); }
98  static Type arcCos(Type value) { return radToDeg(std::acos(value)); }
99  static Type arcTan2(Type valY, Type valX) { return radToDeg(std::atan2(valY, valX)); }
100  static Type sqrt(Type value) { return std::sqrt(value); }
101 
102  static Type pi() { return 3.141592653589793238462643383; }
103  static Type radToDeg(Type rad) { return 180 / pi() * rad; }
104  static Type degToRad(Type deg) { return pi() / 180 * deg; }
105 };
106 
107 // Trigonometric traits: Specialization for long double
108 template <>
109 struct TrigonometricTraits<long double>
110 {
111  typedef long double Type;
112 
113  static Type sin(Type deg) { return std::sin(degToRad(deg)); }
114  static Type cos(Type deg) { return std::cos(degToRad(deg)); }
115  static Type tan(Type deg) { return std::tan(degToRad(deg)); }
116  static Type arcSin(Type value) { return radToDeg(std::asin(value)); }
117  static Type arcCos(Type value) { return radToDeg(std::acos(value)); }
118  static Type arcTan2(Type valY, Type valX) { return radToDeg(std::atan2(valY, valX)); }
119  static Type sqrt(Type value) { return std::sqrt(value); }
120 
121  static Type pi() { return 3.141592653589793238462643383l; }
122  static Type radToDeg(Type rad) { return 180 / pi() * rad; }
123  static Type degToRad(Type deg) { return pi() / 180 * deg; }
124 };
125 
126 // ---------------------------------------------------------------------------------------------------------------------------
127 
128 
131 template <typename T>
132 T toDegree(T radian)
133 {
134  return TrigonometricTraits<T>::radToDeg(radian);
135 }
136 
139 template <typename T>
140 T toRadian(T degree)
141 {
142  return TrigonometricTraits<T>::degToRad(degree);
143 }
144 
147 extern const float THOR_API Pi;
148 
150 
151 } // namespace thor
152 
153 #endif // THOR_TRIGONOMETRY_HPP
Trigonometric traits template.
Definition: Trigonometry.hpp:65
const float Pi
The number Pi (3.1415...)
Definition: Animator.hpp:42
Configuration header of the library.
T toDegree(T radian)
Converts radians to degrees.
Definition: Trigonometry.hpp:132
T toRadian(T degree)
Converts degrees to radians.
Definition: Trigonometry.hpp:140