2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2007-08-22 07:52:33 +00:00
|
|
|
#ifndef BASE_MATH_H
|
|
|
|
#define BASE_MATH_H
|
|
|
|
|
2019-09-20 20:57:35 +00:00
|
|
|
#include <math.h>
|
2008-01-12 17:09:00 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
template <typename T>
|
2020-08-19 20:47:09 +00:00
|
|
|
constexpr inline T clamp(T val, T min, T max)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
2020-08-19 21:02:33 +00:00
|
|
|
return val < min ? min : (val > max ? max : val);
|
2007-08-22 07:52:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 20:47:09 +00:00
|
|
|
constexpr inline float sign(float f)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
return f<0.0f?-1.0f:1.0f;
|
|
|
|
}
|
|
|
|
|
2020-08-19 20:47:09 +00:00
|
|
|
constexpr inline int round_to_int(float f)
|
2008-11-16 22:07:46 +00:00
|
|
|
{
|
2020-08-19 21:02:33 +00:00
|
|
|
return f > 0 ? (int)(f + 0.5f) : (int)(f - 0.5f);
|
2008-11-16 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 20:47:09 +00:00
|
|
|
constexpr inline int round_truncate(float f)
|
2019-04-24 20:47:03 +00:00
|
|
|
{
|
|
|
|
return (int)f;
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:57:35 +00:00
|
|
|
inline int round_ceil(float f)
|
|
|
|
{
|
|
|
|
return (int)ceilf(f);
|
|
|
|
}
|
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
template<typename T, typename TB>
|
2020-08-19 20:47:09 +00:00
|
|
|
constexpr inline T mix(const T a, const T b, TB amount)
|
2007-08-22 07:52:33 +00:00
|
|
|
{
|
|
|
|
return a + (b-a)*amount;
|
|
|
|
}
|
2008-01-12 17:09:00 +00:00
|
|
|
|
|
|
|
inline float frandom() { return rand()/(float)(RAND_MAX); }
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
// float to fixed
|
2020-08-19 20:47:09 +00:00
|
|
|
constexpr inline int f2fx(float v) { return (int)(v*(float)(1<<10)); }
|
|
|
|
constexpr inline float fx2f(int v) { return v*(1.0f/(1<<10)); }
|
2008-02-02 12:38:36 +00:00
|
|
|
|
2020-08-19 21:02:33 +00:00
|
|
|
inline int gcd(int a, int b)
|
2011-06-01 17:27:36 +00:00
|
|
|
{
|
|
|
|
while(b != 0)
|
|
|
|
{
|
2011-06-02 15:38:36 +00:00
|
|
|
int c = a % b;
|
2011-06-01 17:27:36 +00:00
|
|
|
a = b;
|
|
|
|
b = c;
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2008-02-02 12:38:36 +00:00
|
|
|
class fxp
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
public:
|
|
|
|
void set(int v) { value = v; }
|
|
|
|
int get() const { return value; }
|
|
|
|
fxp &operator = (int v) { value = v<<10; return *this; }
|
|
|
|
fxp &operator = (float v) { value = (int)(v*(float)(1<<10)); return *this; }
|
|
|
|
operator float() const { return value/(float)(1<<10); }
|
|
|
|
};
|
|
|
|
|
2020-08-19 20:47:09 +00:00
|
|
|
constexpr float pi = 3.1415926535897932384626433f;
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2020-08-19 20:47:09 +00:00
|
|
|
template <typename T> constexpr inline T minimum(T a, T b) { return a<b?a:b; }
|
|
|
|
template <typename T> constexpr inline T minimum(T a, T b, T c) { return minimum(minimum(a, b), c); }
|
|
|
|
template <typename T> constexpr inline T maximum(T a, T b) { return a>b?a:b; }
|
|
|
|
template <typename T> constexpr inline T maximum(T a, T b, T c) { return maximum(maximum(a, b), c); }
|
|
|
|
template <typename T> constexpr inline T absolute(T a) { return a<T(0)?-a:a; }
|
2007-08-22 07:52:33 +00:00
|
|
|
|
2020-08-19 20:47:09 +00:00
|
|
|
template <typename T> constexpr inline T in_range(T a, T lower, T upper) { return lower <= a && a <= upper; }
|
|
|
|
template <typename T> constexpr inline T in_range(T a, T upper) { return in_range(a, 0, upper); }
|
2019-04-11 22:46:54 +00:00
|
|
|
|
2007-08-22 07:52:33 +00:00
|
|
|
#endif // BASE_MATH_H
|