summaryrefslogtreecommitdiff
path: root/math.c
blob: 71afabcd5a651ea4fccf270b62f31227345ac73e (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
#ifdef MATH_GL
#undef MATH_GL
#define MATH_GL 1
#endif

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#define PIf 3.14159265358979f
#define HALF_PIf 1.5707963267948966f
#define TAUf 6.283185307179586f
#define SQRT2f 1.4142135623730951f
#define HALF_SQRT2f 0.7071067811865476f
#define SQRT3f 1.7320508075688772f
#define HALF_SQRT3f 0.8660254037844386f

#include <math.h>

static float degrees(float r) {
	return r * (180.0f / PIf);
}
static float radians(float r) {
	return r * (PIf / 180.f);
}

// map x from the interval [0, 1] to the interval [a, b]. does NOT clamp.
static float lerpf(float x, float a, float b) {
	return x * (b-a) + a;
}

// opposite of lerp; map x from the interval [a, b] to the interval [0, 1]. does NOT clamp.
static float normf(float x, float a, float b) {
	return (x-a) / (b-a);
}

static float clampf(float x, float a, float b) {
	if (x < a) return a;
	if (x > b) return b;
	return x;
}

static int clampi(int x, int a, int b) {
	if (x < a) return a;
	if (x > b) return b;
	return x;
}

static i32 clamp_i32(i32 x, i32 a, i32 b) {
	if (x < a) return a;
	if (x > b) return b;
	return x;
}

// remap x from the interval [from_a, from_b] to the interval [to_a, to_b], NOT clamping if x is outside the "from" interval.
static float remapf(float x, float from_a, float from_b, float to_a, float to_b) {
	float pos = (x - from_a) / (from_b - from_a);
	return lerpf(pos, to_a, to_b);
}

static float minf(float a, float b) {
	return a < b ? a : b;
}

static float maxf(float a, float b) {
	return a > b ? a : b;
}

static double maxd(double a, double b) {
	return a > b ? a : b;
}

static double mind(double a, double b) {
	return a < b ? a : b;
}

static u32 min_u32(u32 a, u32 b) {
	return a < b ? a : b;
}

static u32 max_u32(u32 a, u32 b) {
	return a > b ? a : b;
}

static i32 min_i32(i32 a, i32 b) {
	return a < b ? a : b;
}

static i32 max_i32(i32 a, i32 b) {
	return a > b ? a : b;
}

static u64 min_u64(u64 a, u64 b) {
	return a < b ? a : b;
}

static u64 max_u64(u64 a, u64 b) {
	return a > b ? a : b;
}

static i64 min_i64(i64 a, i64 b) {
	return a < b ? a : b;
}

static i64 max_i64(i64 a, i64 b) {
	return a > b ? a : b;
}

static i64 mod_i64(i64 a, i64 b) {
	i64 ret = a % b;
	if (ret < 0) ret += b;
	return ret;
}

static i64 abs_i64(i64 x) {
	return x < 0 ? -x : +x;
}

static i64 sgn_i64(i64 x) {
	if (x < 0) return -1;
	if (x > 0) return +1;
	return 0;
}

static float sgnf(float x) {
	if (x < 0) return -1;
	if (x > 0) return +1;
	return 0;
}

static float smoothstepf(float x) {
	if (x <= 0) return 0;
	if (x >= 1) return 1;
	return x * x * (3 - 2 * x);
}

static float randf(void) {
	return (float)rand() / (float)((ulong)RAND_MAX + 1);
}

static float rand_gauss(void) {
	// https://en.wikipedia.org/wiki/Normal_distribution#Generating_values_from_normal_distribution
	float U = randf(), V = randf();
	return sqrtf(-2 * logf(U)) * cosf(TAUf * V);
}

static u32 rand_u32(void) {
	return ((u32)rand() & 0xfff)
		| ((u32)rand() & 0xfff) << 12
		| ((u32)rand() & 0xff) << 24;
}

static float rand_uniform(float from, float to) {
	return lerpf(randf(), from, to);
}

static float sigmoidf(float x) {
	return 1.0f / (1.0f + expf(-x));
}

// returns ⌈x/y⌉ (x/y rounded up)
static i32 ceildivi32(i32 x, i32 y) {
	if (y < 0) {
		// negating both operands doesn't change the answer
		x = -x;
		y = -y;
	}
	if (x < 0) {
		// truncation is the same as ceiling for negative numbers
		return x / y;
	} else {
		return (x + (y-1)) / y;
	}
}

typedef struct {
	float x, y;
} v2;

static v2 const v2_zero = {0, 0};
static v2 V2(float x, float y) {
	v2 v;
	v.x = x;
	v.y = y;
	return v;
}

static v2 v2_add(v2 a, v2 b) {
	return V2(a.x + b.x, a.y + b.y);
}

static v2 v2_add_const(v2 a, float c) {
	return V2(a.x + c, a.y + c);
}

static v2 v2_sub(v2 a, v2 b) {
	return V2(a.x - b.x, a.y - b.y);
}

static v2 v2_scale(v2 v, float s) {
	return V2(v.x * s, v.y * s);
}

static v2 v2_mul(v2 a, v2 b) {
	return V2(a.x * b.x, a.y * b.y);
}

static v2 v2_clamp(v2 x, v2 a, v2 b) {
	return V2(clampf(x.x, a.x, b.x), clampf(x.y, a.y, b.y));
}

static float v2_dot(v2 a, v2 b) {
	return a.x * b.x + a.y * b.y;
}

static float v2_len(v2 v) {
	return sqrtf(v2_dot(v, v));
}

static v2 v2_lerp(float x, v2 a, v2 b) {
	return V2(lerpf(x, a.x, b.x), lerpf(x, a.y, b.y));
}

// rotate v theta radians counterclockwise
static v2 v2_rotate(v2 v, float theta) {
	float c = cosf(theta), s = sinf(theta);
	return V2(
		c * v.x - s * v.y,
		s * v.x + c * v.y
	);
}

static v2 v2_normalize(v2 v) {
	float len = v2_len(v);
	float mul = len == 0.0f ? 1.0f : 1.0f/len;
	return v2_scale(v, mul);
}

static float v2_dist(v2 a, v2 b) {
	return v2_len(v2_sub(a, b));
}

static float v2_dist_squared(v2 a, v2 b) {
	v2 diff = v2_sub(a, b);
	return v2_dot(diff, diff);
}

static void v2_print(v2 v) {
	printf("(%f, %f)\n", v.x, v.y);
}

#if MATH_GL
static void v2_gl_vertex(v2 v) {
	glVertex2f(v.x, v.y);
}
#endif

static v2 v2_rand_unit(void) {
	float theta = rand_uniform(0, TAUf);
	return V2(cosf(theta), sinf(theta));
}

#if MATH_GL
static void v2_rand_unit_test(void) {
	int i;
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_POINTS);
	for (i = 0; i < 100000; ++i) {
		v2_gl_vertex(v2_rand_unit());
	}
	glEnd();
}
#endif

