aboutsummaryrefslogtreecommitdiff
path: root/vector3.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-01-28 22:16:25 +0000
committerrodri <rgl@antares-labs.eu>2023-01-28 22:16:25 +0000
commit983fae140a1e162d947eab8b8a0d9acba291d3a8 (patch)
treeef7a0de9a51ebbfd086e9293d2a22e1da387ee2d /vector3.c
downloadthreedee-983fae140a1e162d947eab8b8a0d9acba291d3a8.tar.gz
threedee-983fae140a1e162d947eab8b8a0d9acba291d3a8.tar.bz2
threedee-983fae140a1e162d947eab8b8a0d9acba291d3a8.zip
initial public release.HEADfront
Diffstat (limited to 'vector3.c')
-rw-r--r--vector3.c59
1 files changed, 59 insertions, 0 deletions
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 <u.h>
+#include <libc.h>
+#include <draw.h>
+#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;
+}