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 27 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 29 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:18

Definition at line 28 of file math.qh.

◆ power2of

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

Definition at line 256 of file math.qh.

Function Documentation

◆ almost_equals()

ERASEABLE float almost_equals ( float a,
float b )

Definition at line 224 of file math.qh.

225{
226 float eps = (max(a, -a) + max(b, -b)) * 0.001;
227 return a - b < eps && b - a < eps;
228}
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 231 of file math.qh.

232{
233 float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
234 return a - b < eps && b - a < eps;
235}
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 238 of file math.qh.

239{
240 float eps = (max(a, -a) + max(c, -c)) * 0.001;
241 if (a > c)
242 eps = -eps;
243 return b == median(a - eps, b, c + eps);
244}
ERASEABLE float median(float a, float b, float c)
Definition math.qh:218

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 40 of file math.qh.

41{
42 while (a1 > 180)
43 a1 -= 360;
44 while (a1 < -179)
45 a1 += 360;
46 while (a2 > 180)
47 a2 -= 360;
48 while (a2 < -179)
49 a2 += 360;
50 float a = a1 - a2;
51 while (a > 180)
52 a -= 360;
53 while (a < -179)
54 a += 360;
55 return a;
56}

◆ bezier_quadratic_getderivative()

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

Definition at line 122 of file math.qh.

123{
124 return (c - 2 * b + a) * (2 * t)
125 + (b - a) * 2;
126}

References vector.

◆ bezier_quadratic_getpoint()

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

Definition at line 114 of file math.qh.

115{
116 return (c - 2 * b + a) * (t * t)
117 + (b - a) * (2 * t)
118 + a;
119}

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 129 of file math.qh.

130{
131 return (((startspeedfactor + endspeedfactor - 2
132 ) * spd - 2 * startspeedfactor - endspeedfactor + 3
133 ) * spd + startspeedfactor
134 ) * spd;
135}

Referenced by SUB_CalcMove_controller_think().

◆ cubic_speedfunc_is_sane()

ERASEABLE bool cubic_speedfunc_is_sane ( float startspeedfactor,
float endspeedfactor )

Definition at line 138 of file math.qh.

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

Referenced by set_platmovetype().

◆ ExponentialFalloff()

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

Definition at line 247 of file math.qh.

248{
249 if (halflifedist > 0)
250 return (0.5 ** ((bound(mindist, d, maxdist) - mindist) / halflifedist));
251 else if (halflifedist < 0)
252 return (0.5 ** ((bound(mindist, d, maxdist) - maxdist) / halflifedist));
253 return 1;
254}
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 212 of file math.qh.

213{
214 return 0.5 + 0.5 * float2range11(f);
215}
ERASEABLE float float2range11(float f)
Continuous function mapping all reals into -1..1.
Definition math.qh:205

References float2range11().

◆ float2range11()

ERASEABLE float float2range11 ( float f)

Continuous function mapping all reals into -1..1.

Definition at line 205 of file math.qh.

206{
207 return f / (fabs(f) + 1);
208}

References fabs().

Referenced by float2range01(), and GetCurrentFov().

◆ fsnap()

ERASEABLE float fsnap ( float val,
float fsize )

Definition at line 59 of file math.qh.

60{
61 return rint(val / fsize) * fsize;
62}
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 81 of file math.qh.

82{
83 return lerpratio(f0, f1, (t - t0) / (t1 - t0));
84}
ERASEABLE float lerpratio(float f0, float f1, float ratio)
Definition math.qh:75
f1
Definition all.inc:566

References f1, and lerpratio().

◆ lerp3ratio()

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

Definition at line 87 of file math.qh.

88{
89 float mid = 0.5;
90 return ratio < mid ? lerpratio(f0, f1, ratio / mid) : ratio > mid ? lerpratio(f1, f2, (ratio - mid) / mid) : f1;
91}
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:369

References f1, f2, and lerpratio().

◆ lerpratio()

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

Definition at line 75 of file math.qh.

76{
77 return f0 * (1 - ratio) + f1 * ratio;
78}

References f1.

Referenced by lerp(), and lerp3ratio().

◆ lerpv()

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

Definition at line 108 of file math.qh.

109{
110 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
111}

References vector.

Referenced by antilag_takebackorigin().

◆ lerpv3ratio()

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

Definition at line 101 of file math.qh.

102{
103 float mid = 0.5;
104 return ratio < mid ? lerpvratio(f0, f1, ratio / mid) : ratio > mid ? lerpvratio(f1, f2, (ratio - mid) / mid) : f1;
105}
ERASEABLE vector lerpvratio(vector f0, vector f1, float ratio)
Definition math.qh:95

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

◆ lerpvratio()

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

Definition at line 95 of file math.qh.

96{
97 return f0 * (1 - ratio) + f1 * ratio;
98}

References f1, and vector.

Referenced by lerpv3ratio().

◆ log2of()

ERASEABLE float log2of ( float e)

Definition at line 259 of file math.qh.

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

◆ 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 377 of file math.qh.

378{
379 if (value <= src_min) return dest_min;
380 if (value >= src_max) return dest_max;
381 return map_ranges(value, src_min, src_max, dest_min, dest_max);
382}
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:367

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 367 of file math.qh.

368{
369 float src_diff = src_max - src_min;
370 float dest_diff = dest_max - dest_min;
371 float ratio = (value - src_min) / src_diff;
372 return dest_min + dest_diff * ratio;
373}

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)
9 return;
10 if (mean == 0)
11 e.(a) *= (value ** weight);
12 else
13 e.(a) += (value ** mean) * weight;
14 e.(c) += weight;
15}

References entity().

◆ mean_evaluate()

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

Definition at line 18 of file math.qh.

19{
20 if (e.(c) == 0)
21 return 0;
22 if (mean == 0)
23 return (e.(a) ** (1.0 / e.(c)));
24 return ((e.(a) / e.(c)) ** (1.0 / mean));
25}

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 312 of file math.qh.

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

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 65 of file math.qh.

66{
67 vector vret;
68 vret.x = rint(point.x / fsize) * fsize;
69 vret.y = rint(point.y / fsize) * fsize;
70 vret.z = ceil(point.z / fsize) * fsize;
71 return vret;
72}
float ceil(float f)

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