#include "mons_math/vec2.h" #include "mons_math/util.h" #include "mons_math/vec3.h" #include 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); }