CMU462 Library  1.0
15-462/15-662: Computer Graphics (Fall 2015)
misc_math.h
1 #ifndef CMU462_MISCMATH_H
2 #define CMU462_MISCMATH_H
3 
4 #include <cmath>
5 #include <limits>
6 #include <utility>
7 
8 #define PI (3.14159265358979323)
9 #define EPS_D (0.00000000001)
10 #define EPS_F (0.00001f)
11 #define INF_D (std::numeric_limits<double>::infinity())
12 #define INF_F (std::numeric_limits<float>::infinity())
13 
14 namespace CMU462 {
15 
16 /*
17  Takes any kind of number and converts from degrees to radians.
18 */
19 template<typename T>
20 inline T radians(T deg) {
21  return deg * (PI / 180);
22 }
23 
24 /*
25  Takes any kind of number and converts from radians to degrees.
26 */
27 template<typename T>
28 inline T degrees(T rad) {
29  return rad * (180 / PI);
30 }
31 
32 /*
33  Takes any kind of number, as well as a lower and upper bound, and clamps the
34  number to be within the bound.
35  NOTE: x, lo, and hi must all be the same type or compilation will fail. A
36  common mistake is to pass an int for x and size_ts for lo and hi.
37 */
38 template<typename T>
39 inline T clamp(T x, T lo, T hi) {
40  return std::min(std::max(x, lo), hi);
41 }
42 
43 } // namespace CMU462
44 
45 #endif // CMU462_MISCMATH_H
Definition: color.h:6