blob: 1728870beb67c71992425e10333aae67c4867d9a (
plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/* (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. */
#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); }
// float to fixed
inline int f2fx(float v) { return (int)(v*(float)(1<<10)); }
inline float fx2f(int v) { return v*(1.0f/(1<<10)); }
inline int gcd(int a, int b)
{
while(b != 0)
{
int c = a % b;
a = b;
b = c;
}
return a;
}
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); }
};
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 absolute(T a) { return a<T(0)?-a:a; }
inline int lcm(int a, int b)
{
if(a == 0 || b == 0)
return -1;
return absolute(a * (b / gcd(a, b)));
}
#endif // BASE_MATH_H
|