aboutsummaryrefslogtreecommitdiff
path: root/mons_math/src/vec3.c
diff options
context:
space:
mode:
Diffstat (limited to 'mons_math/src/vec3.c')
-rw-r--r--mons_math/src/vec3.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/mons_math/src/vec3.c b/mons_math/src/vec3.c
new file mode 100644
index 0000000..725f5b3
--- /dev/null
+++ b/mons_math/src/vec3.c
@@ -0,0 +1,175 @@
+#include "mons_math/vec3.h"
+#include "mons_math/vec2.h"
+#include "mons_math/vec4.h"
+#include "mons_math/util.h"
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+mons_vec3 mons_vec3_add(mons_vec3 a, mons_vec3 b) {
+ mons_vec3 result = {
+ a.x + b.x,
+ a.y + b.y,
+ a.z + b.z,
+ };
+ return result;
+}
+
+void mons_vec3_add_inplace(mons_vec3 *a, mons_vec3 b) {
+ a->x += b.x;
+ a->y += b.y;
+ a->z += b.z;
+}
+
+mons_vec3 mons_vec3_sub(mons_vec3 a, mons_vec3 b) {
+ mons_vec3 result = {
+ a.x - b.x,
+ a.y - b.y,
+ a.z - b.z,
+ };
+ return result;
+}
+
+void mons_vec3_sub_inplace(mons_vec3 *a, mons_vec3 b) {
+ a->x -= b.x;
+ a->y -= b.y;
+ a->z -= b.z;
+}
+
+mons_vec3 mons_vec3_mul_f(mons_vec3 a, float b) {
+ mons_vec3 result = {
+ a.x * b,
+ a.y * b,
+ a.z * b,
+ };
+ return result;
+}
+
+mons_vec3 mons_vec3_mul_i(mons_vec3 a, int b) {
+ mons_vec3 result = {
+ a.x * b,
+ a.y * b,
+ a.z * b,
+ };
+ return result;
+}
+
+float mons_vec3_dot(mons_vec3 a, mons_vec3 b) {
+ return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
+}
+
+void mons_vec3_mul_f_inplace(mons_vec3 *a, float b) {
+ a->x *= b;
+ a->y *= b;
+ a->z *= b;
+}
+
+void mons_vec3_mul_i_inplace(mons_vec3 *a, int b) {
+ a->x *= b;
+ a->y *= b;
+ a->z *= b;
+}
+
+float mons_vec3_len(mons_vec3 a) { return sqrtf(mons_vec3_len_squared(a)); }
+
+float mons_vec3_len_squared(mons_vec3 a) {
+ return (a.x * a.x) + (a.y * a.y) + (a.z * a.z);
+}
+
+mons_vec4 mons_vec3_extend(mons_vec3 a) {
+ mons_vec4 result = {
+ a.x,
+ a.y,
+ a.z,
+ 0.0,
+ };
+ return result;
+}
+
+mons_vec2 mons_vec3_truncate(mons_vec3 a) {
+ mons_vec2 result = {
+ a.x,
+ a.y,
+ };
+ return result;
+}
+
+int mons_vec3_equal(mons_vec3 a, mons_vec3 b) {
+ return mons_float_approx_equal(a.x, b.x)
+ && mons_float_approx_equal(a.y, b.y)
+ && mons_float_approx_equal(a.z, b.z);
+}
+
+mons_vec3 mons_vec3_div_f(mons_vec3 a, float b) {
+ return (mons_vec3) {
+ a.x / b,
+ a.y / b,
+ a.z / b,
+ };
+}
+
+void mons_vec3_div_f_inplace(mons_vec3 *a, float b) {
+ a->x /= b;
+ a->y /= b;
+ a->z /= b;
+}
+
+mons_vec3 mons_vec3_div_i(mons_vec3 a, int b) {
+ return (mons_vec3) {
+ a.x / b,
+ a.y / b,
+ a.z / b,
+ };
+}
+
+void mons_vec3_div_i_inplace(mons_vec3 *a, int b) {
+ a->x /= b;
+ a->y /= b;
+ a->z /= b;
+}
+
+mons_vec3 mons_vec3_negate(mons_vec3 a) {
+ return mons_vec3_mul_i(a, -1);
+}
+
+void mons_vec3_negate_inplace(mons_vec3 *a) {
+ return mons_vec3_mul_i_inplace(a, -1);
+}
+
+mons_vec3 mons_vec3_normalize(mons_vec3 a) {
+ float length = mons_vec3_len(a);
+ return mons_vec3_div_f(a, length);
+}
+
+void mons_vec3_normalize_inplace(mons_vec3 *a) {
+ float length = mons_vec3_len(*a);
+ mons_vec3_div_f_inplace(a, length);
+}
+
+mons_vec3 mons_vec3_cross(mons_vec3 a, mons_vec3 b) {
+ return (mons_vec3) {
+ (a.y * b.z) - (a.z * b.y),
+ (a.z * b.x) - (a.x * b.z),
+ (a.x * b.y) - (a.y * b.x),
+ };
+}
+
+mons_vec3 mons_vec3_mul_memberwise(mons_vec3 a, mons_vec3 b) {
+ return (mons_vec3) {
+ a.x * b.x,
+ a.y * b.y,
+ a.z * b.z,
+ };
+}
+
+void mons_vec3_mul_memberwise_inplace(mons_vec3 *a, mons_vec3 b) {
+ a->x *= b.x;
+ a->y *= b.y;
+ a->z *= b.z;
+}
+
+void mons_vec3_print(mons_vec3 vec) {
+ printf("[%f, %f, %f]", vec.x, vec.y, vec.z);
+}
+