Created
February 11, 2018 02:55
-
-
Save saidm00/ff228b431f40c2614e47a18548ec89ce to your computer and use it in GitHub Desktop.
vector/quaternion math library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CQUAT_H | |
#define CQUAT_H | |
#include <complex.h> | |
#define sqr(x) x*x | |
typedef struct { | |
float x,y; | |
} vec2; | |
typedef struct { | |
float x,y,z; | |
} vec3; | |
typedef struct { | |
float x, y, z, w; | |
} vec4; | |
vec2 add_vec2(vec2 v1, vec2 v2) { | |
return (vec2){v1.x + v2.x, v1.y + v2.y}; | |
} | |
vec3 add_vec3(vec3 v1, vec3 v2) { | |
return (vec3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z}; | |
} | |
vec4 add_vec4(vec4 v1, vec4 v2) { | |
return (vec4){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w}; | |
} | |
vec2 subtract_vec2(vec2 v1, vec2 v2) { | |
return (vec2){v1.x - v2.x, v1.y - v2.y}; | |
} | |
vec3 subtract_vec3(vec3 v1, vec3 v2) { | |
return (vec3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z}; | |
} | |
vec4 subtract_vec4(vec4 v1, vec4 v2) { | |
return (vec4){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w}; | |
} | |
vec2 mul_vec2(vec2 v, float s) { | |
return (vec2){v.x * s, v.y * s}; | |
} | |
vec3 mul_vec3(vec3 v, float s) { | |
return (vec3){v.x * s, v.y * s, v.z * s}; | |
} | |
vec4 mul_vec4(vec4 v, float s) { | |
return (vec4){v.x * s, v.y * s, v.z * s, v.w * s}; | |
} | |
vec2 div_vec2(vec2 v, float s) { | |
return (vec2){v.x / s, v.y / s}; | |
} | |
vec3 div_vec3(vec3 v, float s) { | |
return (vec3){v.x / s, v.y / s, v.z / s}; | |
} | |
vec4 div_vec4(vec4 v, float s) { | |
return (vec4){v.x / s, v.y / s, v.z / s, v.w / s}; | |
} | |
float length_vec2(const vec2* v) { | |
return sqrtf(sqr(v->x) + sqr(v->y)); | |
} | |
float length_vec3(const vec3* v) { | |
return sqrtf(sqr(v->x) + sqr(v->y) + sqr(v->z)); | |
} | |
float length_vec4(const vec4* v) { | |
return sqrtf(sqr(v->x) + sqr(v->y) + sqr(v->z) + sqr(v->w)); | |
} | |
void normalize_vec2(vec2* v) { | |
float l = length_vec2(v); | |
v->x/=l, v->y/=l; | |
} | |
void normalize_vec3(vec3* v) { | |
float l = length_vec3(v); | |
v->x/=l, v->y/=l, v->z/=l; | |
} | |
void normalize_vec4(vec4* v) { | |
float l = length_vec4(v); | |
v->x/=l, v->y/=l, v->z/=l, v->w/=l; | |
} | |
void rotate_vec2(vec2* v, float theta) { | |
float complex v0 = v->x + v->y * I; | |
float complex rot = cosf(theta) + sinf(theta) * I; | |
float complex v1 = rot*v0; | |
v->x = crealf(v1), v->y = cimagf(v1); | |
} | |
void rotate_vec3(vec3* v, float theta) { | |
} | |
vec4 quat_mult(vec4 q1, vec4 q2) { | |
return (vec4) {q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x, | |
-q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y, | |
q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z, | |
-q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w}; | |
} | |
vec4 quat_conjugate(vec4 q) { | |
return (vec4) {-q.x, -q.y, -q.z, q.w}; | |
} | |
#endif //CQUAT_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment