Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
math.qh File Reference
#include "lib/float.qh"
Include dependency graph for math.qh:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MEAN_ACCUMULATE(s, prefix, v, w)
#define MEAN_DECLARE(prefix, m)
#define MEAN_EVALUATE(s, prefix)
#define power2of(e)

Functions

ERASEABLE float almost_equals (float a, float b)
ERASEABLE float almost_equals_eps (float a, float b, float times_eps)
ERASEABLE float almost_in_bounds (float a, float b, float c)
ERASEABLE float angc (float a1, float a2)
ERASEABLE vector bezier_quadratic_getderivative (vector a, vector b, vector c, float t)
ERASEABLE vector bezier_quadratic_getpoint (vector a, vector b, vector c, float t)
ERASEABLE float cubic_speedfunc (float startspeedfactor, float endspeedfactor, float spd)
ERASEABLE bool cubic_speedfunc_is_sane (float startspeedfactor, float endspeedfactor)
ERASEABLE float ExponentialFalloff (float mindist, float maxdist, float halflifedist, float d)
ERASEABLE float float2range01 (float f)
 continuous function mapping all reals into 0..1
ERASEABLE float float2range11 (float f)
 continuous function mapping all reals into -1..1
ERASEABLE float fsnap (float val, float fsize)
ERASEABLE float lerp (float t0, float f0, float t1, float f1, float t)
ERASEABLE float lerp3ratio (float f0, float f1, float f2, float ratio)
ERASEABLE float lerpratio (float f0, float f1, float ratio)
ERASEABLE vector lerpv (float t0, vector v0, float t1, vector v1, float t)
ERASEABLE vector lerpv3ratio (vector f0, vector f1, vector f2, float ratio)
ERASEABLE vector lerpvratio (vector f0, vector f1, float ratio)
ERASEABLE float log2of (float e)
ERASEABLE float map_bound_ranges (float value, float src_min, float src_max, float dest_min, float dest_max)
 Same as map_ranges except that values outside the source range are clamped to min or max.
ERASEABLE float map_ranges (float value, float src_min, float src_max, float dest_min, float dest_max)
 Maps values between the src and dest range: src_min to dest_min, src_max to dest_max, values between them to the corresponding values between and extrapolates for values outside the range.
ERASEABLE void mean_accumulate (entity e,.float a,.float c, float mean, float value, float weight)
ERASEABLE float mean_evaluate (entity e,.float a,.float c, float mean)
ERASEABLE float median (float a, float b, float c)
ERASEABLE vector solve_quadratic (float a, float b, float c)
 ax^2 + bx + c = 0
ERASEABLE vector vsnap (vector point, float fsize)

Macro Definition Documentation

◆ MEAN_ACCUMULATE

#define MEAN_ACCUMULATE ( s,
prefix,
v,
w )
Value:
mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)
ERASEABLE void mean_accumulate(entity e,.float a,.float c, float mean, float value, float weight)
Definition math.qh:6

Definition at line 22 of file math.qh.

Referenced by anticheat_physics().

◆ MEAN_DECLARE

#define MEAN_DECLARE ( prefix,
m )
Value:
float prefix##_mean = m; .float prefix##_count, prefix##_accumulator

Definition at line 24 of file math.qh.

◆ MEAN_EVALUATE

#define MEAN_EVALUATE ( s,
prefix )
Value:
mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)
ERASEABLE float mean_evaluate(entity e,.float a,.float c, float mean)
Definition math.qh:15

Definition at line 23 of file math.qh.

◆ power2of

#define power2of ( e)
Value:
(2 ** e)

Definition at line 248 of file math.qh.

Function Documentation

◆ almost_equals()

ERASEABLE float almost_equals ( float a,
float b )

Definition at line 219 of file math.qh.

220{
221 float eps = (max(a, -a) + max(b, -b)) * 0.001;
222 return a - b < eps && b - a < eps;
223}
float max(float f,...)

References max().

Referenced by GetCurrentFov().

◆ almost_equals_eps()

ERASEABLE float almost_equals_eps ( float a,
float b,
float times_eps )

Definition at line 226 of file math.qh.

227{
228 float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
229 return a - b < eps && b - a < eps;
230}
const float FLOAT_EPSILON
Definition float.qh:4
float fabs(float f)

References fabs(), FLOAT_EPSILON, and max().

Referenced by DamageText::DamageText_update(), and MUTATOR_HOOKFUNCTION().

◆ almost_in_bounds()

ERASEABLE float almost_in_bounds ( float a,
float b,
float c )

Definition at line 233 of file math.qh.

