aboutsummaryrefslogtreecommitdiff
path: root/src/vecmath.c
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-06-10 02:42:44 +0800
committerMistivia <i@mistivia.com>2025-06-10 02:42:44 +0800
commit07f3a1a5751e141c414d77e57c0e631feb441ef3 (patch)
tree9d15ef0ec323ce91972463fd7730f2543ca917a6 /src/vecmath.c
parentc65dc6eab16410deb741797918df58348d7a0d04 (diff)
restructure
Diffstat (limited to 'src/vecmath.c')
-rw-r--r--src/vecmath.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/vecmath.c b/src/vecmath.c
new file mode 100644
index 0000000..8f1c43d
--- /dev/null
+++ b/src/vecmath.c
@@ -0,0 +1,48 @@
+#include "vecmath.h"
+
+#include <math.h>
+#include <stdio.h>
+
+Vec3f vec3f_sub(Vec3f lhs, Vec3f rhs) {
+ return (Vec3f){
+ .x = lhs.x - rhs.x,
+ .y = lhs.y - rhs.y,
+ .z = lhs.z - rhs.z,
+ };
+}
+
+Vec3f vec3f_add(Vec3f lhs, Vec3f rhs) {
+ return (Vec3f){
+ .x = lhs.x + rhs.x,
+ .y = lhs.y + rhs.y,
+ .z = lhs.z + rhs.z,
+ };
+}
+
+float vec3f_dot(Vec3f lhs, Vec3f rhs) {
+ return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
+}
+
+Vec3f vec3f_neg(Vec3f v) {
+ return (Vec3f){-v.x, -v.y, -v.z};
+}
+
+Vec3f vec3f_normalize(Vec3f vec) {
+ float len = sqrt(vec3f_dot(vec, vec));
+ return (Vec3f){vec.x/len, vec.y/len, vec.z/len};
+}
+
+Vec3f vec3f_mul(float a, Vec3f v) {
+ return (Vec3f){v.x * a, v.y * a, v.z * a};
+}
+
+Color icolor(int32_t rgb) {
+ int r = (rgb >> 16) & 0xff;
+ int g = (rgb >> 8) & 0xff;
+ int b = rgb & 0xff;
+ return (Color){.r = r/255.0, .g = g/255.0, .b = b/255.0};
+}
+
+void vec3f_show(const char *name, Vec3f v) {
+ printf("%s(%f,%f,%f)\n", name, v.x, v.y, v.z);
+}