defocus-modules/lib/df_math.c
2023-07-12 22:57:49 +02:00

31 lines
1.8 KiB
C
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <defocus/df_math.h>
#include <math.h>
DF_API df_v3 df_normalize_v3(df_v3 v)
{
float l = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
return (df_v3){v.x / l, v.y / l, v.z / l};
}
DF_API float df_len_v3(df_v3 v) { return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z); }
DF_API df_v3 df_cross(df_v3 a, df_v3 b)
{
return (df_v3){
.x = a.y * b.z - a.z * b.y,
.y = a.z * b.x - a.x * b.z,
.z = a.x * b.y - a.y * b.x,
};
}
DF_API df_v3 df_mul_v3(float s, df_v3 v) { return (df_v3){s * v.x, s * v.y, s * v.z}; }
DF_API df_v3 df_add_v3(df_v3 a, df_v3 b) { return (df_v3){a.x + b.x, a.y + b.y, a.z + b.z}; }
DF_API df_v3 df_sub_v3(df_v3 a, df_v3 b) { return (df_v3){a.x - b.x, a.y - b.y, a.z - b.z}; }
DF_API float df_dot_v3(df_v3 a, df_v3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
DF_API float df_len_v2(df_v2 v) { return sqrtf(v.x * v.x + v.y * v.y); }