fix(vectors): edited functions name

This commit is contained in:
2025-05-16 13:49:16 +02:00
parent 35d6712d45
commit 37ee18c0e6
5 changed files with 97 additions and 83 deletions

View File

@@ -1,5 +1,12 @@
#include "math/vector3.h" #include "math/vector3.h"
#include <stdio.h>
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; return 0;
} }

View File

@@ -4,46 +4,51 @@
#include "vector3.h" #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}; 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); 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); 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); 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; 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); 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); 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)); t = fmaxf(0.f, fminf(t, 1.f));
return vec3( 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( return vec3(
a.y * b.z - a.z * b.y, 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 lenA = vec3_len(a);
float lenB = vec3Len(b); float lenB = vec3_len(b);
if (lenA < FLT_EPSILON || lenB < FLT_EPSILON) if (lenA < FLT_EPSILON || lenB < FLT_EPSILON)
return NAN; return NAN;
return acosf(fmaxf(-1.f, 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, return vec3_scale(b,
vec3Dot(a, b) / vec3Dot(b, 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( Vec3 rlt = vec3_add(
vec3Add( vec3_add(
vec3Scale(v, cosf(angle)), vec3_scale(v, cosf(angle)),
vec3Scale(vec3Cross(normAxis, v), sinf(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; return rlt;

View File

@@ -1,7 +1,8 @@
#ifndef VEC3_H #ifndef VEC3__H
#define VEC3_H #define VEC3__H
typedef struct { typedef struct
{
float x, y, z; float x, y, z;
} Vec3; } Vec3;
@@ -20,7 +21,7 @@ Vec3 vec3(float x, float y, float z);
* @param v2 Second vector. * @param v2 Second vector.
* @return A 3D vector representing the sum of v1 and v2. * @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. * @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. * @param v2 Second vector.
* @return A 3D vector representing the result of v1 minus v2. * @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. * @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. * @param scalar Scalar value.
* @return A 3D vector representing the multiplication of v by the scalar. * @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. * @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 are orthogonal (the angle between them is 90 degrees).
* - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse). * - 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. * @brief Computes the length (magnitude) of a 3D vector.
* @param v 3D vector. * @param v 3D vector.
* @return A scalar value representing the length (magnitude) of the vector v. * @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). * @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. * @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. * 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. * @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. * - t between 0 and 1 returns a point between a and b.
* @return A 3D vector representing the interpolated result 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. * @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. * @param b Second vector.
* @return A 3D vector representing the cross product of vectors a and b. * @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. * @brief Computes the angle between two 3D vectors.
@@ -92,7 +93,7 @@ Vec3 vec3Cross(Vec3 a, Vec3 b);
* @param b Second vector. * @param b Second vector.
* @return The angle between vectors a and b in radians. * @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. * @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. * @param b The vector onto which a is projected.
* @return A 3D vector representing the projection of a onto b. * @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. * @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. * @param normal The normal vector of the surface.
* @return A 3D vector representing the reflection of v across normal. * @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. * @brief Computes the Euclidean distance between two 3D vectors.
@@ -116,7 +117,7 @@ Vec3 vec3Refl(Vec3 v, Vec3 normal);
* @param b The second vector. * @param b The second vector.
* @return The scalar distance between a and b. * @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. * @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. * @param angle Rotation angle in radians.
* @return A 3D vector representing the rotated vector. * @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 #endif // VEC3__H

View File

@@ -4,45 +4,45 @@
#include "vector4.h" #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}; 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); 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); 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); 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; 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); 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); 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)); 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 vec4Angle(Vec4 a, Vec4 b)
{ {
float lenA = vec4Len(a); float lenA = vec4_len(a);
float lenB = vec4Len(b); float lenB = vec4_len(b);
if (isnan(lenA) || isnan(lenB) if (isnan(lenA) || isnan(lenB)
|| lenA < FLT_EPSILON || lenA < FLT_EPSILON
|| lenB < FLT_EPSILON) || lenB < FLT_EPSILON)
return NAN; return NAN;
float dot = vec4Dot(a, b); float dot = vec4_dot(a, b);
float cosTheta = dot / (lenA * lenB); float cosTheta = dot / (lenA * lenB);
cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f)); cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f));
@@ -72,26 +72,26 @@ float vec4Angle(Vec4 a, Vec4 b)
return acosf(cosTheta); return acosf(cosTheta);
} }
Vec4 vec4Proj(Vec4 a, Vec4 b) Vec4 vec4_proj(Vec4 a, Vec4 b)
{ {
float dotA = vec4Dot(a, b); float dotA = vec4_dot(a, b);
float dotB = vec4Dot(b, b); float dotB = vec4_dot(b, b);
float scale = dotA / dotB; 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 proj = vec4_proj(v, normal);
Vec4 scal = vec4Scale(proj, 2.f); Vec4 scal = vec4_scale(proj, 2.f);
Vec4 rlt = vec4Sub(v, scal); Vec4 rlt = vec4_sub(v, scal);
return rlt; return rlt;
} }
float vec4Dist(Vec4 a, Vec4 b) float vec4_dist(Vec4 a, Vec4 b)
{ {
Vec4 vsub = vec4Sub(a, b); Vec4 vsub = vec4_sub(a, b);
float rlt = vec4Len(vsub); float rlt = vec4_len(vsub);
return rlt; return rlt;
} }

View File

@@ -1,7 +1,8 @@
#ifndef VECTOR4_H #ifndef VECTOR4_H
#define VECTOR4_H #define VECTOR4_H
typedef struct { typedef struct
{
float x, y, z, w; float x, y, z, w;
} Vec4; } Vec4;
@@ -21,7 +22,7 @@ Vec4 vec4(float x, float y, float z, float w);
* @param v2 Second vector. * @param v2 Second vector.
* @return A 4D vector representing the sum of v1 and v2. * @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. * @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. * @param v2 Second vector.
* @return A 4D vector representing the result of v1 minus v2. * @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. * @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. * @param scalar Scalar value.
* @return A 4D vector representing the multiplication of v by the scalar. * @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. * @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 are orthogonal (the angle between them is 90 degrees).
* - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse). * - 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. * @brief Computes the length (magnitude) of a 4D vector.
* @param v 4D vector. * @param v 4D vector.
* @return A scalar value representing the length (magnitude) of the vector v. * @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). * @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. * @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. * 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. * @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. * - t between 0 and 1 returns a point between a and b.
* @return A 4D vector representing the interpolated result 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. * @brief Computes the angle between two 4D vectors.
@@ -83,7 +84,7 @@ Vec4 vec4Lerp(Vec4 a, Vec4 b, float t);
* @param b Second vector. * @param b Second vector.
* @return The angle between vectors a and b in radians. * @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. * @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. * @param b The vector onto which a is projected.
* @return A 4D vector representing the projection of a onto b. * @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. * @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. * @param normal The normal vector of the surface.
* @return A 4D vector representing the reflection of v across normal. * @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. * @brief Computes the Euclidean distance between two 4D vectors.
@@ -107,6 +108,6 @@ Vec4 vec4Refl(Vec4 v, Vec4 normal);
* @param b The second vector. * @param b The second vector.
* @return The scalar distance between a and b. * @return The scalar distance between a and b.
*/ */
float vec4Dist(Vec4 a, Vec4 b); float vec4_dist(Vec4 a, Vec4 b);
#endif // VECTOR4_H #endif // VECTOR4_H