static v2 v2_polar(float r, float theta) {
	return V2(r * cosf(theta), r * sinf(theta));
}

typedef struct {
	float x, y, z;
} v3;

static v3 const v3_zero = {0, 0, 0};

static v3 V3(float x, float y, float z) {
	v3 v;
	v.x = x;
	v.y = y;
	v.z = z;
	return v;
}

static v3 v3_from_v2(v2 v) {
	return V3(v.x, v.y, 0);
}

#if MATH_GL
static void v3_gl_vertex(v3 v) {
	glVertex3f(v.x, v.y, v.z);
}

static void v3_gl_color(v3 v) {
	glColor3f(v.x, v.y, v.z);
}

static void v3_gl_color_alpha(v3 v, float alpha) {
	glColor4f(v.x, v.y, v.z, alpha);
}
#endif

static v3 v3_add(v3 a, v3 b) {
	return V3(a.x + b.x, a.y + b.y, a.z + b.z);
}

static v3 v3_sub(v3 a, v3 b) {
	return V3(a.x - b.x, a.y - b.y, a.z - b.z);
}

static v3 v3_scale(v3 v, float s) {
	return V3(v.x * s, v.y * s, v.z * s);
}

static v3 v3_lerp(float x, v3 a, v3 b) {
	return V3(lerpf(x, a.x, b.x), lerpf(x, a.y, b.y), lerpf(x, a.z, b.z));
}

