docs(vector3): start commenting vector3.h

This commit is contained in:
2025-03-15 19:34:29 +01:00
committed by Loïc GUEZO
parent 10c5f7164d
commit 1cd95b0fbd
2 changed files with 21 additions and 5 deletions

View File

@@ -5,6 +5,14 @@ typedef struct {
float x, y, z; float x, y, z;
} Vec3; } Vec3;
/**
* @brief Creates a new 3D vector.
* @param x X-axis coordinate.
* @param y Y-axis coordinate.
* @param z Z-axis coordinate.
* @return A pointer to the newly allocated 3D vector if
* successful, or NULL if the allocation fails.
*/
Vec3* vec3(float x, float y, float z); Vec3* vec3(float x, float y, float z);
Vec3* vec3Add(Vec3* v1, Vec3* v2); Vec3* vec3Add(Vec3* v1, Vec3* v2);

View File

@@ -58,6 +58,7 @@ Vec3* vec3Norm(Vec3* v)
Vec3* vec3Lerp(Vec3* a, Vec3* b, float t) Vec3* vec3Lerp(Vec3* a, Vec3* b, float t)
{ {
if (!a || !b) return NULL;
if (t < 0.f) t = 0.f; if (t < 0.f) t = 0.f;
if (t > 1.f) t = 1.f; if (t > 1.f) t = 1.f;
@@ -70,6 +71,7 @@ Vec3* vec3Lerp(Vec3* a, Vec3* b, float t)
Vec3* vec3Cross(Vec3* a, Vec3* b) Vec3* vec3Cross(Vec3* a, Vec3* b)
{ {
if (!a || !b) return NULL;
return vec3( return vec3(
a->y * b->z - a->z * b->y, a->y * b->z - a->z * b->y,
a->z * b->x - a->x * b->z, a->z * b->x - a->x * b->z,
@@ -79,9 +81,10 @@ Vec3* vec3Cross(Vec3* a, Vec3* b)
float vec3Angle(Vec3* a, Vec3* b) float vec3Angle(Vec3* a, Vec3* b)
{ {
float dot = vec3Dot(a, b);
float lenA = vec3Len(a); float lenA = vec3Len(a);
float lenB = vec3Len(b); float lenB = vec3Len(b);
if (!lenA || !lenB) return 0.f;
float dot = vec3Dot(a, b);
return acosf(dot / (lenA * lenB)); return acosf(dot / (lenA * lenB));
} }
@@ -106,6 +109,7 @@ Vec3* vec3Refl(Vec3* v, Vec3* normal)
float vec3Dist(Vec3* a, Vec3* b) float vec3Dist(Vec3* a, Vec3* b)
{ {
if (!a || !b) return 0;
Vec3* vsub = vec3Sub(a, b); Vec3* vsub = vec3Sub(a, b);
float rlt = vec3Len(vsub); float rlt = vec3Len(vsub);
vec3Free(vsub); vec3Free(vsub);
@@ -114,18 +118,22 @@ float vec3Dist(Vec3* a, Vec3* b)
Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle) Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle)
{ {
axis = vec3Norm(axis); if (!v || !axis) return NULL;
float dot = vec3Dot(axis, v); Vec3* normAxis = vec3Norm(axis);
Vec3* cross = vec3Cross(axis, v); if (!normAxis) return NULL;
float dot = vec3Dot(normAxis, v);
Vec3* cross = vec3Cross(normAxis, v);
Vec3* vscal = vec3Scale(v, cosf(angle)); Vec3* vscal = vec3Scale(v, cosf(angle));
Vec3* cscal = vec3Scale(cross, sinf(angle)); Vec3* cscal = vec3Scale(cross, sinf(angle));
Vec3* add = vec3Add(vscal, cscal); Vec3* add = vec3Add(vscal, cscal);
Vec3* dscal = vec3Scale(axis, dot * (1 - cosf(angle))); Vec3* dscal = vec3Scale(normAxis, dot * (1 - cosf(angle)));
Vec3* rlt = vec3Add(add, dscal); Vec3* rlt = vec3Add(add, dscal);
vec3Free(normAxis);
vec3Free(cross); vec3Free(cross);
vec3Free(vscal); vec3Free(vscal);
vec3Free(cscal); vec3Free(cscal);