-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.qc
More file actions
151 lines (129 loc) · 2.82 KB
/
Copy pathmath.qc
File metadata and controls
151 lines (129 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* math.qc
*
* Author: Joshua Skelton joshua.skelton@gmail.com
*
* A collection of helpful math functions.
*/
// Forward declarations
float(float value, float minValue, float maxValue) clamp;
float(float a, float b) mod;
float(float x) sign;
float(float value, float minValue, float maxValue) wrap;
/*
* clamp
*
* Limits the given value to the given range.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) clamp = {
if (value < minValue) {
return minValue;
}
else if (value > maxValue) {
return maxValue;
}
return value;
};
/*
* mod
*
* Returns the remainder after the division of a by n
*
* a: The dividend
*
* b: The divisor
*
* Returns: The remainder of a divided by n
*/
float(float a, float n) mod = {
return a - (n * floor(a / n));
};
/*
* sign
*
* Returns an indication of the sign of the given number.
*
* x: A number
*
* Returns: -1 if x < 0, 0 if x == 0, 1 if x > 0.
*/
float(float x) sign = {
if (x > 0) {
return 1;
}
else if (x < 0) {
return -1;
}
return 0;
};
/*
* wrap
*
* Limits the given value to the given range and will wrap the value to the
* the other end of the range if exceeded.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) wrap = {
local float range = maxValue - minValue;
return mod(value - minValue, range + 1) + minValue;
};
float(float a, float b, float mix) lerp =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
return (b * mix + a * ( 1 - mix ) );
}
vector(vector a, vector b, float mix) lerpVector =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
return (b * mix + a * ( 1 - mix ) );
}
// for a relaxing lerp: hermite lerp.
float(float a, float b, float mix) lerpHermite =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float h01;
h01 = mix * mix;
h01 *= 3 - 2 * mix;
return (b * h01 + a * ( 1 - h01 ) );
}
vector(vector a, vector b, float mix) lerpVectorHermite =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float h01;
h01 = mix * mix;
h01 *= 3 - 2 * mix;
return (b * h01 + a * ( 1 - h01 ) );
}
float(float anga, float angb) angledif =
{
float dif;
dif = fabs(anga - angb);
if (dif > 180)
dif = 360 - dif;
return dif;
}
float(vector ang, vector base_ang, vector offset) isInAngle = {
if (angledif(ang_x, base_ang_x) > offset_x || angledif(ang_y, base_ang_y) > offset_y)
return FALSE;
else
return TRUE;
};