static float v3_dot(v3 u, v3 v) {
	return u.x*v.x + u.y*v.y + u.z*v.z;
}

static v3 v3_cross(v3 u, v3 v) {
	v3 prod = V3(u.y*v.z - u.z*v.y, u.z*v.x - u.x*v.z, u.x*v.y - u.y*v.x);
	return prod;
}

static float v3_len(v3 v) {
	return sqrtf(v3_dot(v, v));
}

static float v3_dist(v3 a, v3 b) {
	return v3_len(v3_sub(a, b));
}

static float v3_dist_squared(v3 a, v3 b) {
	v3 diff = v3_sub(a, b);
	return v3_dot(diff, diff);
}

static v3 v3_normalize(v3 v) {
	float len = v3_len(v);
	float mul = len == 0.0f ? 1.0f : 1.0f/len;
	return v3_scale(v, mul);
}

static v2 v3_xy(v3 v) {
	return V2(v.x, v.y);
}

// a point on a unit sphere 
static v3 v3_on_sphere(float yaw, float pitch) {
	return V3(cosf(yaw) * cosf(pitch), sinf(pitch), sinf(yaw) * cosf(pitch));
}

static void v3_print(v3 v) {
	printf("(%f, %f, %f)\n", v.x, v.y, v.z);
}

static v3 v3_rand(void) {
	return V3(randf(), randf(), randf());
}

static v3 v3_rand_unit(void) {
	/*
		monte carlo method
		keep generating random points in cube of radius 1 (width 2) centered at origin,
		until you get a point in the unit sphere, then extend it to find the point lying
		on the sphere.
	*/
	while (1) {
		v3 v = V3(rand_uniform(-1.0f, +1.0f), rand_uniform(-1.0f, +1.0f), rand_uniform(-1.0f, +1.0f));
		float dist_squared_to_origin = v3_dot(v, v);
		if (dist_squared_to_origin <= 1 && dist_squared_to_origin != 0.0f) {
			return v3_scale(v, 1.0f / sqrtf(dist_squared_to_origin));
		}
	}
	return V3(0, 0, 0);
}

#if MATH_GL
static void v3_rand_unit_test(void) {
	int i;
	glColor3f(1.0f, 0.0f, 0.0f);
	glBegin(GL_POINTS);
	for (i = 0; i < 100000; ++i) {
		v3_gl_vertex(v3_rand_unit());
	}
	glEnd();
}
#endif

typedef struct {
	float x, y, z, w;
} v4;

static v4 const v4_zero = {0, 0, 0, 0};

static v4 V4(float x, float y, float z, float w) {
	v4 v;
	v.x = x;
	v.y = y;
	v.z = z;
	v.w = w;
	return v;
}

#if MATH_GL
static void v4_gl_color(v4 v) {
	glColor4f(v.x, v.y, v.z, v.w);
}
#endif

static v4 v4_add(v4 a, v4 b) {
	return V4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}

static v4 v4_sub(v4 a, v4 b) {
	return V4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}

static v4 v4_scale(v4 v, float s) {
	return V4(v.x * s, v.y * s, v.z * s, v.w * s);
}

static v4 v4_scale_xyz(v4 v, float s) {
	return V4(v.x * s, v.y * s, v.z * s, v.w);
}

static v4 v4_lerp(float x, v4 a, v4 b) {
	return V4(lerpf(x, a.x, b.x), lerpf(x, a.y, b.y), lerpf(x, a.z, b.z), lerpf(x, a.w, b.w));
}

static float v4_dot(v4 u, v4 v) {
	return u.x*v.x + u.y*v.y + u.z*v.z + u.w*v.w;
}

