diff options
author | 2025-02-07 11:27:18 -0500 | |
---|---|---|
committer | 2025-02-07 11:27:18 -0500 | |
commit | 4da7be39827ea5888ef9c97b1aadf61b0d76347c (patch) | |
tree | 15d0ff8f8bcb0e871efb1b2e65c2bc8d07b17565 /mons_math/src/vec2.c |
initial commit (lol)
Diffstat (limited to 'mons_math/src/vec2.c')
-rw-r--r-- | mons_math/src/vec2.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/mons_math/src/vec2.c b/mons_math/src/vec2.c new file mode 100644 index 0000000..590397d --- /dev/null +++ b/mons_math/src/vec2.c @@ -0,0 +1,120 @@ +#include "mons_math/vec2.h" +#include "mons_math/util.h" +#include "mons_math/vec3.h" +#include <math.h> + +mons_vec2 mons_vec2_add(mons_vec2 a, mons_vec2 b) { + mons_vec2 result = { + a.x + b.x, + a.y + b.y, + }; + return result; +} + +void mons_vec2_add_inplace(mons_vec2 *a, mons_vec2 b) { + a->x += b.x; + a->y += b.y; +} + +mons_vec2 mons_vec2_sub(mons_vec2 a, mons_vec2 b) { + mons_vec2 result = { + a.x - b.x, + a.y - b.y, + }; + return result; +} + +void mons_vec2_sub_inplace(mons_vec2 *a, mons_vec2 b) { + a->x -= b.x; + a->y -= b.y; +} + +mons_vec2 mons_vec2_mul_f(mons_vec2 a, float b) { + mons_vec2 result = { + a.x * b, + a.y * b, + }; + return result; +} + +mons_vec2 mons_vec2_mul_i(mons_vec2 a, int b) { + mons_vec2 result = { + a.x * b, + a.y * b, + }; + return result; +} + +float mons_vec2_dot(mons_vec2 a, mons_vec2 b) { + return (a.x * b.x) + (a.y * b.y); +} + +void mons_vec2_mul_f_inplace(mons_vec2 *a, float b) { + a->x *= b; + a->y *= b; +} + +void mons_vec2_mul_i_inplace(mons_vec2 *a, int b) { + a->x *= b; + a->y *= b; +} + +float mons_vec2_len(mons_vec2 a) { return sqrtf(mons_vec2_len_squared(a)); } + +float mons_vec2_len_squared(mons_vec2 a) { return (a.x * a.x) + (a.y * a.y); } + +mons_vec3 mons_vec2_extend(mons_vec2 a) { + mons_vec3 result = { + a.x, + a.y, + 0.0, + }; + return result; +} + +int mons_vec2_equal(mons_vec2 a, mons_vec2 b) { + return mons_float_approx_equal(a.x, b.x) && + mons_float_approx_equal(a.y, b.y); +} + +mons_vec2 mons_vec2_div_f(mons_vec2 a, float b) { + return (mons_vec2) { + a.x / b, + a.y / b, + }; +} + +mons_vec2 mons_vec2_div_i(mons_vec2 a, int b) { + return (mons_vec2) { + a.x / b, + a.y / b, + }; +} + +void mons_vec2_div_f_inplace(mons_vec2 *a, float b) { + a->x /= b; + a->y /= b; +} + +void mons_vec2_div_i_inplace(mons_vec2 *a, int b) { + a->x /= b; + a->y /= b; +} + +mons_vec2 mons_vec2_negate(mons_vec2 a) { + return mons_vec2_mul_i(a, -1); +} + +void mons_vec2_negate_inplace(mons_vec2 *a) { + mons_vec2_mul_i_inplace(a, -1); +} + +mons_vec2 mons_vec2_normalize(mons_vec2 a) { + float len = mons_vec2_len(a); + return mons_vec2_div_f(a, len); +} + +void mons_vec2_normalize_inplace(mons_vec2 *a) { + float len = mons_vec2_len(*a); + mons_vec2_div_f_inplace(a, len); +} |