Xonotic
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 crandom()   (2 * (random() - 0.5))
 Returns a random number between -1.0 and 1.0. More...
 
#define MEAN_ACCUMULATE(s, prefix, v, w)   mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)
 
#define MEAN_DECLARE(prefix, m)   float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
 
#define MEAN_EVALUATE(s, prefix)   mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)
 
#define power2of(e)   (2 ** 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 More...
 
ERASEABLE float float2range11 (float f)
 continuous function mapping all reals into -1..1 More...
 
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. More...
 
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. More...
 
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 More...
 
ERASEABLE vector vsnap (vector point, float fsize)
 

Macro Definition Documentation

◆ crandom

#define crandom ( )    (2 * (random() - 0.5))

Returns a random number between -1.0 and 1.0.

Definition at line 27 of file math.qh.

Referenced by ctf_Handle_Throw(), Item_ScheduleRespawn(), and ka_DropEvent().

◆ MEAN_ACCUMULATE

#define MEAN_ACCUMULATE (   s,
  prefix,
  v,
 
)    mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)

Definition at line 22 of file math.qh.

Referenced by anticheat_physics().

◆ MEAN_DECLARE

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

Definition at line 24 of file math.qh.

◆ MEAN_EVALUATE

#define MEAN_EVALUATE (   s,
  prefix 
)    mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)

Definition at line 23 of file math.qh.

◆ power2of

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

Definition at line 251 of file math.qh.

Function Documentation

◆ almost_equals()

ERASEABLE float almost_equals ( float  a,
float  b 
)

Definition at line 222 of file math.qh.

References ERASEABLE, and max().

Referenced by GetCurrentFov().

223 {
224  float eps = (max(a, -a) + max(b, -b)) * 0.001;
225  return a - b < eps && b - a < eps;
226 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ almost_equals_eps()

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

Definition at line 229 of file math.qh.

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

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

230 {
231  float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
232  return a - b < eps && b - a < eps;
233 }
const float FLOAT_EPSILON
Definition: float.qh:4
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ almost_in_bounds()

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

Definition at line 236 of file math.qh.

References ERASEABLE, max(), and median().

Referenced by Slider_draw(), Slider_keyDown(), Slider_mouseDrag(), and Slider_valueToText().

237 {
238  float eps = (max(a, -a) + max(c, -c)) * 0.001;
239  if (a > c) eps = -eps;
240  return b == median(a - eps, b, c + eps);
241 }
ERASEABLE float median(float a, float b, float c)
Definition: math.qh:216
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ angc()

ERASEABLE float angc ( float  a1,
float  a2 
)

Definition at line 38 of file math.qh.

References ERASEABLE.

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

◆ bezier_quadratic_getderivative()

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

Definition at line 122 of file math.qh.

References ERASEABLE.

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

◆ bezier_quadratic_getpoint()

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

Definition at line 114 of file math.qh.

References ERASEABLE.

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

◆ cubic_speedfunc()

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

Definition at line 129 of file math.qh.

References ERASEABLE.

Referenced by SUB_CalcMove_controller_think().

130 {
131  return (((startspeedfactor + endspeedfactor - 2
132  ) * spd - 2 * startspeedfactor - endspeedfactor + 3
133  ) * spd + startspeedfactor
134  ) * spd;
135 }
+ Here is the caller graph for this function:

◆ cubic_speedfunc_is_sane()

ERASEABLE bool cubic_speedfunc_is_sane ( float  startspeedfactor,
float  endspeedfactor 
)

Definition at line 138 of file math.qh.

References ERASEABLE.

Referenced by set_platmovetype().

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

◆ ExponentialFalloff()

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

Definition at line 244 of file math.qh.

References bound().

245 {
246  if (halflifedist > 0) return (0.5 ** ((bound(mindist, d, maxdist) - mindist) / halflifedist));
247  else if (halflifedist < 0) return (0.5 ** ((bound(mindist, d, maxdist) - maxdist) / halflifedist));
248  else return 1;
249 }
+ Here is the call graph for this function:

◆ float2range01()

ERASEABLE float float2range01 ( float  f)

continuous function mapping all reals into 0..1

Definition at line 210 of file math.qh.

References ERASEABLE, and float2range11().

211 {
212  return 0.5 + 0.5 * float2range11(f);
213 }
ERASEABLE float float2range11(float f)
continuous function mapping all reals into -1..1
Definition: math.qh:203
+ Here is the call graph for this function:

◆ float2range11()

ERASEABLE float float2range11 ( float  f)

continuous function mapping all reals into -1..1

Definition at line 203 of file math.qh.

References ERASEABLE, and fabs().

Referenced by float2range01(), and GetCurrentFov().

204 {
205  return f / (fabs(f) + 1);
206 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fsnap()

ERASEABLE float fsnap ( float  val,
float  fsize 
)

Definition at line 57 of file math.qh.

References ERASEABLE, and rint().

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

58 {
59  return rint(val / fsize) * fsize;
60 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lerp()

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

Definition at line 81 of file math.qh.

References ERASEABLE, and lerpratio().

82 {
83  return lerpratio(f0, f1, (t - t0) / (t1 - t0));
84 }
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1
Definition: all.inc:654
ERASEABLE float lerpratio(float f0, float f1, float ratio)
Definition: math.qh:75
+ Here is the call graph for this function:

◆ lerp3ratio()

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

Definition at line 87 of file math.qh.

References ERASEABLE, f1, and lerpratio().

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_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1
Definition: all.inc:654
ERASEABLE float lerpratio(float f0, float f1, float ratio)
Definition: math.qh:75
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 s2loc s1 s2 f1 f1points f2
Definition: all.inc:348
+ Here is the call graph for this function:

◆ lerpratio()

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

Definition at line 75 of file math.qh.

References ERASEABLE.

Referenced by lerp(), and lerp3ratio().

76 {
77  return f0 * (1 - ratio) + f1 * ratio;
78 }
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1
Definition: all.inc:654
+ Here is the caller graph for this function:

◆ lerpv()

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

Definition at line 108 of file math.qh.

References ERASEABLE.

Referenced by antilag_takebackorigin().

109 {
110  return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
111 }
+ Here is the caller graph for this function:

◆ lerpv3ratio()

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

Definition at line 101 of file math.qh.

References ERASEABLE, f1, and lerpvratio().

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 }
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1
Definition: all.inc:654
ERASEABLE vector lerpvratio(vector f0, vector f1, float ratio)
Definition: math.qh:95
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 s2loc s1 s2 f1 f1points f2
Definition: all.inc:348
+ Here is the call graph for this function:

◆ lerpvratio()

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

Definition at line 95 of file math.qh.

References ERASEABLE.

Referenced by lerpv3ratio().

96 {
97  return f0 * (1 - ratio) + f1 * ratio;
98 }
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1
Definition: all.inc:654
+ Here is the caller graph for this function:

◆ log2of()

ERASEABLE float log2of ( float  e)

Definition at line 254 of file math.qh.

References ERASEABLE.

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

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

References map_ranges().

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

375  {
376  if (value <= src_min) return dest_min;
377  if (value >= src_max) return dest_max;
378  return map_ranges(value, src_min, src_max, dest_min, dest_max);
379 }
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.
Definition: math.qh:366
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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

References ERASEABLE.

Referenced by map_bound_ranges().

366  {
367  float src_diff = src_max - src_min;
368  float dest_diff = dest_max - dest_min;
369  float ratio = (value - src_min) / src_diff;
370  return dest_min + dest_diff * ratio;
371 }
+ Here is the caller graph for this function:

◆ 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.

References ERASEABLE.

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 }

◆ 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 }

