-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_vec2.h
188 lines (155 loc) · 3.64 KB
/
math_vec2.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
#ifndef MATH_VEC2_MATH_H
#define MATH_VEC2_MATH_H
#ifdef __cplusplus
extern "C" {
#endif
#include "math_m.h"
typedef float vec2[2]; // v[0]: x, v[1]: y
typedef int ivec2[2]; // v[0]: x, v[1]: y
// ---- vec2 creation ----
#define VEC2_INIT(f) { f, f }
#define VEC2_XY_INIT(x, y) { x, y }
#define VEC2_X_INIT(x) { x, 0 }
#define VEC2_Y_INIT(y) { 0, y }
#define VEC2(f) (vec2){ f, f }
#define VEC2_XY(x, y) (vec2){ x, y }
#define VEC2_X(x) (vec2){ x, 0 }
#define VEC2_Y(y) (vec2){ 0, y }
// ---- print util ----
#define P_VEC2(v) printf("|%s| x: %.2f, y: %.2f\n", #v, v[0], v[1])
#define P_IL_VEC2(v) printf("|%s| x: %.2f, y: %.2f", #v, v[0], v[1])
// static to avoid duplication when including header in multiple files
// forced inline to always inline the functions
// #define VEC2_INLINE static inline __attribute((always_inline))
// ---- vec2 ----
M_INLINE void vec2_add(vec2 a, vec2 b, vec2 out)
{
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
}
M_INLINE void vec2_add_f(vec2 a, float f, vec2 out)
{
out[0] = a[0] + f;
out[1] = a[1] + f;
}
M_INLINE void vec2_sub(vec2 a, vec2 b, vec2 out)
{
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
}
M_INLINE void vec2_sub_f(vec2 a, float f, vec2 out)
{
out[0] = a[0] - f;
out[1] = a[1] - f;
}
M_INLINE void vec2_mul(vec2 a, vec2 b, vec2 out)
{
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
}
M_INLINE void vec2_mul_f(vec2 a, float f, vec2 out)
{
out[0] = a[0] * f;
out[1] = a[1] * f;
}
M_INLINE void vec2_div(vec2 a, vec2 b, vec2 out)
{
out[0] = a[0] / b[0];
out[1] = a[1] / b[1];
}
M_INLINE void vec2_div_f(vec2 a, float f, vec2 out)
{
out[0] = a[0] / f;
out[1] = a[1] / f;
}
M_INLINE float vec2_dot(vec2 a, vec2 b)
{
return (a[0] * b[0]) + (a[1] * b[1]);
}
M_INLINE float vec2_magnitude(vec2 a) // length of vec
{
return sqrtf((a[0] * a[0]) + (a[1] * a[1]));
}
M_INLINE void vec2_copy(vec2 a, vec2 out)
{
out[0] = a[0];
out[1] = a[1];
}
M_INLINE void vec2_copy_f(float f, vec2 out)
{
out[0] = f;
out[1] = f;
}
M_INLINE void vec2_negate(vec2 a, vec2 out)
{
out[0] = -a[0];
out[1] = -a[1];
}
M_INLINE void vec2_normalize(vec2 a, vec2 out)
{
vec2_copy(a, out);
float mag = vec2_magnitude(out);
vec2_div_f(out, mag, out);
}
M_INLINE float vec2_distance(vec2 a, vec2 b)
{
vec2 d;
vec2_sub(a, b, d);
return vec2_magnitude(d);
}
M_INLINE void vec2_clamp(vec2 a, vec2 min, vec2 max, vec2 out)
{
out[0] = CLAMP(a[0], min[0], max[0]);
out[1] = CLAMP(a[1], min[1], max[1]);
}
M_INLINE void vec2_clamp_f(vec2 a, float min, float max, vec2 out)
{
out[0] = CLAMP(a[0], min, max);
out[1] = CLAMP(a[1], min, max);
}
M_INLINE void vec2_abs(vec2 a, vec2 out)
{
out[0] = fabsf(a[0]);
out[1] = fabsf(a[1]);
}
// ---- interpolation ----
M_INLINE void vec2_lerp(vec2 start, vec2 end, float percentage, vec2 out)
{
out[0] = m_lerp(start[0], end[0], percentage);
out[1] = m_lerp(start[1], end[1], percentage);
}
M_INLINE void vec2_lerp_f(float start, float end, float percentage, vec2 out)
{
out[0] = m_lerp(start, end, percentage);
out[1] = m_lerp(start, end, percentage);
}
// ---- conditionals ----
M_INLINE bool vec2_equal(vec2 a, vec2 b)
{
// return a[0] == b[0] && a[1] == b[1];
return FLOAT_EQ(a[0], b[0]) && FLOAT_EQ(a[1], b[1]);
}
M_INLINE bool vec2_not_equal(vec2 a, vec2 b)
{
return !vec2_equal(a, b);
}
M_INLINE bool vec2_greater(vec2 a, vec2 b)
{
return a[0] > b[0] && a[1] > b[1];
}
M_INLINE bool vec2_less(vec2 a, vec2 b)
{
return a[0] < b[0] && a[1] < b[1];
}
M_INLINE bool vec2_greater_eq(vec2 a, vec2 b)
{
return a[0] >= b[0] && a[1] >= b[1];
}
M_INLINE bool vec2_less_eq(vec2 a, vec2 b)
{
return a[0] <= b[0] && a[1] <= b[1];
}
#ifdef __cplusplus
} // extern C
#endif
#endif