]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/math.qh
Prefer lowercase filenames
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / math.qh
1 #ifndef MATH_H
2 #define MATH_H
3
4 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
5 {
6         if (weight == 0)
7                 return;
8         if (mean == 0)
9                 e.(a) *= pow(value, weight);
10         else
11                 e.(a) += pow(value, mean) * weight;
12         e.(c) += weight;
13 }
14
15 float mean_evaluate(entity e, .float a, .float c, float mean)
16 {
17         if (e.(c) == 0)
18                 return 0;
19         if (mean == 0)
20                 return pow(e.(a), 1.0 / e.(c));
21         else
22                 return pow(e.(a) / e.(c), 1.0 / mean);
23 }
24
25 #define MEAN_ACCUMULATE(prefix,v,w) mean_accumulate(self,prefix##_accumulator,prefix##_count,prefix##_mean,v,w)
26 #define MEAN_EVALUATE(prefix) mean_evaluate(self,prefix##_accumulator,prefix##_count,prefix##_mean)
27 #define MEAN_DECLARE(prefix,m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
28
29 /*
30 ==================
31 crandom
32
33 Returns a random number between -1.0 and 1.0
34 ==================
35 */
36 float crandom (void)
37 {
38         return 2 * (random () - 0.5);
39 }
40
41
42 /*
43 ==================
44 Angc used for animations
45 ==================
46 */
47
48
49 float angc (float a1, float a2)
50 {
51         float   a;
52
53         while (a1 > 180)
54                 a1 = a1 - 360;
55         while (a1 < -179)
56                 a1 = a1 + 360;
57
58         while (a2 > 180)
59                 a2 = a2 - 360;
60         while (a2 < -179)
61                 a2 = a2 + 360;
62
63         a = a1 - a2;
64         while (a > 180)
65                 a = a - 360;
66         while (a < -179)
67                 a = a + 360;
68
69         return a;
70 }
71
72 float fsnap(float val,float fsize)
73 {
74     return rint(val / fsize) * fsize;
75 }
76
77 vector vsnap(vector point,float fsize)
78 {
79     vector vret;
80
81     vret.x = rint(point.x / fsize) * fsize;
82     vret.y = rint(point.y / fsize) * fsize;
83     vret.z = ceil(point.z / fsize) * fsize;
84
85     return vret;
86 }
87
88 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
89 {
90         return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
91 }
92
93 #endif