234{
235 float eps = (max(a, -a) + max(c, -c)) * 0.001;
236 if (a > c) eps = -eps;
237 return b == median(a - eps, b, c + eps);
238}
ERASEABLE float median(float a, float b, float c)
Definition math.qh:213

References max(), and median().

Referenced by Slider::mousePress(), Slider_draw(), Slider_keyDown(), and Slider_valueToText().

◆ angc()

ERASEABLE float angc ( float a1,
float a2 )

Definition at line 35 of file math.qh.

36{
37 while (a1 > 180)
38 a1 -= 360;
39 while (a1 < -179)
40 a1 += 360;
41 while (a2 > 180)
42 a2 -= 360;
43 while (a2 < -179)
44 a2 += 360;
45 float a = a1 - a2;
46 while (a > 180)
47 a -= 360;
48 while (a < -179)
49 a += 360;
50 return a;
51}

◆ bezier_quadratic_getderivative()

ERASEABLE vector bezier_quadratic_getderivative ( vector a,
vector b,
vector c,
float t )

Definition at line 119 of file math.qh.

120{
121 return (c - 2 * b + a) * (2 * t)
122 + (b - a) * 2;
123}

References vector.

◆ bezier_quadratic_getpoint()

ERASEABLE vector bezier_quadratic_getpoint ( vector a,
vector b,
vector c,
float t )

Definition at line 111 of file math.qh.

112{
113 return (c - 2 * b + a) * (t * t)
114 + (b - a) * (2 * t)
115 + a;
116}

References vector.

Referenced by Draw_ArcBeam(), and W_Arc_Beam_Think().

◆ cubic_speedfunc()

ERASEABLE float cubic_speedfunc ( float startspeedfactor,
float endspeedfactor,
float spd )

Definition at line 126 of file math.qh.

127{
128 return (((startspeedfactor + endspeedfactor - 2
129 ) * spd - 2 * startspeedfactor - endspeedfactor + 3
130 ) * spd + startspeedfactor
131 ) * spd;
132}

Referenced by SUB_CalcMove_controller_think().

◆ cubic_speedfunc_is_sane()

ERASEABLE bool cubic_speedfunc_is_sane ( float startspeedfactor,
float endspeedfactor )

Definition at line 135 of file math.qh.

136{
137 if (startspeedfactor < 0 || endspeedfactor < 0) return false;
138
139 /*
140 // if this is the case, the possible zeros of the first derivative are outside
141 // 0..1
142 We can calculate this condition as condition
143 if(se <= 3)
144 return true;
145 */
146
147 // better, see below:
148 if (startspeedfactor <= 3 && endspeedfactor <= 3) return true;
149
150 // if this is the case, the first derivative has no zeros at all
151 float se = startspeedfactor + endspeedfactor;
152 float s_e = startspeedfactor - endspeedfactor;
153 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
154 return true;
155
156 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
157 // we also get s_e <= 6 - se
158 // 3 * (se - 4)^2 + (6 - se)^2
159 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
160 // Therefore, above "better" check works!
161
162 return false;
163
164 // known good cases:
165 // (0, [0..3])
166 // (0.5, [0..3.8])
167 // (1, [0..4])
168 // (1.5, [0..3.9])
169 // (2, [0..3.7])
170 // (2.5, [0..3.4])
171 // (3, [0..3])
172 // (3.5, [0.2..2.3])
173 // (4, 1)
174
175 /*
176 On another note:
177 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
178
179 s + e - 2 == 0: no inflection
180
181 s + e > 2:
182 0 < inflection < 1 if:
183 0 < 2s + e - 3 < 3s + 3e - 6
184 2s + e > 3 and 2e + s > 3
185
186 s + e < 2:
187 0 < inflection < 1 if:
188 0 > 2s + e - 3 > 3s + 3e - 6
189 2s + e < 3 and 2e + s < 3
190
191 Therefore: there is an inflection point iff:
192 e outside (3 - s)/2 .. 3 - s*2
193
194 in other words, if (s,e) in triangle (1,1)(0,3)(0,1.5) or in triangle (1,1)(3,0)(1.5,0)
195 */
196}

Referenced by set_platmovetype().

◆ ExponentialFalloff()

ERASEABLE float ExponentialFalloff ( float mindist,
float maxdist,
float halflifedist,
float d )

Definition at line 241 of file math.qh.

242{
243 if (halflifedist > 0) return (0.5 ** ((bound(mindist, d, maxdist) - mindist) / halflifedist));
244 else if (halflifedist < 0) return (0.5 ** ((bound(mindist, d, maxdist) - maxdist) / halflifedist));
245 else return 1;
246}
float bound(float min, float value, float max)

References bound().

