From 37ee18c0e604f659b5ddc17e6b9e72d2365333a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Fri, 16 May 2025 13:49:16 +0200 Subject: [PATCH] fix(vectors): edited functions name --- src/main.c | 9 ++++++- src/math/vector3.c | 63 +++++++++++++++++++++++++--------------------- src/math/vector3.h | 35 +++++++++++++------------- src/math/vector4.c | 48 +++++++++++++++++------------------ src/math/vector4.h | 25 +++++++++--------- 5 files changed, 97 insertions(+), 83 deletions(-) diff --git a/src/main.c b/src/main.c index 0dc30a8..bc8fa7c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,12 @@ #include "math/vector3.h" +#include -int main() { +#define printVector3(v) { printf("(%f, %f, %f)", v.x, v.y, v.z); } + +int main() +{ + Vec3 vec = vec3(1, 1, 1); + Vec3 val = vec3_add(vec, vec3(1, 2, 3)); + printVector3(val); return 0; } \ No newline at end of file diff --git a/src/math/vector3.c b/src/math/vector3.c index 64f4cba..2781814 100644 --- a/src/math/vector3.c +++ b/src/math/vector3.c @@ -4,46 +4,51 @@ #include "vector3.h" -Vec3 vec3(float x, float y, float z) +inline Vec3 vec3(float x, float y, float z) { return (Vec3) {x, y, z}; } -Vec3 vec3Add(Vec3 v1, Vec3 v2) +Vec3 vec3_add(Vec3 v1, Vec3 v2) { return vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); } -Vec3 vec3Sub(Vec3 v1, Vec3 v2) +Vec3 vec3_sub(Vec3 v1, Vec3 v2) { return vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); } -Vec3 vec3Scale(Vec3 v, float scalar) +Vec3 vec3_scale(Vec3 v, float scalar) { return vec3(v.x * scalar, v.y * scalar, v.z * scalar); } -float vec3Dot(Vec3 a, Vec3 b) +Vec3 vec3_mul(Vec3 v1, Vec3 v2) +{ + return vec3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); +} + +float vec3_dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } -float vec3Len(Vec3 v) +float vec3_len(Vec3 v) { return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z); } -Vec3 vec3Norm(Vec3 v) +Vec3 vec3_norm(Vec3 v) { - float length = vec3Len(v); + float length = vec3_len(v); if (length == 0.f) return vec3(0, 0, 0); - return vec3Scale(v, 1.f / length); + return vec3_scale(v, 1.f / length); } -Vec3 vec3Lerp(Vec3 a, Vec3 b, float t) +Vec3 vec3_lerp(Vec3 a, Vec3 b, float t) { t = fmaxf(0.f, fminf(t, 1.f)); return vec3( @@ -53,7 +58,7 @@ Vec3 vec3Lerp(Vec3 a, Vec3 b, float t) ); } -Vec3 vec3Cross(Vec3 a, Vec3 b) +Vec3 vec3_cross(Vec3 a, Vec3 b) { return vec3( a.y * b.z - a.z * b.y, @@ -62,44 +67,44 @@ Vec3 vec3Cross(Vec3 a, Vec3 b) ); } -float vec3Angle(Vec3 a, Vec3 b) +float vec3_angle(Vec3 a, Vec3 b) { - float lenA = vec3Len(a); - float lenB = vec3Len(b); + float lenA = vec3_len(a); + float lenB = vec3_len(b); if (lenA < FLT_EPSILON || lenB < FLT_EPSILON) return NAN; return acosf(fmaxf(-1.f, - fminf(vec3Dot(a, b) / (lenA * lenB), 1.f))); + fminf(vec3_dot(a, b) / (lenA * lenB), 1.f))); } -Vec3 vec3Proj(Vec3 a, Vec3 b) +Vec3 vec3_proj(Vec3 a, Vec3 b) { - return vec3Scale(b, - vec3Dot(a, b) / vec3Dot(b, b)); + return vec3_scale(b, + vec3_dot(a, b) / vec3_dot(b, b)); } -Vec3 vec3Refl(Vec3 v, Vec3 normal) +Vec3 vec3_refl(Vec3 v, Vec3 normal) { - return vec3Sub(v, vec3Scale(vec3Proj(v, normal), 2.f)); + return vec3_sub(v, vec3_scale(vec3_proj(v, normal), 2.f)); } -float vec3Dist(Vec3 a, Vec3 b) +float vec3_dist(Vec3 a, Vec3 b) { - return vec3Len(vec3Sub(a, b));; + return vec3_len(vec3_sub(a, b)); } -Vec3 vec3Rotate(Vec3 v, Vec3 axis, float angle) +Vec3 vec3_rotate(Vec3 v, Vec3 axis, float angle) { - Vec3 normAxis = vec3Norm(axis); + Vec3 normAxis = vec3_norm(axis); - Vec3 rlt = vec3Add( - vec3Add( - vec3Scale(v, cosf(angle)), - vec3Scale(vec3Cross(normAxis, v), sinf(angle)) + Vec3 rlt = vec3_add( + vec3_add( + vec3_scale(v, cosf(angle)), + vec3_scale(vec3_cross(normAxis, v), sinf(angle)) ), - vec3Scale(normAxis, vec3Dot(normAxis, v) * (1 - cosf(angle))) + vec3_scale(normAxis, vec3_dot(normAxis, v) * (1 - cosf(angle))) ); return rlt; diff --git a/src/math/vector3.h b/src/math/vector3.h index 74da70a..0fa6d87 100644 --- a/src/math/vector3.h +++ b/src/math/vector3.h @@ -1,7 +1,8 @@ -#ifndef VEC3_H -#define VEC3_H +#ifndef VEC3__H +#define VEC3__H -typedef struct { +typedef struct +{ float x, y, z; } Vec3; @@ -20,7 +21,7 @@ Vec3 vec3(float x, float y, float z); * @param v2 Second vector. * @return A 3D vector representing the sum of v1 and v2. */ -Vec3 vec3Add(Vec3 v1, Vec3 v2); +Vec3 vec3_add(Vec3 v1, Vec3 v2); /** * @brief Subtracts two 3D vectors and returns a new 3D vector. @@ -28,7 +29,7 @@ Vec3 vec3Add(Vec3 v1, Vec3 v2); * @param v2 Second vector. * @return A 3D vector representing the result of v1 minus v2. */ -Vec3 vec3Sub(Vec3 v1, Vec3 v2); +Vec3 vec3_sub(Vec3 v1, Vec3 v2); /** * @brief Scales a 3D vector by a constant scalar and returns a new 3D vector. @@ -36,7 +37,7 @@ Vec3 vec3Sub(Vec3 v1, Vec3 v2); * @param scalar Scalar value. * @return A 3D vector representing the multiplication of v by the scalar. */ -Vec3 vec3Scale(Vec3 v, float scalar); +Vec3 vec3_scale(Vec3 v, float scalar); /** * @brief Computes the dot product of two 3D vectors. @@ -47,14 +48,14 @@ Vec3 vec3Scale(Vec3 v, float scalar); * - scalar = 0: Vectors are orthogonal (the angle between them is 90 degrees). * - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse). */ -float vec3Dot(Vec3 a, Vec3 b); +float vec3_dot(Vec3 a, Vec3 b); /** * @brief Computes the length (magnitude) of a 3D vector. * @param v 3D vector. * @return A scalar value representing the length (magnitude) of the vector v. */ -float vec3Len(Vec3 v); +float vec3_len(Vec3 v); /** * @brief Normalizes a 3D vector (scales it to unit length). @@ -62,7 +63,7 @@ float vec3Len(Vec3 v); * @return A 3D vector representing the normalized version of v. * Returns a zero vector (0, 0, 0) if the input vector is a zero vector. */ -Vec3 vec3Norm(Vec3 v); +Vec3 vec3_norm(Vec3 v); /** * @brief Performs linear interpolation between two 3D vectors. @@ -74,7 +75,7 @@ Vec3 vec3Norm(Vec3 v); * - t between 0 and 1 returns a point between a and b. * @return A 3D vector representing the interpolated result between a and b. */ -Vec3 vec3Lerp(Vec3 a, Vec3 b, float t); +Vec3 vec3_lerp(Vec3 a, Vec3 b, float t); /** * @brief Computes the cross product of two 3D vectors. @@ -84,7 +85,7 @@ Vec3 vec3Lerp(Vec3 a, Vec3 b, float t); * @param b Second vector. * @return A 3D vector representing the cross product of vectors a and b. */ -Vec3 vec3Cross(Vec3 a, Vec3 b); +Vec3 vec3_cross(Vec3 a, Vec3 b); /** * @brief Computes the angle between two 3D vectors. @@ -92,7 +93,7 @@ Vec3 vec3Cross(Vec3 a, Vec3 b); * @param b Second vector. * @return The angle between vectors a and b in radians. */ -float vec3Angle(Vec3 a, Vec3 b); +float vec3_angle(Vec3 a, Vec3 b); /** * @brief Computes the projection of vector a onto vector b. @@ -100,7 +101,7 @@ float vec3Angle(Vec3 a, Vec3 b); * @param b The vector onto which a is projected. * @return A 3D vector representing the projection of a onto b. */ -Vec3 vec3Proj(Vec3 a, Vec3 b); +Vec3 vec3_proj(Vec3 a, Vec3 b); /** * @brief Computes the reflection of a vector v against a normal. @@ -108,7 +109,7 @@ Vec3 vec3Proj(Vec3 a, Vec3 b); * @param normal The normal vector of the surface. * @return A 3D vector representing the reflection of v across normal. */ -Vec3 vec3Refl(Vec3 v, Vec3 normal); +Vec3 vec3_refl(Vec3 v, Vec3 normal); /** * @brief Computes the Euclidean distance between two 3D vectors. @@ -116,7 +117,7 @@ Vec3 vec3Refl(Vec3 v, Vec3 normal); * @param b The second vector. * @return The scalar distance between a and b. */ -float vec3Dist(Vec3 a, Vec3 b); +float vec3_rist(Vec3 a, Vec3 b); /** * @brief Rotates a 3D vector around a given axis by a specified angle. @@ -125,6 +126,6 @@ float vec3Dist(Vec3 a, Vec3 b); * @param angle Rotation angle in radians. * @return A 3D vector representing the rotated vector. */ -Vec3 vec3Rotate(Vec3 v, Vec3 axis, float angle); +Vec3 vec3_rotate(Vec3 v, Vec3 axis, float angle); -#endif // VEC3_H \ No newline at end of file +#endif // VEC3__H \ No newline at end of file diff --git a/src/math/vector4.c b/src/math/vector4.c index 864db89..70cc0b5 100644 --- a/src/math/vector4.c +++ b/src/math/vector4.c @@ -4,45 +4,45 @@ #include "vector4.h" -Vec4 vec4(float x, float y, float z, float w) +inline Vec4 vec4(float x, float y, float z, float w) { return (Vec4) {x, y, z, w}; } -Vec4 vec4Add(Vec4 v1, Vec4 v2) +Vec4 vec4_add(Vec4 v1, Vec4 v2) { return vec4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w); } -Vec4 vec4Sub(Vec4 v1, Vec4 v2) +Vec4 vec4_sub(Vec4 v1, Vec4 v2) { return vec4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w); } -Vec4 vec4Scale(Vec4 v, float scalar) +Vec4 vec4_scale(Vec4 v, float scalar) { return vec4(v.x * scalar, v.y * scalar, v.z * scalar, v.w * scalar); } -float vec4Dot(Vec4 a, Vec4 b) +float vec4_dot(Vec4 a, Vec4 b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } -float vec4Len(Vec4 v) +float vec4_len(Vec4 v) { return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); } -Vec4 vec4Norm(Vec4 v) +Vec4 vec4_norm(Vec4 v) { - float length = vec4Len(v); + float length = vec4_len(v); if (length == 0.f) return vec4(0, 0, 0, 0); - return vec4Scale(v, 1.f / length); + return vec4_scale(v, 1.f / length); } -Vec4 vec4Lerp(Vec4 a, Vec4 b, float t) +Vec4 vec4_lerp(Vec4 a, Vec4 b, float t) { t = fmaxf(0.f, fminf(t, 1.f)); @@ -56,15 +56,15 @@ Vec4 vec4Lerp(Vec4 a, Vec4 b, float t) float vec4Angle(Vec4 a, Vec4 b) { - float lenA = vec4Len(a); - float lenB = vec4Len(b); + float lenA = vec4_len(a); + float lenB = vec4_len(b); if (isnan(lenA) || isnan(lenB) || lenA < FLT_EPSILON || lenB < FLT_EPSILON) return NAN; - float dot = vec4Dot(a, b); + float dot = vec4_dot(a, b); float cosTheta = dot / (lenA * lenB); cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f)); @@ -72,26 +72,26 @@ float vec4Angle(Vec4 a, Vec4 b) return acosf(cosTheta); } -Vec4 vec4Proj(Vec4 a, Vec4 b) +Vec4 vec4_proj(Vec4 a, Vec4 b) { - float dotA = vec4Dot(a, b); - float dotB = vec4Dot(b, b); + float dotA = vec4_dot(a, b); + float dotB = vec4_dot(b, b); float scale = dotA / dotB; - return vec4Scale(b, scale); + return vec4_scale(b, scale); } -Vec4 vec4Refl(Vec4 v, Vec4 normal) +Vec4 vec4_refl(Vec4 v, Vec4 normal) { - Vec4 proj = vec4Proj(v, normal); - Vec4 scal = vec4Scale(proj, 2.f); - Vec4 rlt = vec4Sub(v, scal); + Vec4 proj = vec4_proj(v, normal); + Vec4 scal = vec4_scale(proj, 2.f); + Vec4 rlt = vec4_sub(v, scal); return rlt; } -float vec4Dist(Vec4 a, Vec4 b) +float vec4_dist(Vec4 a, Vec4 b) { - Vec4 vsub = vec4Sub(a, b); - float rlt = vec4Len(vsub); + Vec4 vsub = vec4_sub(a, b); + float rlt = vec4_len(vsub); return rlt; } \ No newline at end of file diff --git a/src/math/vector4.h b/src/math/vector4.h index 92dee6b..a3241e6 100644 --- a/src/math/vector4.h +++ b/src/math/vector4.h @@ -1,7 +1,8 @@ #ifndef VECTOR4_H #define VECTOR4_H -typedef struct { +typedef struct +{ float x, y, z, w; } Vec4; @@ -21,7 +22,7 @@ Vec4 vec4(float x, float y, float z, float w); * @param v2 Second vector. * @return A 4D vector representing the sum of v1 and v2. */ -Vec4 vec4Add(Vec4 v1, Vec4 v2); +Vec4 vec4_add(Vec4 v1, Vec4 v2); /** * @brief Subtracts two 4D vectors and returns a new 4D vector. @@ -29,7 +30,7 @@ Vec4 vec4Add(Vec4 v1, Vec4 v2); * @param v2 Second vector. * @return A 4D vector representing the result of v1 minus v2. */ -Vec4 vec4Sub(Vec4 v1, Vec4 v2); +Vec4 vec4_sub(Vec4 v1, Vec4 v2); /** * @brief Scales a 4D vector by a constant scalar and returns a new 4D vector. @@ -37,7 +38,7 @@ Vec4 vec4Sub(Vec4 v1, Vec4 v2); * @param scalar Scalar value. * @return A 4D vector representing the multiplication of v by the scalar. */ -Vec4 vec4Scale(Vec4 v, float scalar); +Vec4 vec4_scale(Vec4 v, float scalar); /** * @brief Computes the dot product of two 4D vectors. @@ -48,14 +49,14 @@ Vec4 vec4Scale(Vec4 v, float scalar); * - scalar = 0: Vectors are orthogonal (the angle between them is 90 degrees). * - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse). */ -float vec4Dot(Vec4 a, Vec4 b); +float vec4_dot(Vec4 a, Vec4 b); /** * @brief Computes the length (magnitude) of a 4D vector. * @param v 4D vector. * @return A scalar value representing the length (magnitude) of the vector v. */ -float vec4Len(Vec4 v); +float vec4_len(Vec4 v); /** * @brief Normalizes a 4D vector (scales it to unit length). @@ -63,7 +64,7 @@ float vec4Len(Vec4 v); * @return A 4D vector representing the normalized version of v. * Returns a zero vector (0, 0, 0) if the input vector is a zero vector. */ -Vec4 vec4Norm(Vec4 v); +Vec4 vec4_norm(Vec4 v); /** * @brief Performs linear interpolation between two 4D vectors. @@ -75,7 +76,7 @@ Vec4 vec4Norm(Vec4 v); * - t between 0 and 1 returns a point between a and b. * @return A 4D vector representing the interpolated result between a and b. */ -Vec4 vec4Lerp(Vec4 a, Vec4 b, float t); +Vec4 vec4_lerp(Vec4 a, Vec4 b, float t); /** * @brief Computes the angle between two 4D vectors. @@ -83,7 +84,7 @@ Vec4 vec4Lerp(Vec4 a, Vec4 b, float t); * @param b Second vector. * @return The angle between vectors a and b in radians. */ -float vec4Angle(Vec4 a, Vec4 b); +float vec4_angle(Vec4 a, Vec4 b); /** * @brief Computes the projection of vector a onto vector b. @@ -91,7 +92,7 @@ float vec4Angle(Vec4 a, Vec4 b); * @param b The vector onto which a is projected. * @return A 4D vector representing the projection of a onto b. */ -Vec4 vec4Proj(Vec4 a, Vec4 b); +Vec4 vec4_proj(Vec4 a, Vec4 b); /** * @brief Computes the reflection of a vector v against a normal. @@ -99,7 +100,7 @@ Vec4 vec4Proj(Vec4 a, Vec4 b); * @param normal The normal vector of the surface. * @return A 4D vector representing the reflection of v across normal. */ -Vec4 vec4Refl(Vec4 v, Vec4 normal); +Vec4 vec4_refl(Vec4 v, Vec4 normal); /** * @brief Computes the Euclidean distance between two 4D vectors. @@ -107,6 +108,6 @@ Vec4 vec4Refl(Vec4 v, Vec4 normal); * @param b The second vector. * @return The scalar distance between a and b. */ -float vec4Dist(Vec4 a, Vec4 b); +float vec4_dist(Vec4 a, Vec4 b); #endif // VECTOR4_H \ No newline at end of file