// create a new vector by multiplying the respective components of u and v 
static v4 v4_mul(v4 u, v4 v) {
	return V4(u.x * v.x, u.y * v.y, u.z * v.z, u.w * v.w);
}

static float v4_len(v4 v) {
	return sqrtf(v4_dot(v, v));
}

static v4 v4_normalize(v4 v) {
	float len = v4_len(v);
	float mul = len == 0.0f ? 1.0f : 1.0f/len;
	return v4_scale(v, mul);
}

static v3 v4_xyz(v4 v) {
	return V3(v.x, v.y, v.z);
}

static v4 v4_rand(void) {
	return V4(randf(), randf(), randf(), randf());
}

static void v4_print(v4 v) {
	printf("(%f, %f, %f, %f)\n", v.x, v.y, v.z, v.w);
}


// matrices are column-major, because that's what they are in OpenGL 
typedef struct {
	float e[16];
} m4;

static m4 const m4_identity = {{
	1, 0, 0, 0,
	0, 1, 0, 0,
	0, 0, 1, 0,
	0, 0, 0, 1
}};

static void m4_print(m4 m) {
	int i;
	for (i = 0; i < 4; ++i)
		printf("[ %f %f %f %f ]\n", m.e[i], m.e[i+4], m.e[i+8], m.e[i+12]);
	printf("\n");
}

static m4 M4(
	float a, float b, float c, float d,
	float e, float f, float g, float h,
	float i, float j, float k, float l,
	float m, float n, float o, float p) {
	m4 ret;
	float *x = ret.e;
	x[0] = a; x[4] = b; x[ 8] = c; x[12] = d;
	x[1] = e; x[5] = f; x[ 9] = g; x[13] = h;
	x[2] = i; x[6] = j; x[10] = k; x[14] = l;
	x[3] = m; x[7] = n; x[11] = o; x[15] = p;
	return ret;
}

// see https://en.wikipedia.org/wiki/Rotation_matrix#General_rotations 
static m4 m4_yaw(float yaw) {
	float c = cosf(yaw), s = sinf(yaw);
	return M4(
		c, 0, -s, 0,
		0, 1,  0, 0,
		s, 0,  c, 0,
		0, 0,  0, 1
	);
}

static m4 m4_pitch(float pitch) {
	float c = cosf(pitch), s = sinf(pitch);
	return M4(
		1, 0,  0, 0,
		0, c, -s, 0,
		0, s,  c, 0,
		0, 0,  0, 1
	);
}

// https://en.wikipedia.org/wiki/Translation_(geometry) 
static m4 m4_translate(v3 t) {
	return M4(
		1, 0, 0, t.x,
		0, 1, 0, t.y,
		0, 0, 1, t.z,
		0, 0, 0, 1
	);
}

// multiply m by [v.x, v.y, v.z, 1] 
static v3 m4_mul_v3(m4 m, v3 v) {
	return v3_add(v3_scale(V3(m.e[0], m.e[1], m.e[2]), v.x), v3_add(v3_scale(V3(m.e[4], m.e[5], m.e[6]), v.y),
		v3_add(v3_scale(V3(m.e[8], m.e[9], m.e[10]), v.z), V3(m.e[12], m.e[13], m.e[14]))));
}

/*
4x4 perspective matrix.
fov - field of view in radians, aspect - width:height aspect ratio, z_near/z_far - clipping planes
math stolen from gluPerspective (https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml)
*/
static m4 m4_perspective(float fov, float aspect, float z_near, float z_far) {
	float f = 1.0f / tanf(fov / 2.0f);
	return M4(
		f/aspect, 0, 0, 0,
		0, f, 0, 0,
		0, 0, (z_far+z_near) / (z_near-z_far), (2.0f*z_far*z_near) / (z_near-z_far),
		0, 0, -1, 0
	);
}

// windows.h defines near and far, so let's not use those 
static m4 m4_ortho(float left, float right, float bottom, float top, float near_, float far_) {
	float tx = -(right + left)/(right - left);
	float ty = -(top + bottom)/(top - bottom);
	float tz = -(far_ + near_)/(far_ - near_);
	return M4(
		2.0f / (right - left), 0, 0, tx,
		0, 2.0f / (top - bottom), 0, ty,
		0, 0, -2.0f / (far_ - near_), tz,
		0, 0, 0, 1
	);
}


