1
0
Fork 0
tsgames/build/assets/lib/vec2.c

52 lines
899 B
C

#include "vec2.h"
#include <math.h>
vec2 vec2_add(vec2 a, vec2 b) {
return (vec2){
.x = a.x + b.x,
.y = a.y + b.y,
};
}
vec2 vec2_sub(vec2 a, vec2 b) {
return (vec2){
.x = a.x - b.x,
.y = a.y - b.y,
};
}
float vec2_dot(vec2 a, vec2 b) {
return a.x * b.x + a.y * b.y;
}
vec2 vec2_mul(vec2 v, float f) {
return (vec2){
.x = v.x * f,
.y = v.y * f,
};
}
vec2 vec2_div(vec2 v, float f) {
return (vec2){
.x = v.x / f,
.y = v.y / f,
};
}
float vec2_mag(vec2 v) {
return sqrtf(v.x * v.x + v.y * v.y);
}
vec2 vec2_normalize(vec2 x) {
float len = vec2_mag(x);
return vec2_div(x, len);
}
vec2 vec2_lerp(vec2 a, vec2 b, float t) {
return vec2_add(a, vec2_mul(vec2_sub(b, a), t));
}
vec2 vec2_reflect(vec2 x, vec2 n) {
return vec2_sub(x, vec2_mul(vec2_mul(n, vec2_dot(x, n)), 2));
}