Referenced by fireBullet_falloff(), and W_Arc_Beam_Think().

◆ float2range01()

ERASEABLE float float2range01 ( float f)

continuous function mapping all reals into 0..1

Definition at line 207 of file math.qh.

208{
209 return 0.5 + 0.5 * float2range11(f);
210}
ERASEABLE float float2range11(float f)
continuous function mapping all reals into -1..1
Definition math.qh:200

References float2range11().

◆ float2range11()

ERASEABLE float float2range11 ( float f)

continuous function mapping all reals into -1..1

Definition at line 200 of file math.qh.

201{
202 return f / (fabs(f) + 1);
203}

References fabs().

Referenced by float2range01(), and GetCurrentFov().

◆ fsnap()

ERASEABLE float fsnap ( float val,
float fsize )

Definition at line 54 of file math.qh.

55{
56 return rint(val / fsize) * fsize;
57}
float rint(float f)

References rint().

Referenced by pathlib_astar(), pathlib_flynode(), pathlib_nodeatpoint(), pathlib_swimnode(), pathlib_walknode(), and pathlib_wateroutnode().

◆ lerp()

ERASEABLE float lerp ( float t0,
float f0,
float t1,
float f1,
float t )

Definition at line 78 of file math.qh.

79{
80 return lerpratio(f0, f1, (t - t0) / (t1 - t0));
81}
ERASEABLE float lerpratio(float f0, float f1, float ratio)
Definition math.qh:72
f1
Definition all.inc:561

References f1, and lerpratio().

◆ lerp3ratio()

ERASEABLE float lerp3ratio ( float f0,
float f1,
float f2,
float ratio )

Definition at line 84 of file math.qh.

85{
86 float mid = 0.5;
87 return ratio < mid ? lerpratio(f0, f1, ratio / mid) : ratio > mid ? lerpratio(f1, f2, (ratio - mid) / mid) : f1;
88}
spree_inf s1 s2 s3loc s2 spree_inf s1 s2 s3loc s2 spree_inf s1 s2 s3loc s2 s1 s2loc s1 s2loc s1 s2loc s1 s2loc s1 s2loc s1 s2loc s1 s2 f1points f2
Definition all.inc:364

References f1, f2, and lerpratio().

◆ lerpratio()

ERASEABLE float lerpratio ( float f0,
float f1,
float ratio )

Definition at line 72 of file math.qh.

73{
74 return f0 * (1 - ratio) + f1 * ratio;
75}

References f1.

Referenced by lerp(), and lerp3ratio().

◆ lerpv()

ERASEABLE vector lerpv ( float t0,
vector v0,
float t1,
vector v1,
float t )

Definition at line 105 of file math.qh.

106{
107 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
108}

References vector.

Referenced by antilag_takebackorigin().

◆ lerpv3ratio()

ERASEABLE vector lerpv3ratio ( vector f0,
vector f1,
vector f2,
float ratio )

Definition at line 98 of file math.qh.

99{
100 float mid = 0.5;
101 return ratio < mid ? lerpvratio(f0, f1, ratio / mid) : ratio > mid ? lerpvratio(f1, f2, (ratio - mid) / mid) : f1;
102}
ERASEABLE vector lerpvratio(vector f0, vector f1, float ratio)
Definition math.qh:92

References f1, f2, lerpvratio(), and vector.

◆ lerpvratio()

ERASEABLE vector lerpvratio ( vector f0,
vector f1,
float ratio )

Definition at line 92 of file math.qh.

93{
94 return f0 * (1 - ratio) + f1 * ratio;
95}

References f1, and vector.

Referenced by lerpv3ratio().

◆ log2of()

ERASEABLE float log2of ( float e)

Definition at line 251 of file math.qh.

252{
253 // NOTE: generated code
254 if (e > 2048)
255 if (e > 131072)
256 if (e > 1048576)
257 if (e > 4194304) return 23;
258 else
259 if (e > 2097152) return 22;
260 else return 21;
261 else
262 if (e > 524288) return 20;
263 else
264 if (e > 262144) return 19;
265 else return 18;
266 else
267 if (e > 16384)
268 if (e > 65536) return 17;
269 else
270 if (e > 32768) return 16;
271 else return 15;
272 else
273 if (e > 8192) return 14;
274 else
275 if (e > 4096) return 13;
276 else return 12;
277 else
278 if (e > 32)
279 if (e > 256)
280 if (e > 1024) return 11;
281 else
282 if (e > 512) return 10;
283 else return 9;
284 else
285 if (e > 128) return 8;
286 else
287 if (e > 64) return 7;
288 else return 6;
289 else
290 if (e > 4)
291 if (e > 16) return 5;
292 else
293 if (e > 8) return 4;
294 else return 3;
295 else
296 if (e > 2) return 2;
297 else
298 if (e > 1) return 1;
299 else return 0;
300}

