aboutsummaryrefslogtreecommitdiff
path: root/common/math.h
diff options
context:
space:
mode:
authorDavid Fioramonti2018-05-02 04:28:54 -0700
committerThierry Crozat2018-05-20 11:21:06 +0100
commitabb8ae09361d6a9e19383c21d65e085fa41c0d1e (patch)
treef9ffa3c71f26fdec679bad87cf393546e7583eb0 /common/math.h
parent5ee24f3970d7c2d5868e44dd1a2eece83e013cb7 (diff)
downloadscummvm-rg350-abb8ae09361d6a9e19383c21d65e085fa41c0d1e.tar.gz
scummvm-rg350-abb8ae09361d6a9e19383c21d65e085fa41c0d1e.tar.bz2
scummvm-rg350-abb8ae09361d6a9e19383c21d65e085fa41c0d1e.zip
COMMON: Template deg2rad and rad2deg in Common/math
The input and output type can be different. Currently, rad2deg is only being used by sci (float to int) and deg2rad in wintermute.
Diffstat (limited to 'common/math.h')
-rw-r--r--common/math.h42
1 files changed, 38 insertions, 4 deletions
diff --git a/common/math.h b/common/math.h
index ddb5c67dfe..a8e07686ff 100644
--- a/common/math.h
+++ b/common/math.h
@@ -107,12 +107,46 @@ inline int intLog2(uint32 v) {
}
#endif
-inline float rad2deg(float rad) {
- return rad * 180.0f / (float)M_PI;
+// Convert radians to degrees
+// Input and Output type can be different
+// Upconvert everything to floats
+template <class inputT, class outputT>
+inline outputT rad2deg(inputT rad) {
+ return (outputT)( (float)rad * (float)57.2957795130823); // 180.0/M_PI = 57.2957795130823
}
-inline float deg2rad(float deg) {
- return deg * (float)M_PI / 180.0f;
+// Handle the case differently when the input type is double
+template <class outputT>
+inline outputT rad2deg(double rad) {
+ return (outputT)( rad * 57.2957795130823);
+}
+
+// Convert radians to degrees
+// Input and Output type are the same
+template <class T>
+inline T rad2deg(T rad) {
+ return rad2deg<T,T>(rad);
+}
+
+// Convert degrees to radians
+// Input and Output type can be different
+// Upconvert everything to floats
+template <class inputT, class outputT>
+inline outputT deg2rad(inputT deg) {
+ return (outputT)( (float)deg * (float)0.0174532925199433); // M_PI/180.0 = 0.0174532925199433
+}
+
+// Handle the case differently when the input type is double
+template <class outputT>
+inline outputT deg2rad(double deg) {
+ return (outputT)( deg * 0.0174532925199433);
+}
+
+// Convert degrees to radians
+// Input and Output type are the same
+template <class T>
+inline T deg2rad(T deg) {
+ return deg2rad<T,T>(deg);
}
} // End of namespace Common