aboutsummaryrefslogtreecommitdiff
path: root/09-september/tomcat/math/vector.c
diff options
context:
space:
mode:
authorThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:02:32 +0100
committerThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:02:32 +0100
commit6b8af9cf83851c075c6c9514b1deaa931c2b19a4 (patch)
tree428986b49c32e21d3f7a3c2dfa41858ae0153209 /09-september/tomcat/math/vector.c
Initial commit
Diffstat (limited to '09-september/tomcat/math/vector.c')
-rw-r--r--09-september/tomcat/math/vector.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/09-september/tomcat/math/vector.c b/09-september/tomcat/math/vector.c
new file mode 100644
index 0000000..230de51
--- /dev/null
+++ b/09-september/tomcat/math/vector.c
@@ -0,0 +1,177 @@
+#include "vector.h"
+#include <SDL2/SDL.h>
+
+Vec4 vec4_add(const Vec4 *a, const Vec4 *b)
+{
+ Vec4 c;
+ c.x = a->x + b->x;
+ c.y = a->y + b->y;
+ c.z = a->z + b->z;
+ c.w = a->w + b->w;
+ return c;
+}
+
+Vec4 vec4_sub(const Vec4 *a, const Vec4 *b)
+{
+ Vec4 c;
+ c.x = a->x - b->x;
+ c.y = a->y - b->y;
+ c.z = a->z - b->z;
+ c.w = a->w - b->w;
+ return c;
+}
+
+Vec4 vec4_scalar_mul(const Vec4 *a, GLfloat scalar)
+{
+ Vec4 c;
+ c.x = a->x * scalar;
+ c.y = a->y * scalar;
+ c.z = a->z * scalar;
+ c.w = a->w * scalar;
+ return c;
+}
+
+GLfloat vec4_dot_mul(const Vec4 *a, const Vec4 *b)
+{
+ return ( (a->x * b->x) + (a->y * b->y) + (a->z * b->z) + (a->w * b->w) );
+}
+
+GLfloat vec4_length(Vec4 *a)
+{
+ return SDL_sqrtf(SDL_pow(a->x, 2.0f) + SDL_pow(a->y, 2.0f) + SDL_pow(a->z, 2.0f) + SDL_pow(a->w, 2.0f) );
+}
+
+GLfloat vec4_length2(Vec4 *a)
+{
+ return ( SDL_pow(a->x, 2.0f) + SDL_pow(a->y, 2.0f) + SDL_pow(a->z, 2.0f) + SDL_pow(a->w, 2.0f) );
+}
+
+Vec4 vec4_normalize(Vec4 *a)
+{
+ Vec4 b;
+ GLfloat length = vec4_length(a);
+ b.x = a->x / length;
+ b.y = a->y / length;
+ b.z = a->z / length;
+ b.w = a->w / length;
+ return b;
+}
+
+Vec3 vec3_add(const Vec3 *a, const Vec3 *b)
+{
+ Vec3 c;
+ c.x = a->x + b->x;
+ c.y = a->y + b->y;
+ c.z = a->z + b->z;
+ return c;
+}
+
+Vec3 vec3_sub(const Vec3 *a, const Vec3 *b)
+{
+ Vec3 c;
+ c.x = a->x - b->x;
+ c.y = a->y - b->y;
+ c.z = a->z - b->z;
+ return c;
+}
+
+Vec3 vec3_scalar_mul(const Vec3 *a, GLfloat scalar)
+{
+ Vec3 c;
+ c.x = a->x * scalar;
+ c.y = a->y * scalar;
+ c.z = a->z * scalar;
+ return c;
+}
+
+GLfloat vec3_dot_mul(const Vec3 *a, const Vec3 *b)
+{
+ return ( (a->x * b->x) + (a->y * b->y) + (a->z * b->z) );
+}
+
+
+Vec3 vec3_cross_mul(const Vec3 *a, const Vec3 *b)
+{
+ Vec3 c;
+ c.x = (a->y * b->z) - (a->z * b->y);
+ c.y = (a->z * b->x) - (a->x * b->z);
+ c.z = (a->x * b->y) - (a->y * b->x);
+ return c;
+}
+
+GLfloat vec3_length(Vec3 *a)
+{
+ return SDL_sqrtf(SDL_pow(a->x, 2.0f) + SDL_pow(a->y, 2.0f) + SDL_pow(a->z, 2.0f));
+}
+
+GLfloat vec3_length2(Vec3 *a)
+{
+ return ( SDL_pow(a->x, 2.0f) + SDL_pow(a->y, 2.0f) + SDL_pow(a->z, 2.0f) );
+}
+
+Vec3 vec3_normalize(Vec3 *a)
+{
+ Vec3 b;
+ GLfloat length = vec3_length(a);
+ b.x = a->x / length;
+ b.y = a->y / length;
+ b.z = a->z / length;
+ return b;
+}
+
+Vec2 vec2_add(const Vec2 *a, const Vec2 *b)
+{
+ Vec2 c;
+ c.x = a->x + b->x;
+ c.y = a->y + b->y;
+ return c;
+}
+
+Vec2 vec2_sub(const Vec2 *a, const Vec2 *b)
+{
+ Vec2 c;
+ c.x = a->x - b->x;
+ c.y = a->y - b->y;
+ return c;
+}
+
+Vec2 vec2_scalar_mul(const Vec2 *a, GLfloat scalar)
+{
+ Vec2 c;
+ c.x = a->x * scalar;
+ c.y = a->y * scalar;
+ return c;
+}
+
+GLfloat vec2_dot_mul(const Vec2 *a, const Vec2 *b)
+{
+ return ( (a->x * b->x) + (a->y * b->y) );
+}
+
+
+Vec2 vec2_cross_mul(const Vec2 *a, const Vec2 *b)
+{
+ Vec2 c;
+ c.x = (a->x * b->y) - (a->y * b->x);
+ c.y = (a->y * b->x) - (a->x * b->y);
+ return c;
+}
+
+GLfloat vec2_length(Vec2 *a)
+{
+ return SDL_sqrtf(SDL_pow(a->x, 2.0f) + SDL_pow(a->y, 2.0f) );
+}
+
+GLfloat vec2_length2(Vec2 *a)
+{
+ return ( SDL_pow(a->x, 2.0f) + SDL_pow(a->y, 2.0f) );
+}
+
+Vec2 vec2_normalize(Vec2 *a)
+{
+ Vec2 b;
+ GLfloat length = vec2_length(a);
+ b.x = a->x / length;
+ b.y = a->y / length;
+ return b;
+}