-
Notifications
You must be signed in to change notification settings - Fork 1
/
math.h
295 lines (246 loc) · 4.6 KB
/
math.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <cmath> // fabs, sqrt
#include <cfloat> // FLT_MAX FLT_MIN
struct float2
{
float x, y;
float2() {}
float2(float a, float b) : x(a), y(b) {}
};
inline float si_absf(float x)
{
return ::fabsf(x);
}
inline float si_sqrtf(float x)
{
return ::sqrtf(x);
}
inline float si_sinf(float x)
{
return ::sinf(x);
}
inline float si_cosf(float x)
{
return ::cosf(x);
}
inline float si_atan2(float y, float x)
{
return ::atan2f(y, x);
}
inline float si_powf(float x, float p)
{
return ::powf(x, p);
}
inline float lerp(float a, float b, float t)
{
return a * (1.0f - t) + b * t;
}
inline float invlerp(float a, float b, float x)
{
return (x - a) / (b - a);
}
inline bool iszero(const float2 &v)
{
bool result;
result = si_absf(v.x) < 0.0000001f && si_absf(v.y) < 0.0000001f;
return result;
}
inline float2 operator-(const float2 &v)
{
float2 result;
result.x = -v.x;
result.y = -v.y;
return result;
}
inline float2 operator+(const float2 &a, const float2 &b)
{
float2 result;
result.x = a.x + b.x;
result.y = a.y + b.y;
return result;
}
inline float2 operator-(const float2 &a, const float2 &b)
{
float2 result;
result.x = a.x - b.x;
result.y = a.y - b.y;
return result;
}
inline float2 operator*(float k, const float2 &v)
{
float2 result;
result.x = k * v.x;
result.y = k * v.y;
return result;
}
inline float2 operator*(const float2 &v, float k)
{
float2 result;
result.x = k * v.x;
result.y = k * v.y;
return result;
}
inline float2 operator/(const float2 &v, float k)
{
float2 result;
result.x = v.x / k;
result.y = v.y / k;
return result;
}
inline float len(const float2 &v)
{
float result = si_sqrtf(v.x * v.x + v.y * v.y);
return result;
}
inline float dot(const float2 &a, const float2 &b)
{
float result;
result = a.x * b.x + a.y * b.y;
return result;
}
inline float cross(const float2 &a, const float2 &b)
{
float result;
result = a.x * b.y - a.y * b.x;
return result;
}
inline float2 perp(const float2 &v)
{
float2 result;
result.x = -v.y;
result.y = v.x;
return result;
}
inline float2 norm(const float2 &v)
{
return v * (1.0f / len(v));
}
inline float cos(const float2 &a, const float2 &b)
{
float result;
result = dot(a, b);
result /= len(a);
result /= len(b);
return result;
}
inline float sin(const float2 &a, const float2 &b)
{
float result;
result = cross(a, b);
result /= len(a);
result /= len(b);
return result;
}
inline float2 lerp(const float2& a, const float2& b, float t)
{
return a * (1.0f - t) + b * t;
}
inline float si_minf(float a, float b)
{
if (a < b) return a;
return b;
}
inline float si_maxf(float a, float b)
{
if (a > b) return a;
return b;
}
inline double si_mind(double a, double b)
{
if (a < b) return a;
return b;
}
inline double si_maxd(double a, double b)
{
if (a > b) return a;
return b;
}
inline float si_clampf(float x, float x0, float x1)
{
return si_minf(si_maxf(x, x0), x1);
}
inline double si_clampd(double x, double x0, double x1)
{
return si_mind(si_maxd(x, x0), x1);
}
const float si_pi = 3.1415926535f;
struct box2
{
float2 lb, rt;
box2() :
lb(FLT_MAX, FLT_MAX),
rt(-FLT_MAX, -FLT_MAX)
{}
box2(float l, float r, float b, float t) :
lb(l, b),
rt(r, t)
{}
void grow(float s)
{
lb.x -= s;
rt.x += s;
lb.y -= s;
rt.y += s;
}
void add(const float2 &p)
{
if (p.x > rt.x) rt.x = p.x;
if (p.x < lb.x) lb.x = p.x;
if (p.y > rt.y) rt.y = p.y;
if (p.y < lb.y) lb.y = p.y;
}
void add_box(const box2 &b)
{
if (!b.empty())
{
add(b.lb);
add(b.rt);
}
}
bool empty() const
{
return lb.x > rt.x || lb.y > rt.y;
}
bool contains(const float2 &p)
{
return p.x >= lb.x && p.x <= rt.x &&
p.y >= lb.y && p.y <= rt.y;
}
float width() const
{
return rt.x - lb.x;
}
float height() const
{
return rt.y - lb.y;
}
float2 get_lt() const
{
return{ lb.x, rt.y };
}
float2 get_rb() const
{
return{ rt.x, lb.y };
}
};
// basis with perpendicular y and x axes
// NOTE: y is 90 degrees ccw x
struct basis2s
{
float2 o;
float2 x;
float2 get_y() const { return{ -x.y, x.x }; }
};
struct basis2r
{
float2 o;
float2 x;
float2 y;
};
struct color3f
{
float r, g, b;
};
struct color4f
{
float r, g, b, a;
};