From 5b5b4c31fc81fec5b420f7f13816af07712e7309 Mon Sep 17 00:00:00 2001
From: Sara <sara@saragerretsen.nl>
Date: Sat, 23 Sep 2023 22:52:23 +0200
Subject: [PATCH] Added vector constants and floating point vector math
 functions

---
 src/vmath.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/src/vmath.h b/src/vmath.h
index b445315..838bffd 100644
--- a/src/vmath.h
+++ b/src/vmath.h
@@ -1,6 +1,7 @@
 #ifndef _fencer_vmath_h
 #define _fencer_vmath_h
 
+#include <math.h>
 typedef struct Vector {
     float x;
     float y;
@@ -11,4 +12,71 @@ typedef struct IVector {
     int y;
 } IVector;
 
+// Vector Constant Macros
+#define ZeroVector (Vector){0.0f, 0.0f}
+#define InfinityVector (Vector){INFINITY, INFINITY}
+#define OneVector (Vector){1.0f,1.0f}
+
+#define UpVector (Vector){0.0f,-1.0f}
+#define RightVector (Vector){1.0f,0.0f}
+#define LeftVector (Vector){-1.0f,0.0f}
+#define DownVector (Vector){0.0f,1.0f}
+
+// Integer Vector Constant Macros
+#define ZeroIVector (IVector){0,0}
+#define OneIVector (IVector){1,1}
+
+#define UpIVector (IVector){-1,0}
+#define DownIVector (IVector){1,0}
+#define RightIVector (IVector){1,0}
+#define LeftIVector (IVector){-1,0}
+
+///
+// Floating point vector maths functions.
+///
+static inline
+Vector vaddf(Vector a, Vector b) {
+    return (Vector){a.x + b.x, a.y + b.y};
+}
+static inline
+Vector vsubf(Vector a, Vector b) {
+    return (Vector){a.x - b.x, a.y - b.y};
+}
+static inline
+Vector vmulf(Vector a, float b) {
+    return (Vector){a.x * b, a.y * b};
+}
+static inline
+Vector vinvf(Vector a) {
+    return (Vector){-a.x, -a.y};
+}
+static inline
+Vector vperpendicularf(Vector a) {
+    return (Vector){a.y, -a.x};
+}
+static inline
+float vmagnitudef(Vector a) {
+    return sqrtf(a.x*a.x + a.y*a.y);
+}
+static inline
+float vsqrmagnitudef(Vector a) {
+    return a.x*a.x + a.y*a.y;
+}
+static inline
+Vector vnormalizedf(Vector a) {
+    return vmulf(a, 1.0/vmagnitudef(a));
+}
+static inline
+float vdotf(Vector a, Vector b) {
+    return (a.x*b.x) + (a.y*b.y);
+}
+static inline
+float vdistancef(Vector a, Vector b) {
+    return vmagnitudef(vsubf(a, b));
+}
+static inline
+float vsqrdistf(Vector a, Vector b) {
+    return vsqrmagnitudef(vsubf(a, b));
+}
+
 #endif // !_fencer_vmath_h