-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathna_math.h
3161 lines (2558 loc) · 77.2 KB
/
na_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
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
na_math.h - v0.04
Nick Aversano's C++ math helper library
This is a single header file with a bunch of useful math functions.
===========================================================================
USAGE
Define this in your code:
#include "na_math.h"
LICENSE
This software is dual-licensed to the public domain and under the following
license: you are granted a perpetual, irrevocable license to copy, modify,
publish, and distribute this file as you see fit.
CREDITS
Written by Nick Aversano
Credits are much appreciated but not required.
VERSION HISTORY
0.04 - add C support and better base types
0.03 - added matrix functions, RGB to linear conversions, extra clamps,
distance, height, aspect ratio helpers
0.02 - Breaking API changes
0.01 - Initial version
*/
#ifndef NA_MATH_H
#define NA_MATH_H
//
// TODO(nick):
// - SIMD?
//
#include <math.h>
#define PI 3.14159265359f
#define TAU 6.28318530717958647692f
#define EPSILON_F32 1.1920929e-7f
#define EPSILON_F64 2.220446e-16
#define SQRT_2 0.70710678118
#ifndef INFINITY
#define INFINITY 1e5000f
#endif
#define Mod(x, y) fmodf(x, y)
#define AbsF32(x) fabsf(x)
#define Sqrt(x) sqrtf(x)
#define Sin(x) sinf(x)
#define Cos(x) cosf(x)
#define Tan(x) tanf(x)
#define Pow(x, e) powf(x, e)
#define Exp(x) expf(x)
#define Log(x) logf(x)
#define Log2(x) log2f(x)
#define v2f(x, y) v2((f32)(x), (f32)(y))
#define v3f(x, y, z) v3((f32)(x), (f32)(y), (f32)(z))
#define v4f(x, y, z, w) v4((f32)(x), (f32)(y), (f32)(z), (f32)(w))
//
// NOTE(nick): shared imports to make this standalone
//
#define function static
#define Min(a, b) (((a) < (b)) ? (a) : (b))
#define Max(a, b) (((a) > (b)) ? (a) : (b))
#define Clamp(value, lower, upper) (Max(Min(value, upper), lower))
#define ClampTop(a, b) Min(a, b)
#define ClampBot(a, b) Max(a, b)
#define Sign(x) (((x) > 0) - ((x) < 0))
#define Abs(x) (((x) < 0) ? (0u - x) : (0u + x))
#if !defined(__cplusplus) && !defined(bool)
#define bool int
#define true 1
#define false 0
#endif
#ifndef U8_MAX
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef i8 b8;
typedef i16 b16;
typedef i32 b32;
typedef i64 b64;
typedef float f32;
typedef double f64;
typedef void VoidFunction(void);
#define U8_MAX 0xff
#define I8_MAX 0x7f
#define I8_MIN 0x80
#define U16_MAX 0xffff
#define I16_MAX 0x7fff
#define I16_MIN 0x8000
#define U32_MAX 0xffffffff
#define I32_MAX 0x7fffffff
#define I32_MIN 0x80000000
#define U64_MAX 0xffffffffffffffff
#define I64_MAX 0x7fffffffffffffff
#define I64_MIN 0x8000000000000000
#define F32_MIN 1.17549435e-38f
#define F32_MAX 3.40282347e+38f
#define F64_MIN 2.2250738585072014e-308
#define F64_MAX 1.7976931348623157e+308
#endif // U8_MAX
//
// Types
//
typedef union Vector2 Vector2;
union Vector2
{
struct {
f32 x, y;
};
struct {
f32 width, height;
};
f32 e[2];
};
typedef union Vector2i Vector2i;
union Vector2i
{
struct {
i32 x, y;
};
struct {
i32 width, height;
};
i32 e[2];
};
typedef union Vector3 Vector3;
union Vector3
{
struct {
f32 x, y, z;
};
struct {
f32 r, g, b;
};
struct {
f32 h, s, v;
};
struct {
Vector2 xy;
f32 __ignored1;
};
struct {
f32 __ignored2;
Vector2 yz;
};
f32 e[3];
};
typedef union Vector3i Vector3i;
union Vector3i
{
struct {
i32 x, y, z;
};
struct {
i32 r, g, b;
};
struct {
Vector2i xy;
i32 __ignored1;
};
struct {
i32 __ignored2;
Vector2i yz;
};
i32 e[3];
};
typedef union Vector4 Vector4;
union Vector4
{
struct {
union {
Vector3 xyz;
struct {
f32 x, y, z;
};
};
f32 w;
};
struct {
union {
Vector3 rgb;
struct {
f32 r, g, b;
};
};
f32 a;
};
struct {
f32 red, green, blue, alpha;
};
struct {
f32 left, top, right, bottom;
};
struct {
Vector2 xy;
f32 __ignored0;
f32 __ignored1;
};
struct {
f32 __ignored2;
Vector2 yz;
f32 __ignored3;
};
struct {
f32 __ignored4;
f32 __ignored5;
Vector2 zw;
};
f32 e[4];
};
typedef union Vector4i Vector4i;
union Vector4i
{
struct {
union {
Vector3i xyz;
struct {
i32 x, y, z;
};
};
i32 w;
};
struct {
union {
Vector3i rgb;
struct {
i32 r, g, b;
};
};
i32 a;
};
struct {
Vector2i xy;
i32 __ignored0;
i32 __ignored1;
};
struct {
i32 __ignored2;
Vector2i yz;
i32 __ignored3;
};
struct {
i32 __ignored4;
i32 __ignored5;
Vector2i zw;
};
i32 e[4];
};
typedef struct Rectangle2 Rectangle2;
struct Rectangle2
{
union {
Vector2 p0;
Vector2 u;
Vector2 min;
struct {
f32 x0, y0;
};
};
union {
Vector2 p1;
Vector2 v;
Vector2 max;
struct {
f32 x1, y1;
};
};
};
typedef struct Rectangle2i Rectangle2i;
struct Rectangle2i
{
union {
Vector2i p0;
struct {
i32 x0, y0;
};
};
union {
Vector2i p1;
struct {
i32 x1, y1;
};
};
};
typedef struct Rectangle3 Rectangle3;
struct Rectangle3
{
union {
Vector3 p0;
struct {
f32 x0, y0, z0;
};
};
union {
Vector3 p1;
struct {
f32 x1, y1, z1;
};
};
};
typedef union Matrix3 Matrix3;
union Matrix3
{
Vector3 rows[3];
// NOTE(nick): These are stored row major - e[row][coulumn]
f32 e[3][3];
};
typedef union Matrix4 Matrix4;
union Matrix4
{
Vector4 rows[4];
// NOTE(nick): These are stored row major - e[row][coulumn]
f32 e[4][4];
};
typedef union Quaternion Quaternion;
union Quaternion
{
struct {
f32 x, y, z, w;
};
f32 e[4];
};
//
// Constants
//
const Vector2 v2_zero = {0, 0};
const Vector2 v2_one = {1, 1};
const Vector2 v2_half = {0.5, 0.5};
const Vector2 v2_center = {0.5, 0.5};
const Vector2 v2_top_left = {0, 0};
const Vector2 v2_bottom_left = {0, 1};
const Vector2 v2_center_left = {0, 0.5};
const Vector2 v2_top_right = {1, 0};
const Vector2 v2_bottom_right = {1, 1};
const Vector2 v2_center_right = {1, 0.5};
const Vector3 v3_zero = {0, 0, 0};
const Vector3 v3_one = {1, 1, 1};
const Vector3 v3_half = {0.5, 0.5, 0.5};
const Vector3 v3_center = {0.5, 0.5, 0.5};
const Vector4 v4_zero = {0, 0, 0, 0};
const Vector4 v4_one = {1, 1, 1, 1};
const Vector4 v4_half = {0.5, 0.5, 0.5, 0.5};
const Vector4 v4_center = {0.5, 0.5, 0.5, 0.5};
const Vector4 v4_black = {0, 0, 0, 1};
const Vector4 v4_white = {1, 1, 1, 1};
const Vector4 v4_red = {1, 0, 0, 1};
const Vector4 v4_green = {0, 1, 0, 1};
const Vector4 v4_blue = {0, 0, 1, 1};
const Vector4 v4_cyan = {0, 1, 1, 1};
const Vector4 v4_magenta = {1, 0, 1, 1};
const Vector4 v4_yellow = {1, 1, 0, 1};
const Rectangle2 r2_zero = {0, 0, 0, 0};
const Rectangle2 r2_one = {1, 1, 1, 1};
//
// Basic Functions
//
function i32 min_i32(i32 a, i32 b) { return Min(a, b); }
function u32 min_u32(u32 a, u32 b) { return Min(a, b); }
function i64 min_i64(i64 a, i64 b) { return Min(a, b); }
function u64 min_u64(u64 a, u64 b) { return Min(a, b); }
function f32 min_f32(f32 a, f32 b) { return Min(a, b); }
function f64 min_f64(f64 a, f64 b) { return Min(a, b); }
function i32 max_i32(i32 a, i32 b) { return Max(a, b); }
function u32 max_u32(u32 a, u32 b) { return Max(a, b); }
function i64 max_i64(i64 a, i64 b) { return Max(a, b); }
function u64 max_u64(u64 a, u64 b) { return Max(a, b); }
function f32 max_f32(f32 a, f32 b) { return Max(a, b); }
function f64 max_f64(f64 a, f64 b) { return Max(a, b); }
function f32 min3_f32(f32 a, f32 b, f32 c) { return Min(Min(a, b), c); }
function f32 min3_f64(f64 a, f64 b, f64 c) { return Min(Min(a, b), c); }
function f32 max3_f32(f32 a, f32 b, f32 c) { return Max(Max(a, b), c); }
function f32 max3_f64(f64 a, f64 b, f64 c) { return Max(Max(a, b), c); }
function f32 min4_f32(f32 a, f32 b, f32 c, f32 d) { return Min(Min(a, b), Min(c, d)); }
function f32 min4_f64(f64 a, f64 b, f64 c, f32 d) { return Min(Min(a, b), Min(c, c)); }
function f32 max4_f32(f32 a, f32 b, f32 c, f32 d) { return Max(Max(a, b), Max(c, d)); }
function f32 max4_f64(f64 a, f64 b, f64 c, f32 d) { return Max(Max(a, b), Max(c, d)); }
function i32 clamp_i32(i32 value, i32 lower, i32 upper) { return Clamp(value, lower, upper); }
function u32 clamp_u32(u32 value, u32 lower, u32 upper) { return Clamp(value, lower, upper); }
function u64 clamp_u64(u64 value, u64 lower, u64 upper) { return Clamp(value, lower, upper); }
function f32 clamp_f32(f32 value, f32 lower, f32 upper) { return Clamp(value, lower, upper); }
function f64 clamp_f64(f64 value, f64 lower, f64 upper) { return Clamp(value, lower, upper); }
function f32 clamp_top_f32(f32 value, f32 upper) { return ClampTop(value, upper); }
function f32 clamp_bot_f32(f32 value, f32 lower) { return ClampBot(value, lower); }
function f32 clamp_01_f32(f32 value) { return clamp_f32(value, 0, 1); }
function f32 clamp_01_f64(f64 value) { return clamp_f64(value, 0, 1); }
function f32 wrap_f32(f32 value, f32 lower, f32 upper) {
f32 range = upper - lower;
while (value > upper) value -= range;
while (value < lower) value += range;
return value;
}
function i32 sign_i32(i32 a) { return Sign(a); }
function i32 sign_i64(i64 a) { return Sign(a); }
function f32 sign_f32(f32 a) { return (f32)Sign(a); }
function f64 sign_f64(f64 a) { return Sign(a); }
function i32 abs_i32(i32 a) { return Abs(a); }
function i32 abs_i64(i64 a) { return (i32)Abs(a); }
function f32 abs_f32(f32 x) {
union {
f32 f;
u32 i;
} u = {x};
u.i &= 0x7fffffff;
return u.f;
}
function f64 abs_f64(f64 x) {
union {
f64 f;
u64 i;
} u = {x};
u.i &= 0x7fffffffffffffff;
return u.f;
}
function i32 round_i32(f32 x) {
return (x >= 0) ? (i32)(x + 0.5) : (i32)(x - 0.5);
}
function i64 round_i64(f64 x) {
return (x >= 0) ? (i64)(x + 0.5) : (i64)(x - 0.5);
}
function f32 round_f32(f32 x) { return (f32)round_i32(x); }
function f64 round_f64(f64 x) { return (f64)round_i64(x); }
function i32 floor_i32(f32 x) {
return (i32)x - (x < 0);
}
function i64 floor_i64(f64 x) {
return (i64)x - (x < 0);
}
function f32 floor_f32(f32 x) { return (f32)floor_i32(x); }
function f64 floor_f64(f64 x) { return (f64)floor_i64(x); }
function i32 ceil_i32(f32 x) {
// NOTE(nick): handle negative numbers properly and if the number is already whole
return (i32)(x + (((f32)(i32)x) != x) * (1.0f - (x < 0.0f)));
}
function i64 ceil_i64(f64 x) {
// NOTE(nick): handle negative numbers properly and if the number is already whole
return (i64)(x + (((f64)(i64)x) != x) * (1.0 - (x < 0.0)));
}
function f32 ceil_f32(f32 x) { return (f32)ceil_i32(x); }
function f64 ceil_f64(f64 x) { return (f64)ceil_i64(x); }
function f32 mod_f32(f32 x, f32 y) { return (f32)Mod(x, y); }
function f64 mod_f64(f64 x, f64 y) { return (f64)Mod(x, y); }
function b32 f32_is_zero(f32 x) {
return abs_f32(x) <= EPSILON_F32;
}
function f32 pow_f32(f32 x, f32 n) {
return Pow(x, n);
}
function f32 log_f32(f32 x) { return Log(x); }
function f32 log2_f32(f32 x) { return Log2(x); }
function f32 logn_f32(f32 n, f32 x) { return Log(x) / Log(n); }
function f32 exp_f32(f32 x) { return Exp(x); }
//
// Integers
//
// -fsanitize=signed-integer-overflow in clang
// -fwrapv in gcc and clang
// > Treat signed integer overflow as two’s complement
#if 0
int add_safe(int a, int b)
{
if (a > 0 && b > INT_MAX - a) { ERROR: overflow happens }
else if (a < 0 && b < INT_MIN - a) { ERROR: underflow happens }
return a + b; // now it is OK
}
#endif
// C specifies signed integer representation as using 1 of 3 forms: sign and magnitude, two’s complement, or ones’ complement.
// Given these forms, only division by 0 and two’s complement division of INT_MIN/-1 may overflow.
#if C_VERSION_23
#include <stdckdint.h>
#endif
#if defined(__has_builtin) && __has_builtin(__builtin_add_overflow)
#define __checked_add_overflow(a, b, r) __builtin_add_overflow(a, b, r)
#define __checked_sub_overflow(a, b, r) __builtin_sub_overflow(a, b, r)
#define __checked_mul_overflow(a, b, r) __builtin_mul_overflow(a, b, r)
#elif C_VERSION_23
#define __checked_add_overflow(r, a, b) ckd_add(r, a, b)
#define __checked_sub_overflow(r, a, b) ckd_sub(r, a, b)
#define __checked_mul_overflow(r, a, b) ckd_mul(r, a, b)
#endif
function bool checked_add_i16(i16 a, i16 b, i16 *result) {
#ifdef __checked_add_overflow
return __checked_add_overflow(a, b, result);
#else
*result = (i16)((u16)a + (u16)b);
return (b < 0) ? (a < I16_MIN - b) : (a > I16_MAX - b);
#endif
}
function bool checked_add_i32(i32 a, i32 b, i32 *result) {
#ifdef __checked_add_overflow
return __checked_add_overflow(a, b, result);
#else
*result = (i32)((u32)a + (u32)b);
return (b < 0) ? (a < I32_MIN - b) : (a > I32_MAX - b);
#endif
}
function bool checked_add_i64(i64 a, i64 b, i64 *result) {
#ifdef __checked_add_overflow
return __checked_add_overflow(a, b, result);
#else
*result = (i64)((u64)a + (u64)b);
return (b < 0) ? (a < I64_MIN - b) : (a > I64_MAX - b);
#endif
}
function bool checked_add_u16(u16 a, u16 b, u16 *result) {
#ifdef __checked_add_overflow
return __checked_add_overflow(a, b, result);
#else
u16 r = a + b;
*result = r;
return r < a;
#endif
}
function bool checked_add_u32(u32 a, u32 b, u32 *result) {
#ifdef __checked_add_overflow
return __checked_add_overflow(a, b, result);
#else
u32 r = a + b;
*result = r;
return r < a;
#endif
}
function bool checked_add_u64(u64 a, u64 b, u64 *result) {
#ifdef __checked_add_overflow
return __checked_add_overflow(a, b, result);
#else
u64 r = a + b;
*result = r;
return r < a;
#endif
}
// Saturating addition - limits result between [I32_MIN, I32_MAX]
function i32 saturated_add_i32(i32 a, i32 b) {
#ifdef __checked_add_overflow
i32 res;
if (!__checked_add_overflow(a, b, &res)) return res;
return res >= 0 ? I32_MIN : I32_MAX;
#else
i64 r = (i64)a + (i64)b;
return r < I32_MIN ? I32_MIN : r > I32_MAX ? I32_MAX : (i32)r;
#endif
}
function i16 add_i16(i16 a, i16 b) {
i16 result = 0;
checked_add_i16(a, b, &result);
return result;
}
function i32 add_i32(i32 a, i32 b) {
i32 result = 0;
checked_add_i32(a, b, &result);
return result;
}
function i64 add_i64(i64 a, i64 b) {
i64 result = 0;
checked_add_i64(a, b, &result);
return result;
}
// TODO(nick): subtraction
// TODO(nick): multiplication
// TODO(nick): division
function bool checked_div_i32(i32 a, i32 b, i32 *result)
{
if (b == 0) { return 1; }
#if INT_MIN != -INT_MAX
if (a == I32_MIN && b == -1) { return 1; }
#endif
*result = a / b;
return 0;
}
function bool checked_div_i64(i64 a, i64 b, i64 *result)
{
if (b == 0) { return 1; }
if (a == I64_MIN && b == -1) { return 1; }
*result = a / b;
return 0;
}
function i32 div_i32(i32 a, i32 b)
{
i32 result = 0;
checked_div_i32(a, b, &result);
return result;
}
function i32 div_i64(i64 a, i64 b)
{
i64 result = 0;
checked_div_i64(a, b, &result);
return result;
}
function i16 truncate_i32_to_i16(i32 x)
{
assert(x <= I16_MAX);
i16 result = (i16)x;
return result;
}
function u8 truncate_u64_to_u8(u64 x)
{
assert(x <= U8_MAX);
u8 result = (u8)x;
return result;
}
function u16 truncate_u64_to_u16(u64 x)
{
assert(x <= U16_MAX);
u16 result = (u16)x;
return result;
}
function u32 truncate_u64_to_u32(u64 x)
{
assert(x <= U32_MAX);
u32 result = (u32)x;
return result;
}
//
// Floats
//
// x^(-1/2) with monic 2nd iteration, error = 4.639856 * 10^(-7)
// TODO(nick): test against hardware sqrt?
function f32 fast_sqrt_f32(f32 x)
{
u32 X = *(u32 *)&x;
u32 Y = 0x5F5FFF00 - (X >> 1);
f32 y = *(f32 *)&Y;
y *= 0.9439607f - x*y*y*0.19755164f;
y *= 1.8898820f - x*y*y;
return y;
}
function f32 sqrt_f32(f32 x)
{
return (f32)Sqrt(x);
}
// fast sin function; maximum error is 0.001
function f32 sin_f32(f32 x) {
x = x * (1 / PI);
i32 k = (i32)round_i32(x);
x = x - k;
f32 y = (4 - 4 * abs_f32(x)) * x;
y = 0.225f * (y * abs_f32(y) - y) + y;
return (k & 1) ? -y : y;
}
function f32 cos_f32(f32 x) {
return sin_f32((PI / 2) - x);
}
function f32 tan_f32(f32 x) {
return sin_f32(x) / cos_f32(x);
}
function f32 atan2_f32(f32 y, f32 x) {
// http://pubs.opengroup.org/onlinepubs/009695399/functions/atan2.html
// Volkan SALMA
f32 r, angle;
f32 abs_y = abs_f32(y) + 1e-10f; // kludge to prevent 0/0 condition
if (x < 0.0f) {
r = (x + abs_y) / (abs_y - x);
angle = 0.75 * PI;
} else {
r = (x - abs_y) / (x + abs_y);
angle = 0.25 * PI;
}
angle += (0.1963f * r * r - 0.9817f) * r;
return y < 0 ? -angle : angle;
}
function f32 acos_f32(f32 x) {
f32 negate = (f32)(x < 0);
x = AbsF32(x);
f32 ret = -0.0187293f;
ret = ret * x;
ret = ret + 0.0742610f;
ret = ret * x;
ret = ret - 0.2121144f;
ret = ret * x;
ret = ret + 1.5707288f;
ret = ret * Sqrt(1.0f - x);
ret = ret - 2 * negate * ret;
return negate * PI + ret;
}
function f32 lerp_f32(f32 a, f32 b, f32 t) {
return (1 - t) * a + b * t;
}
function f32 unlerp_f32(f32 a, f32 b, f32 v) {
return (v - a) / (b - a);
}
function f32 remap_f32(f32 in_min, f32 in_max, f32 out_min, f32 out_max, f32 v)
{
f32 t = unlerp_f32(in_min, in_max, v);
return lerp_f32(out_min, out_max, t);
}
function f32 move_f32(f32 a, f32 b, f32 amount) {
return (a > b) ? max_f32(a - amount, b) : min_f32(a + amount, b);
}
function f32 approach_f32(f32 a, f32 b, f32 inc, f32 dec) {
return (a > b) ? max_f32(a - dec, b) : min_f32(a + inc, b);
}
function f32 snap_f32(f32 value, f32 grid) {
return round_f32(value / grid) * grid;
}
function f32 degrees_to_radians(f32 degrees)
{
return degrees * (PI / 180.0f);
}
function f32 radians_to_degrees(f32 radians)
{
return radians * (180.0f / PI);
}
function f32 angle_between(Vector2 p0, Vector2 p1) {
return atan2(p1.y - p0.y, p1.x - p0.x);
}
function f32 direction(Vector2 p0) {
return -atan2(-p0.y, p0.x);
}
//
// Vector2
//
function Vector2 v2(f32 x, f32 y) {
Vector2 result = {x, y};
return result;
}
function Vector2 v2_from_v2i(Vector2i v) {
Vector2 result = {(f32)v.x, (f32)v.y};
return result;
}
function b32 v2_equals(Vector2 a, Vector2 b) {
return abs_f32(a.x - b.x) <= EPSILON_F32 && abs_f32(a.y - b.y) <= EPSILON_F32;
}
function b32 v2_is_zero(Vector2 a) {
return v2_equals(a, v2_zero);
}
function Vector2 v2_add(Vector2 a, Vector2 b) {
Vector2 result = {a.x + b.x, a.y + b.y};
return result;
}
function Vector2 v2_sub(Vector2 a, Vector2 b) {
Vector2 result = {a.x - b.x, a.y - b.y};
return result;
}
function Vector2 v2_inv(Vector2 a) {
Vector2 result = {-a.x, -a.y};
return result;
}
function Vector2 v2_mul(Vector2 a, Vector2 b) {
Vector2 result = {a.x * b.x, a.y * b.y};
return result;
}
function Vector2 v2_mulf(Vector2 a, f32 s) {
Vector2 result = {a.x * s, a.y * s};
return result;
}
function Vector2 v2_divf(Vector2 a, f32 s) {
Vector2 result = {a.x / s, a.y / s};
return result;
}
function Vector2 v2_div(Vector2 a, Vector2 b) {
Vector2 result = {
a.x / b.x,
a.y / b.y,
};
return result;
}
function f32 v2_dot(Vector2 a, Vector2 b) {
return a.x * b.x + a.y * b.y;
}
function Vector2 v2_perp(Vector2 a) {
Vector2 result = {-a.y, a.x};
return result;
}
function f32 v2_length_squared(Vector2 a) {
return v2_dot(a, a);
}
function f32 v2_length(Vector2 a) {
return Sqrt(v2_length_squared(a));
}
function f32 v2_distance_squared(Vector2 a, Vector2 b) {
return v2_length_squared(v2_sub(a, b));
}
function f32 v2_distance(Vector2 a, Vector2 b) {
return v2_length(v2_sub(a, b));
}
function Vector2 v2_normalize(Vector2 a) {
Vector2 result = {0};
f32 len = v2_length(a);
if (len != 0.0f)
{
result.x = a.x * (1.0f / len);
result.y = a.y * (1.0f / len);
}
return result;
}
function Vector2 v2_arm(f32 angle) {
Vector2 result = {Cos(angle), Sin(angle)};
return result;
}
function f32 v2_triangle_area(Vector2 a, Vector2 b, Vector2 c)
{
f32 abx = b.x - a.x;
f32 aby = b.y - a.y;
f32 acx = c.x - a.x;
f32 acy = c.y - a.y;
return (acx*aby - abx*acy) * 0.5;
}
// Functions:
function Vector2 lerp_v2(Vector2 a, Vector2 b, f32 t) {
Vector2 result = {
lerp_f32(a.x, b.x, t),
lerp_f32(a.y, b.y, t),
};
return result;
}
function Vector2 min_v2(Vector2 a, Vector2 b) {
Vector2 result = {
min_f32(a.x, b.x),
min_f32(a.y, b.y),
};
return result;
}
function Vector2 max_v2(Vector2 a, Vector2 b) {
Vector2 result = {max_f32(a.x, b.x), max_f32(a.y, b.y)};