◆ median()

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

Definition at line 216 of file math.qh.

References bound(), and ERASEABLE.

Referenced by almost_in_bounds(), Slider_keyDown(), Slider_mouseDrag(), XonoticSliderCheckBox_configureXonoticSliderCheckBox(), XonoticSliderCheckBox_draw(), and XonoticSliderCheckBox_setChecked().

217 {
218  return (a < c) ? bound(a, b, c) : bound(c, b, a);
219 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ solve_quadratic()

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

ax^2 + bx + c = 0

Definition at line 307 of file math.qh.

References ERASEABLE, sqrt(), v, and vector().

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

308 {
309  vector v;
310  float D;
311  v = '0 0 0';
312  if (a == 0)
313  {
314  if (b != 0)
315  {
316  v.x = v.y = -c / b;
317  v.z = 1;
318  }
319  else
320  {
321  if (c == 0)
322  {
323  // actually, every number solves the equation!
324  v.z = 1;
325  }
326  }
327  }
328  else
329  {
330  D = b * b - 4 * a * c;
331  if (D >= 0)
332  {
333  D = sqrt(D);
334  if (a > 0) // put the smaller solution first
335  {
336  v.x = ((-b) - D) / (2 * a);
337  v.y = ((-b) + D) / (2 * a);
338  }
339  else
340  {
341  v.x = (-b + D) / (2 * a);
342  v.y = (-b - D) / (2 * a);
343  }
344  v.z = 1;
345  }
346  else
347  {
348  // complex solutions!
349  D = sqrt(-D);
350  v.x = -b / (2 * a);
351  if (a > 0) v.y = D / (2 * a);
352  else v.y = -D / (2 * a);
353  v.z = 0;
354  }
355  }
356  return v;
357 }
vector(float skel, float bonenum) _skel_get_boneabs_hidden
vector v
Definition: ent_cs.qc:116
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ vsnap()

ERASEABLE vector vsnap ( vector  point,
float  fsize 
)

Definition at line 63 of file math.qh.

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

64 {
65  vector vret;
66 
67  vret.x = rint(point.x / fsize) * fsize;
68  vret.y = rint(point.y / fsize) * fsize;
69  vret.z = ceil(point.z / fsize) * fsize;
70 
71  return vret;
72 }
vector(float skel, float bonenum) _skel_get_boneabs_hidden
+ Here is the call graph for this function: