-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_stuff.h
More file actions
290 lines (234 loc) · 3.56 KB
/
Copy pathmath_stuff.h
File metadata and controls
290 lines (234 loc) · 3.56 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
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
#ifndef math_stuff_h
#define math_stuff_h
#include "types.h"
#define atLeast(x, y) max(x, y)
#define atMost(x, y) min(x, y)
#define min(x, y) (((x) < (y)) ? (x) : (y))
#define max(x, y) (((x) > (y)) ? (x) : (y))
//
// Miscellaneous
//
f32
lerp(f32 a, f32 t, f32 b)
{
return (a + t * (b - a));
}
int
hexagonNumber(int k)
{
return (1 + 3 * k * (k - 1));
}
int
mod(int a, int b)
{
int result = a % b;
if (result < 0)
{
result += b;
}
return result;
}
//
// Vector
//
struct V2 {
f32 x, y;
};
inline V2
v2(f32 x, f32 y)
{
V2 result = {x, y};
return result;
}
inline V2
operator * (f32 a, V2 b)
{
V2 result;
result.x = a * b.x;
result.y = a * b.y;
return result;
}
inline V2
operator * (V2 b, f32 a)
{
return a * b;
}
inline V2
operator *= (V2 &b, f32 a)
{
b = a * b;
return b;
}
inline V2
operator / (V2 a, f32 b)
{
return a * (1.0f / b);
}
inline V2
operator /= (V2 &a, f32 b)
{
a = a / b;
return a;
}
inline V2
operator + (V2 a, V2 b)
{
V2 result;
result.x = a.x + b.x;
result.y = a.y + b.y;
return result;
}
inline V2
operator += (V2 &b, V2 a)
{
b = b + a;
return b;
}
inline V2
operator - (V2 a)
{
V2 result;
result.x = - a.x;
result.y = - a.y;
return result;
}
inline V2
operator - (V2 a, V2 b)
{
V2 result;
result.x = a.x - b.x;
result.y = a.y - b.y;
return result;
}
inline V2
operator -= (V2 &b, V2 a)
{
b = b - a;
return b;
}
inline f32
inner(V2 a, V2 b)
{
return a.x * b.x + a.y * b.y;
}
inline f32
outer(V2 a, V2 b)
{
return a.x * b.y - a.y * b.x;
}
inline f32
square(f32 a)
{
return a * a;
}
inline f64
square(f64 a)
{
return a * a;
}
inline f32
square(V2 a)
{
return inner(a, a);
}
// TODO(pontus): get rid of this epsilon
inline bool
operator == (V2 a, V2 b)
{
return square(a - b) < 0.001f;
}
inline V2
normalize(V2 a)
{
f32 sq = square(a);
// TODO(pontus): should this be epsilon?
if (sq > 0) {
return a * (1.0f / sqrt(sq));
}
else
{
return a;
}
}
inline bool
isNonZero(V2 a)
{
return (a.x != 0) || (a.y != 0);
}
inline V2
normalFromVector(V2 a)
{
// NOTE(pontus): on the right side of the vector
return normalize( v2(a.y, -a.x) );
}
inline V2
v2FromAngle(f32 angle)
{
return v2(cos(angle), sin(angle));
}
bool
ccw(V2 a, V2 b, V2 c)
{
return outer(b - a, c - b) > 0;
}
V2
periodize(V2 a, f32 width, f32 height)
{
a.x -= width * round(a.x / width);
a.y -= height * round(a.y / height);
return a;
}
void printV2(V2 v)
{
printf("%.2f, %.2f\n", v.x, v.y);
}
//
// Colors
//
struct Color4 {
f32 r, g, b, a;
};
Color4
c4(f32 r, f32 g, f32 b, f32 a)
{
Color4 color = {r, g, b, a};
return color;
}
//
// Random Number Generation
//
f32
randomF32()
{
return ((f32)rand() / RAND_MAX);
}
f32
randomBetween(f32 a, f32 b)
{
return lerp(a, randomF32(), b);
}
f32
randomGaussian()
{
// NOTE: this algorithm generates 2 random numbers at a time
// we save the second one, so every other call is faster
local_persist bool isPrepared;
local_persist f32 preparedValue;
if (isPrepared)
{
isPrepared = false;
return preparedValue;
}
// Taken from http://www.design.caltech.edu/erik/Misc/Gaussian.html
f32 x, y, w;
do
{
x = 2 * randomF32() - 1;
y = 2 * randomF32() - 1;
w = x * x + y * y;
} while (w >= 1);
f32 factor = sqrt( (-2.0 * log(w)) / w);
preparedValue = x * factor;
return (y * factor);
}
#endif