From 5e70354ec23e11ecf7325fe4833778978968c6cb Mon Sep 17 00:00:00 2001 From: Leo Sulzbacher Date: Sat, 14 Mar 2026 17:25:54 +0100 Subject: [PATCH] feat: add common vec2 and vec3 operations --- .gitea/workflows/ci.yml | 2 +- include/curtle/vector.h | 19 ++++++++--- src/vector.c | 71 +++++++++++++++++++++++++++++++++++++++-- tests/test_vector.c | 10 +++--- 4 files changed, 90 insertions(+), 12 deletions(-) diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index cd23a60..45025d2 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -1,6 +1,6 @@ name: CI -on: [push, pull_request] +on: [ push, pull_request ] jobs: build: diff --git a/include/curtle/vector.h b/include/curtle/vector.h index 02f6109..f320c4d 100644 --- a/include/curtle/vector.h +++ b/include/curtle/vector.h @@ -1,12 +1,23 @@ #ifndef CURTLE_VECTOR_H #define CURTLE_VECTOR_H -struct vec2 -{ - double x; - double y; +struct vec2 { + float x; + float y; +}; + +struct vec3 { + float x; + float y; + float z; }; struct vec2 vec2_add(struct vec2 a, struct vec2 b); +struct vec2 vec2_sub(struct vec2 a, struct vec2 b); + +float vec2_dot(struct vec2 a, struct vec2 b); + +float vec2_det(struct vec2 a, struct vec2 b); + #endif \ No newline at end of file diff --git a/src/vector.c b/src/vector.c index 8536259..dd8b221 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,6 +1,73 @@ #include "curtle/vector.h" +#include -struct vec2 vec2_add(struct vec2 a, struct vec2 b) +struct vec2 vec2_add(const struct vec2 a, const struct vec2 b) { - return (struct vec2){a.x + b.x, a.y + b.y}; + return (struct vec2){ a.x + b.x, a.y + b.y }; +} + +struct vec2 vec2_sub(const struct vec2 a, const struct vec2 b) +{ + return (struct vec2){ a.x - b.x, a.y - b.y }; +} + +float vec2_dot(const struct vec2 a, const struct vec2 b) +{ + return a.x * b.x + a.y * b.y; +} + +float vec2_det(const struct vec2 a, const struct vec2 b) +{ + return a.x * b.y - a.y * b.x; +} + +struct vec2 vec2_scale(const struct vec2 v, const float s) +{ + return (struct vec2){ v.x * s, v.y * s }; +} + +float vec2_len(const struct vec2 v) +{ + return sqrtf(v.x * v.x + v.y * v.y); +} + +struct vec2 vec2_normalize(const struct vec2 v) +{ + return vec2_scale(v, 1.0f / vec2_len(v)); +} + +struct vec3 vec3_add(const struct vec3 a, const struct vec3 b) +{ + return (struct vec3){ a.x + b.x, a.y + b.y, a.z + b.z }; +} + +struct vec3 vec3_sub(const struct vec3 a, const struct vec3 b) +{ + return (struct vec3){ a.x - b.x, a.y - b.y, a.z - b.z }; +} + +float vec3_dot(const struct vec3 a, const struct vec3 b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +struct vec3 vec3_cross(const struct vec3 a, const struct vec3 b) +{ + return (struct 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 }; +} + +struct vec3 vec3_scale(const struct vec3 v, const float s) +{ + return (struct vec3){ v.x * s, v.y * s, v.z * s }; +} + +float vec3_len(const struct vec3 v) +{ + return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z); +} + +struct vec3 vec3_normalize(const struct vec3 v) +{ + return vec3_scale(v, 1.0f / vec3_len(v)); } \ No newline at end of file diff --git a/tests/test_vector.c b/tests/test_vector.c index b0341da..8e8e69c 100644 --- a/tests/test_vector.c +++ b/tests/test_vector.c @@ -3,11 +3,11 @@ int main() { - struct vec2 a = {1, 2}; - struct vec2 b = {3, 4}; + struct vec2 a = { 1, 2 }; + struct vec2 b = { 3, 4 }; - struct vec2 r = vec2_add(a, b); + struct vec2 r = vec2_add(a, b); - assert(r.x == 4); - assert(r.y == 6); + assert(r.x == 4); + assert(r.y == 6); } \ No newline at end of file