◆ map_bound_ranges()

ERASEABLE float map_bound_ranges ( float value,
float src_min,
float src_max,
float dest_min,
float dest_max )

Same as map_ranges except that values outside the source range are clamped to min or max.

Definition at line 372 of file math.qh.

372 {
373 if (value <= src_min) return dest_min;
374 if (value >= src_max) return dest_max;
375 return map_ranges(value, src_min, src_max, dest_min, dest_max);
376}
ERASEABLE float map_ranges(float value, float src_min, float src_max, float dest_min, float dest_max)
Maps values between the src and dest range: src_min to dest_min, src_max to dest_max,...
Definition math.qh:363

References map_ranges().

Referenced by bobmodel_ofs(), DamageText::DamageText_update(), determine_force(), Handicap_UpdateHandicapLevel(), and HUD_Panel_HlCenterLine().

◆ map_ranges()

ERASEABLE float map_ranges ( float value,
float src_min,
float src_max,
float dest_min,
float dest_max )

Maps values between the src and dest range: src_min to dest_min, src_max to dest_max, values between them to the corresponding values between and extrapolates for values outside the range.

src_min and src_max must not be the same or division by zero occurs.

dest_max can be smaller than dest_min if you want the resulting range to be inverted, all values can be negative.

Definition at line 363 of file math.qh.

363 {
364 float src_diff = src_max - src_min;
365 float dest_diff = dest_max - dest_min;
366 float ratio = (value - src_min) / src_diff;
367 return dest_min + dest_diff * ratio;
368}

Referenced by map_bound_ranges().

◆ mean_accumulate()

ERASEABLE void mean_accumulate ( entity e,
.float a,
.float c,
float mean,
float value,
float weight )

Definition at line 6 of file math.qh.

7{
8 if (weight == 0) return;
9 if (mean == 0) e.(a) *= (value ** weight);
10 else e.(a) += (value ** mean) * weight;
11 e.(c) += weight;
12}

References entity().

◆ mean_evaluate()

ERASEABLE float mean_evaluate ( entity e,
.float a,
.float c,
float mean )

Definition at line 15 of file math.qh.

16{
17 if (e.(c) == 0) return 0;
18 if (mean == 0) return (e.(a) ** (1.0 / e.(c)));
19 else return ((e.(a) / e.(c)) ** (1.0 / mean));
20}

References entity().

◆ median()

ERASEABLE float median ( float a,
float b,
float c )

◆ solve_quadratic()

ERASEABLE vector solve_quadratic ( float a,
float b,
float c )

ax^2 + bx + c = 0

Definition at line 304 of file math.qh.

305{
306 vector v;
307 float D;
308 v = '0 0 0';
309 if (a == 0)
310 {
311 if (b != 0)
312 {
313 v.x = v.y = -c / b;
314 v.z = 1;
315 }
316 else
317 {
318 if (c == 0)
319 {
320 // actually, every number solves the equation!
321 v.z = 1;
322 }
323 }
324 }
325 else
326 {
327 D = b * b - 4 * a * c;
328 if (D >= 0)
329 {
330 D = sqrt(D);
331 if (a > 0) // put the smaller solution first
332 {
333 v.x = ((-b) - D) / (2 * a);
334 v.y = ((-b) + D) / (2 * a);
335 }
336 else
337 {
338 v.x = (-b + D) / (2 * a);
339 v.y = (-b - D) / (2 * a);
340 }
341 v.z = 1;
342 }
343 else
344 {
345 // complex solutions!
346 D = sqrt(-D);
347 v.x = -b / (2 * a);
348 if (a > 0) v.y = D / (2 * a);
349 else v.y = -D / (2 * a);
350 v.z = 0;
351 }
352 }
353 return v;
354}
float sqrt(float f)
vector
Definition self.qh:92

References sqrt(), and vector.

Referenced by get_shotvelocity(), solve_shotdirection(), spiberbot_calcartillery(), StartFrame(), trigger_push_calculatevelocity(), and W_Devastator_SteerTo().

◆ vsnap()

ERASEABLE vector vsnap ( vector point,
float fsize )

Definition at line 60 of file math.qh.

61{
62 vector vret;
63
64 vret.x = rint(point.x / fsize) * fsize;
65 vret.y = rint(point.y / fsize) * fsize;
66 vret.z = ceil(point.z / fsize) * fsize;
67
68 return vret;
69}
float ceil(float f)

References ceil(), rint(), and vector.