about summary refs log tree commit diff
path: root/src/base/tl/math.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/base/tl/math.hpp')
-rw-r--r--src/base/tl/math.hpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/base/tl/math.hpp b/src/base/tl/math.hpp
new file mode 100644
index 00000000..b323dea8
--- /dev/null
+++ b/src/base/tl/math.hpp
@@ -0,0 +1,45 @@
+/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
+#ifndef BASE_MATH_H
+#define BASE_MATH_H
+
+#include <stdlib.h>
+
+template <typename T>
+inline T clamp(T val, T min, T max)
+{
+	if(val < min)
+		return min;
+	if(val > max)
+		return max;
+	return val;
+}
+
+inline float sign(float f)
+{
+	return f<0.0f?-1.0f:1.0f;
+}
+
+inline int round(float f)
+{
+	if(f > 0)
+		return (int)(f+0.5f);
+	return (int)(f-0.5f);
+}
+
+template<typename T, typename TB>
+inline T mix(const T a, const T b, TB amount)
+{
+	return a + (b-a)*amount;
+}
+
+inline float frandom() { return rand()/(float)(RAND_MAX); }
+
+const float pi = 3.1415926535897932384626433f;
+
+template <typename T> inline T min(T a, T b) { return a<b?a:b; }
+template <typename T> inline T max(T a, T b) { return a>b?a:b; }
+
+template <typename T> inline T min(T a, T b, T c) { return min(min(a,b),c); }
+template <typename T> inline T max(T a, T b, T c) { return max(max(a,b),c); }
+
+#endif // BASE_MATH_H