From 983fae140a1e162d947eab8b8a0d9acba291d3a8 Mon Sep 17 00:00:00 2001 From: rodri Date: Sat, 28 Jan 2023 22:16:25 +0000 Subject: initial public release. --- vector3.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 vector3.c (limited to 'vector3.c') diff --git a/vector3.c b/vector3.c new file mode 100644 index 0000000..933716e --- /dev/null +++ b/vector3.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include "dat.h" +#include "fns.h" + +Vector3 +Vec3(double x, double y, double z) +{ + return (Vector3){x, y, z}; +} + +Vector3 +addvec3(Vector3 v, Vector3 u) +{ + return (Vector3){v.x+u.x, v.y+u.y, v.z+u.z}; +} + +Vector3 +subvec3(Vector3 v, Vector3 u) +{ + return (Vector3){v.x-u.x, v.y-u.y, v.z-u.z}; +} + +Vector3 +mulvec3(Vector3 v, double s) +{ + return (Vector3){v.x*s, v.y*s, v.z*s}; +} + +double +dotvec3(Vector3 v, Vector3 u) +{ + return v.x*u.x + v.y*u.y + v.z*u.z; +} + +Vector3 +crossvec(Vector3 v, Vector3 u) +{ + return (Vector3){ + v.y*u.z - v.z*u.y, + v.z*u.x - v.x*u.z, + v.x*u.y - v.y*u.x + }; +} + +Vector3 +normvec3(Vector3 v) +{ + double len; + + len = hypot3(v.x, v.y, v.z); + if(len == 0) + return (Vector3){0, 0, 0}; + v.x /= len; + v.y /= len; + v.z /= len; + return v; +} -- cgit v1.2.3