static m4 m4_mul(m4 a, m4 b) {
	m4 prod = {0};
	int i, j;
	float *x = prod.e;
	for (i = 0; i < 4; ++i) {
		for (j = 0; j < 4; ++j, ++x) {
			float *as = &a.e[j];
			float *bs = &b.e[4*i];
			*x = as[0]*bs[0] + as[4]*bs[1] + as[8]*bs[2] + as[12]*bs[3];
		}
	}
	return prod;
}

static m4 m4_inv(m4 mat) {
	m4 ret;
	float *inv = ret.e;
	float *m = mat.e;

	inv[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
	inv[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10];
	inv[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9];
	inv[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9];
	inv[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10];
	inv[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10];
	inv[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9];
	inv[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9];
	inv[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6];
	inv[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] - m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6];
	inv[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5];
	inv[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] - m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5];
	inv[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] - m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6];
	inv[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6];
	inv[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] - m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5];
	inv[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5];

	float det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];

	if (det == 0) {
		memset(inv, 0, sizeof *inv);
	} else {
		det = 1 / det;

		for (int i = 0; i < 16; i++)
			inv[i] *= det;
	}

	return ret;
}
typedef struct {
	int x, y;
} v2i;

static v2i V2I(int x, int y) {
	v2i v;
	v.x = x;
	v.y = y;
	return v;
}

static bool rect_contains_point_v2(v2 pos, v2 size, v2 point) {
	float x1 = pos.x, y1 = pos.y, x2 = pos.x + size.x, y2 = pos.y + size.y,
		x = point.x, y = point.y;
	return x >= x1 && x < x2 && y >= y1 && y < y2;
}

static bool centered_rect_contains_point(v2 center, v2 size, v2 point) {
	return rect_contains_point_v2(v2_sub(center, v2_scale(size, 0.5f)), size, point);
}

typedef struct {
	v2 pos, size;
} Rect;

static Rect rect(v2 pos, v2 size) {
	Rect r;
	r.pos = pos;
	r.size = size;
	return r;
}

static Rect rect4(float x1, float y1, float x2, float y2) {
	assert(x2 >= x1);
	assert(y2 >= y1);
	return rect(V2(x1,y1), V2(x2-x1, y2-y1));
}

static Rect rect_centered(v2 center, v2 size) {
	Rect r;
	r.pos = v2_sub(center, v2_scale(size, 0.5f));
	r.size = size;
	return r;
}

static v2 rect_center(Rect r) {
	return v2_add(r.pos, v2_scale(r.size, 0.5f));
}

static bool rect_contains_point(Rect r, v2 point) {
	return rect_contains_point_v2(r.pos, r.size, point);
}

static Rect rect_translate(Rect r, v2 by) {
	return rect(v2_add(r.pos, by), r.size);
}

static float rect_x1(Rect r) { return r.pos.x; }
static float rect_y1(Rect r) { return r.pos.y; }
static float rect_x2(Rect r) { return r.pos.x + r.size.x; }
static float rect_y2(Rect r) { return r.pos.y + r.size.y; }

static void rect_coords(Rect r, float *x1, float *y1, float *x2, float *y2) {
	*x1 = r.pos.x;
	*y1 = r.pos.y;
	*x2 = r.pos.x + r.size.x;
	*y2 = r.pos.y + r.size.y;
}

static void rect_print(Rect r) {
	printf("Position: (%f, %f), Size: (%f, %f)\n", r.pos.x, r.pos.y, r.size.x, r.size.y);
}

#if MATH_GL
// must be rendering GL_QUADS to use these functions! 

static void rect_render(Rect r) {
	float x1 = r.pos.x, y1 = r.pos.y, x2 = x1 + r.size.x, y2 = y1 + r.size.y;
	glVertex2f(x1, y1);
	glVertex2f(x2, y1);
	glVertex2f(x2, y2);
	glVertex2f(x1, y2);
}

