vector math

This commit is contained in:
Tyrel Souza 2024-04-21 23:02:39 -04:00
parent f8d67c57e3
commit 648d1662c4
3 changed files with 102 additions and 7 deletions

View File

@ -78,7 +78,7 @@ void update(void) {
mesh.rotation.x += .01f;
mesh.rotation.y += .01f;
mesh.rotation.z += .01f;
mesh.rotation.z += .02f;
int num_faces = array_length(mesh.faces);
for (int i = 0; i < num_faces; i++) {

View File

@ -1,7 +1,45 @@
#include <math.h>
#include "vector.h"
// Implement all vector functons
////////////////////////////////////////////////////////////////////
/// Vec 2d
////////////////////////////////////////////////////////////////////
float vec2_length(vec2_t v) {
return sqrt(v.x * v.x + v.y *v.y);
}
vec2_t vec2_add(vec2_t a, vec2_t b) {
vec2_t result = {
.x = a.x + b.x,
.y = a.y + b.y,
};
return result;
}
vec2_t vec2_sub(vec2_t a, vec2_t b) {
vec2_t result = {
.x = a.x - b.x,
.y = a.y - b.y,
};
return result;
}
vec2_t vec2_mul(vec2_t a, float factor) {
vec2_t result = {
.x = a.x * factor,
.y = a.y * factor,
};
return result;
}
vec2_t vec2_div(vec2_t a, float factor){
vec2_t result = {
.x = a.x / factor,
.y = a.y / factor,
};
return result;
}
////////////////////////////////////////////////////////////////////
// Vec 3d
////////////////////////////////////////////////////////////////////
vec3_t vec3_rotate_x(vec3_t v, float angle){
vec3_t rotated_vector = {
.x = v.x,
@ -26,3 +64,42 @@ vec3_t vec3_rotate_z(vec3_t v, float angle) {
};
return rotated_vector;
}
float vec3_length(vec3_t v) {
return sqrt(v.x * v.x + v.y *v.y + v.z *v.z);
}
vec3_t vec3_add(vec3_t a, vec3_t b) {
vec3_t result = {
.x = a.x + b.x,
.y = a.y + b.y,
.z = a.z + b.z,
};
return result;
}
vec3_t vec3_sub(vec3_t a, vec3_t b) {
vec3_t result = {
.x = a.x - b.x,
.y = a.y - b.y,
.z = a.z - b.z,
};
return result;
}
vec3_t vec3_mul(vec3_t a, float factor) {
vec3_t result = {
.x = a.x * factor,
.y = a.y * factor,
.z = a.z * factor,
};
return result;
}
vec3_t vec3_div(vec3_t a, float factor){
vec3_t result = {
.x = a.x / factor,
.y = a.y / factor,
.z = a.z / factor,
};
return result;
}

View File

@ -2,20 +2,38 @@
#define VECTOR_H
typedef struct {
float x;
float y;
float x, y;
} vec2_t;
typedef struct {
float x;
float y;
float z;
float x, y, z;
} vec3_t;
/////
// Vec 2d
/////
float vec2_length(vec2_t v);
vec2_t vec2_add(vec2_t a, vec2_t b);
vec2_t vec2_sub(vec2_t a, vec2_t b);
vec2_t vec2_mul(vec2_t a, float factor);
vec2_t vec2_div(vec2_t a, float factor);
/////
// Vec 3d
/////
vec3_t vec3_rotate_x(vec3_t v, float angle);
vec3_t vec3_rotate_y(vec3_t v, float angle);
vec3_t vec3_rotate_z(vec3_t v, float angle);
float vec3_length(vec3_t v);
vec3_t vec3_add(vec3_t a, vec3_t b);
vec3_t vec3_sub(vec3_t a, vec3_t b);
vec3_t vec3_mul(vec3_t a, float factor);
vec3_t vec3_div(vec3_t a, float factor);
#endif