]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/vector.qh
Lib: re-home functions from common/util
[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; v.x = x; v.y = y; v.z = z;
60     return v;
61 }
62
63 #ifndef MENUQC
64 vector get_corner_position(entity box, int corner)
65 {
66     switch (corner) {
67         case 1: return vec3(box.absmin.x, box.absmin.y, box.absmin.z);
68         case 2: return vec3(box.absmax.x, box.absmin.y, box.absmin.z);
69         case 3: return vec3(box.absmin.x, box.absmax.y, box.absmin.z);
70         case 4: return vec3(box.absmin.x, box.absmin.y, box.absmax.z);
71         case 5: return vec3(box.absmax.x, box.absmax.y, box.absmin.z);
72         case 6: return vec3(box.absmin.x, box.absmax.y, box.absmax.z);
73         case 7: return vec3(box.absmax.x, box.absmin.y, box.absmax.z);
74         case 8: return vec3(box.absmax.x, box.absmax.y, box.absmax.z);
75         default: return '0 0 0';
76     }
77 }
78
79 vector NearestPointOnBox(entity box, vector org)
80 {
81     vector m1 = box.mins + box.origin;
82     vector m2 = box.maxs + box.origin;
83
84     vector ret;
85     ret.x = bound(m1.x, org.x, m2.x);
86     ret.y = bound(m1.y, org.y, m2.y);
87     ret.z = bound(m1.z, org.z, m2.z);
88     return ret;
89 }
90 #endif
91
92 #endif