-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math.hpp
53 lines (46 loc) · 1.05 KB
/
Math.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* FILENAME: Math.hpp
* AUTHOR: Josh Trzebiatowski <[email protected]>
* DATE: April 10, 2015
*/
#ifndef MATH_HPP
#define MATH_HPP
/*
* Math namespace, contains templated functions for simple math operations
*/
namespace Math {
/*
* Return the maximum of two values
*/
template <typename T> T max(T a, T b) {
return a > b ? a : b;
}
/*
* Return the minimum of two values
*/
template <typename T> T min(T a, T b) {
return a < b ? a : b;
}
/*
* Clamp a value to a range
*/
template <typename T> T clamp(T val, T min, T max) {
return Math::max(min, Math::min(val, max));
}
/*
* Check if a value is in a range
*/
template <typename T> bool inRange(T val, T min, T max) {
return (val >= min) && (val <= max);
}
/*
* Linearly scale a value from an input range to an output range with translation
*/
template <typename T> T scale(T value, T in_min, T in_max, T out_min, T out_max) {
T rin = in_max - in_min;
T rout = out_max - out_min;
T slope = rout/rin;
return (value-in_min) * slope + out_min;
}
}
#endif