]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/vector.qh
Uncrustify lib/*
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / vector.qh
1 #ifndef VECTOR_H
2 #define VECTOR_H
3
4 const vector eX = '1 0 0';
5 const vector eY = '0 1 0';
6 const vector eZ = '0 0 1';
7
8 vector randompos(vector m1, vector m2)
9 {
10         vector v;
11         m2 = m2 - m1;
12         v_x = m2_x * random() + m1_x;
13         v_y = m2_y * random() + m1_y;
14         v_z = m2_z * random() + m1_z;
15         return v;
16 }
17
18 float vlen2d(vector v)
19 {
20         return sqrt(v.x * v.x + v.y * v.y);
21 }
22
23 float vlen_maxnorm2d(vector v)
24 {
25         return max(v.x, v.y, -v.x, -v.y);
26 }
27
28 float vlen_minnorm2d(vector v)
29 {
30         return min(max(v.x, -v.x), max(v.y, -v.y));
31 }
32
33 float dist_point_line(vector p, vector l0, vector ldir)
34 {
35         ldir = normalize(ldir);
36
37         // remove the component in line direction
38         p = p - (p * ldir) * ldir;
39
40         // vlen of the remaining vector
41         return vlen(p);
42 }
43
44 /** requires that m2>m1 in all coordinates, and that m4>m3 */
45 float boxesoverlap(vector m1, vector m2, vector m3, vector m4) { return m2_x >= m3_x && m1_x <= m4_x && m2_y >= m3_y && m1_y <= m4_y && m2_z >= m3_z && m1_z <= m4_z; }
46
47 /** requires the same as boxesoverlap, but is a stronger condition */
48 float boxinsidebox(vector smins, vector smaxs, vector bmins, vector bmaxs) { return smins.x >= bmins.x && smaxs.x <= bmaxs.x && smins.y >= bmins.y && smaxs.y <= bmaxs.y && smins.z >= bmins.z && smaxs.z <= bmaxs.z; }
49
50
51 vector vec2(vector v)
52 {
53         v.z = 0;
54         return v;
55 }
56
57 vector vec3(float x, float y, float z)
58 {
59         vector v;
60         v.x = x;
61         v.y = y;
62         v.z = z;
63         return v;
64 }
65
66 #ifndef MENUQC
67         vector get_corner_position(entity box, int corner)
68         {
69                 switch (corner)
70                 {
71                         case 1: return vec3(box.absmin.x, box.absmin.y, box.absmin.z);
72                         case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
73                         case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
74                         case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
75                         case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
76                         case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
77                         case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
78                         case 8: return vec3(box.absmax.x, box.absmax.y, box.absmax.z);
79                         default: return '0 0 0';
80                 }
81         }
82
83         vector NearestPointOnBox(entity box, vector org)
84         {
85                 vector m1 = box.mins + box.origin;
86                 vector m2 = box.maxs + box.origin;
87
88                 vector ret;
89                 ret.x = bound(m1.x, org.x, m2.x);
90                 ret.y = bound(m1.y, org.y, m2.y);
91                 ret.z = bound(m1.z, org.z, m2.z);
92                 return ret;
93         }
94 #endif
95
96 #endif