static void rect_render_border(Rect r, float border_thickness) {
	float border_radius = border_thickness * 0.5f;
	float x1 = r.pos.x, y1 = r.pos.y, x2 = x1 + r.size.x, y2 = y1 + r.size.y;

	glVertex2f(x1+border_radius, y1-border_radius);
	glVertex2f(x1+border_radius, y1+border_radius);
	glVertex2f(x2+border_radius, y1+border_radius);
	glVertex2f(x2+border_radius, y1-border_radius);
	
	glVertex2f(x1-border_radius, y2-border_radius);
	glVertex2f(x1-border_radius, y2+border_radius);
	glVertex2f(x2-border_radius, y2+border_radius);
	glVertex2f(x2-border_radius, y2-border_radius);
	
	glVertex2f(x1-border_radius, y1-border_radius);
	glVertex2f(x1+border_radius, y1-border_radius);
	glVertex2f(x1+border_radius, y2-border_radius);
	glVertex2f(x1-border_radius, y2-border_radius);
	
	glVertex2f(x2-border_radius, y1+border_radius);
	glVertex2f(x2+border_radius, y1+border_radius);
	glVertex2f(x2+border_radius, y2+border_radius);
	glVertex2f(x2-border_radius, y2+border_radius);
}


/* 
	gl grayscale color
	i am tired of not having this
*/
static void gl_color1f(float v) {
	glColor3f(v,v,v);
}

/* 
	gl grayscale+alpha color
*/
static void gl_color2f(float v, float a) {
	glColor4f(v,v,v,a);
}

static void rgba_u32_to_floats(u32 rgba, float floats[4]) {
	floats[0] = (float)(rgba >> 24) / 255.f;
	floats[1] = (float)(rgba >> 16) / 255.f;
	floats[2] = (float)(rgba >>  8) / 255.f;
	floats[3] = (float)(rgba >>  0) / 255.f;
}

// color is 0xRRGGBBAA
static void gl_color_rgba(u32 color) {
	glColor4ub((u8)(color >> 24), (u8)(color >> 16), (u8)(color >> 8), (u8)color);
}

// color is 0xRRGGBB
static void gl_color_rgb(u32 color) {
	gl_color_rgba((color << 8) | 0xff);
}

static void gl_quad(float x1, float y1, float x2, float y2) {
	glVertex2f(x1, y1);
	glVertex2f(x2, y1);
	glVertex2f(x2, y2);
	glVertex2f(x1, y2);
}

// returns average of red green and blue components of color
static float rgba_brightness(u32 color) {
	u8 r = (u8)(color >> 24), g = (u8)(color >> 16), b = (u8)(color >> 8);
	return ((float)r+(float)g+(float)b) * (1.0f / 3);
}

static float rects_intersect(Rect r1, Rect r2) {
	if (r1.pos.x >= r2.pos.x + r2.size.x) return false; // r1 is to the right of r2
	if (r2.pos.x >= r1.pos.x + r1.size.x) return false; // r2 is to the right of r1
	if (r1.pos.y >= r2.pos.y + r2.size.y) return false; // r1 is above r2
	if (r2.pos.y >= r1.pos.y + r1.size.y) return false; // r2 is above r1
	return true;
}

// returns whether or not there is any of the clipped rectangle left
static bool rect_clip_to_rect(Rect *clipped, Rect clipper) {
	v2 start_pos = clipped->pos;
	clipped->pos.x = maxf(clipped->pos.x, clipper.pos.x);
	clipped->pos.y = maxf(clipped->pos.y, clipper.pos.y);
	clipped->size = v2_add(clipped->size, v2_sub(start_pos, clipped->pos));

	clipped->size.x = clampf(clipped->size.x, 0, clipper.pos.x + clipper.size.x - clipped->pos.x);
	clipped->size.y = clampf(clipped->size.y, 0, clipper.pos.y + clipper.size.y - clipped->pos.y);
	return clipped->size.x > 0 && clipped->size.y > 0;
}
#endif