I'm writing a little math module in C to handle vectors and matrices. This will be column-major style but right now I've only finished the basics of the vector functions and wanted some feedback on optimization tips, naming conventions, etc. This is C so no real function overloading. I would like to keep it C style.
vec3f.h
#include "stdlib.h"
#include "math.h"
const double m_PI = 3.14159265359;
typedef struct vec3f
{
double p[3];
}vec3f;
vec3f* vec3fx(double x, double y, double z);
vec3f* vec3fv(vec3f* v3);
vec3f vec3fs(double x, double y, double z);
void vec3fadd(vec3f* out, struct vec3f* a, struct vec3f* b);
void vec3fsub(vec3f* out, struct vec3f* a, struct vec3f* b);
void vec3fmul(vec3f* out, struct vec3f* a, struct vec3f* b);
void vec3fnegate(vec3f* out);
void vec3fscale(vec3f* out, double a);
void vec3fscaleu(vec3f* out, double x, double y, double z);
void vec3fscalex(vec3f* out, double a);
void vec3fscaley(vec3f* out, double a);
void vec3fscalez(vec3f* out, double a);
void vec3flen(double* out, vec3f* in);
void toDegrees(double* out, double angleInRadians);
void toRadians(double* out, double angleInDegrees);
void vec3fdot(double* out, vec3f* a, vec3f* b);
void vec3fcross(vec3f* out, vec3f* a, vec3f* b);
void vec3fnorm(vec3f* out, vec3f* in);
void vec3fdist(double* out, vec3f* a, vec3f* b);
void vec3fangle(double* out, vec3f* a, vec3f* b);
vec3f.c
#include "vec3f.h"
vec3f* vec3fx(double x, double y, double z)
{
vec3f* v = (vec3f*)malloc(sizeof(vec3f));
v->p[0] = x;
v->p[1] = y;
v->p[2] = z;
return v;
}
vec3f* vec3fv(vec3f* v3)
{
vec3f* v = (vec3f*)malloc(sizeof(vec3f));
v->p[0] = v3->p[0];
v->p[1] = v3->p[1];
v->p[2] = v3->p[2];
return v;
}
void vec3fadd(vec3f* out, vec3f* a, vec3f* b)
{
out->p[0] = a->p[0] + b->p[0];
out->p[1] = a->p[1] + b->p[1];
out->p[2] = a->p[2] + b->p[2];
}
void vec3fsub(vec3f* out, vec3f* a, vec3f* b)
{
out->p[0] = a->p[0] - b->p[0];
out->p[1] = a->p[1] - b->p[1];
out->p[2] = a->p[2] - b->p[2];
}
void vec3fmul(vec3f* out, vec3f* a, vec3f* b)
{
out->p[0] = a->p[0] * b->p[0];
out->p[1] = a->p[1] * b->p[1];
out->p[2] = a->p[2] * b->p[2];
}
void vec3fnegate(vec3f* out)
{
out->p[0] = -out->p[0];
out->p[1] = -out->p[1];
out->p[2] = -out->p[2];
}
void vec3fscale(vec3f* out, double a)
{
out->p[0] *= a;
out->p[1] *= a;
out->p[2] *= a;
}
void vec3fscaleu(vec3f* out, double x, double y, double z)
{
out->p[0] *= x;
out->p[1] *= y;
out->p[2] *= z;
}
void vec3fscalex(vec3f* out, double a)
{
out->p[0] *= a;
}
void vec3fscaley(vec3f* out, double a)
{
out->p[1] *= a;
}
void vec3fscalez(vec3f* out, double a)
{
out->p[2] *= a;
}
void vec3flen(double* out, vec3f* in)
{
double res = 0;
res += in->p[0] * in->p[0];
res += in->p[1] * in->p[1];
res += in->p[2] * in->p[2];
*out = sqrt(res);
}
void toDegrees(double* out, double angleInRadians)
{
*out = angleInRadians * (double)(180.0/m_PI);
}
void toRadians(double* out, double angleInDegrees)
{
*out = angleInDegrees * (double)(m_PI/180.0);
}
void vec3fdot(double* out, vec3f* a, vec3f* b)
{
double total = 0;
total += a->p[0] * b->p[0];
total += a->p[1] * b->p[1];
total += a->p[2] * b->p[2];
*out = total;
}
void vec3fcross(vec3f* out, vec3f* a, vec3f* b)
{
out->p[0] = a->p[1] * b->p[2] - b->p[1] * a->p[2];
out->p[1] = a->p[2] * b->p[0] - b->p[2] * a->p[0];
out->p[2] = a->p[0] * b->p[1] - b->p[0] * a->p[1];
}
void vec3fnorm(vec3f* out, vec3f* in)
{
double t = 0;
vec3flen(&t, in);
out->p[0] = in->p[0]/t;
out->p[1] = in->p[1]/t;
out->p[2] = in->p[2]/t;
}
void vec3fdist(double* out, vec3f* a, vec3f*b)
{
vec3f tmp;
vec3fsub(&tmp, b, a);
vec3flen(out, &tmp);
}
void vec3fangle(double* out, vec3f* a, vec3f* b)
{
double tmp;
vec3fdot(&tmp, a, b);
*out = acos(tmp);
}