diff options
| author | hybrid <hybrid@hybridlabs.pro> | 2026-08-08 08:10:17 +0300 |
|---|---|---|
| committer | hybrid <hybrid@hybridlabs.pro> | 2026-08-08 08:10:17 +0300 |
| commit | 1d1796c97867ada2211a39b313ef6398297c7b8f (patch) | |
| tree | 927e9c477d7a974fb219ed9963fced6ea8109b5c /include/vector.h | |
| parent | 884bac2a767bf5bf77413c055a6484a7ea87700c (diff) | |
| download | a3catragmx-1d1796c97867ada2211a39b313ef6398297c7b8f.tar.gz a3catragmx-1d1796c97867ada2211a39b313ef6398297c7b8f.tar.bz2 a3catragmx-1d1796c97867ada2211a39b313ef6398297c7b8f.zip | |
upd: tmp
Diffstat (limited to 'include/vector.h')
| -rw-r--r-- | include/vector.h | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/include/vector.h b/include/vector.h index 72e6523..07d44cd 100644 --- a/include/vector.h +++ b/include/vector.h @@ -1,9 +1,42 @@ #pragma once -#include "vector.h" + +#include <math.h> typedef struct { double x, y, z; } Vec3; -static inline double vec3_magnitude(Vec3 v); -static inline Vec3 vec3_diff(Vec3 a, Vec3 b); -static inline Vec3 vec3_normalize(Vec3 v); -static inline Vec3 vec3_mul(Vec3 v, double n); +static inline double vec3_magnitude(Vec3 v) { + return hypot(v.x, hypot(v.y, v.z)); +} + +static inline Vec3 vec3_diff(Vec3 a, Vec3 b) { + return (Vec3){.x = a.x - b.x, + .y = a.y - b.y, + .z = a.z - b.z}; +} + +static inline Vec3 vec3_normalize(Vec3 v) { + if (v.x == 0.0 || v.y == 0.0 || v.z == 0.0) + return (Vec3){.x = 0.0, + .y = 0.0, + .z = 0.0}; + + double len = sqrt(v.x * v.x + + v.y * v.y + + v.z * v.z); + return (Vec3){.x = v.x / len, + .y = v.y / len, + .z = v.z / len}; +} + +static inline Vec3 vec3_mul(Vec3 v, double n) { + return (Vec3) {.x = v.x * n, + .y = v.y * n, + .z = v.z * n}; +} + +static inline Vec3 vec3_add(Vec3 a, Vec3 b) { + return (Vec3) {.x = a.x + b.x, + .y = a.y + b.y, + .z = a.z + b.z}; +} + |
