fix: float4 did not have the functions named float4 + renamve to and create function

This commit is contained in:
2025-03-04 13:27:46 +01:00
parent 3da3e2897f
commit 3164fe1661
3 changed files with 44 additions and 26 deletions

View File

@@ -2,12 +2,13 @@
#include <math.h> #include <math.h>
// stores a 2D point using a floating-point number
typedef struct { typedef struct {
float x; float x;
float y; float y;
} float2; } float2;
// adds two float2s together // adds one float2 to another
static inline float2 float2_add(float2 v1, float2 v2) { static inline float2 float2_add(float2 v1, float2 v2) {
return (float2){v1.x + v2.x, v1.y + v2.y}; return (float2){v1.x + v2.x, v1.y + v2.y};
} }
@@ -17,8 +18,13 @@ static inline float2 float2_sub(float2 v1, float2 v2) {
return (float2){v1.x - v2.x, v1.y - v2.y}; return (float2){v1.x - v2.x, v1.y - v2.y};
} }
// multiplies the float2 by some value // multiplies one float2 by another
static inline float2 float2_mlt(float2 v, float n) { static inline float2 float2_mlt(float2 v1, float2 v2) {
return (float2){v1.x * v2.x, v1.y * v2.y};
}
// scales the float2 by some value
static inline float2 float2_scale(float2 v, float n) {
return (float2){v.x * n, v.y * n}; return (float2){v.x * n, v.y * n};
} }

View File

@@ -2,13 +2,14 @@
#include <math.h> #include <math.h>
// stores a 3D point using a floating-point number
typedef struct { typedef struct {
float x; float x;
float y; float y;
float z; float z;
} float3; } float3;
// adds two float3s together // adds one float3 to another
static inline float3 float3_add(float3 v1, float3 v2) { static inline float3 float3_add(float3 v1, float3 v2) {
return (float3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z}; return (float3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
} }
@@ -18,8 +19,13 @@ static inline float3 float3_sub(float3 v1, float3 v2) {
return (float3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z}; return (float3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
} }
// multiplies the float3 by some value // multiplies one float3 by another
static inline float3 float3_mlt(float3 v, float n) { static inline float3 float3_mlt(float3 v1, float3 v2) {
return (float3){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z};
}
// scales the float3 by some value
static inline float3 float3_scale(float3 v, float n) {
return (float3){v.x * n, v.y * n, v.z * n}; return (float3){v.x * n, v.y * n, v.z * n};
} }
@@ -46,5 +52,5 @@ static inline float3 float3_norm(float3 v) {
// gets the dot product of two float3s // gets the dot product of two float3s
static inline float float3_dot(float3 v1, float3 v2) { static inline float float3_dot(float3 v1, float3 v2) {
return v1.x * v2.x + v1.y * v2.y +v1.z * v2.z; return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
} }

View File

@@ -2,6 +2,7 @@
#include <math.h> #include <math.h>
// stores a 3D point using a floating-point number
typedef struct { typedef struct {
float x; float x;
float y; float y;
@@ -9,43 +10,48 @@ typedef struct {
float w; float w;
} float4; } float4;
// adds two float3s together // adds one float4 to another
static inline float4 float3_add(float4 v1, float4 v2) { static inline float4 float4_add(float4 v1, float4 v2) {
return (float4){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w}; return (float4){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w};
} }
// subtracts one float3 from another // subtracts one float4 from another
static inline float4 float3_sub(float4 v1, float4 v2) { static inline float4 float4_sub(float4 v1, float4 v2) {
return (float4){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w}; return (float4){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w};
} }
// multiplies the float3 by some value // multiplies one float4 by another
static inline float4 float3_mlt(float4 v, float n) { static inline float4 float4_mlt(float4 v1, float4 v2) {
return (float4){v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w};
}
// scales the float4 by some value
static inline float4 float4_scale(float4 v, float n) {
return (float4){v.x * n, v.y * n, v.z * n, v.w * n}; return (float4){v.x * n, v.y * n, v.z * n, v.w * n};
} }
// divides the float3 by some value // divides the float4 by some value
static inline float4 float3_div(float4 v, float n) { static inline float4 float4_div(float4 v, float n) {
return (float4){v.x / n, v.y / n, v.z / n, v.w / n}; return (float4){v.x / n, v.y / n, v.z / n, v.w / n};
} }
// gets the squared length of float3 // gets the squared length of float4
static inline float float3_len2(float4 v) { static inline float float4_len2(float4 v) {
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z); return (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
} }
// gets the length of float3 (magnitude) // gets the length of float4 (magnitude)
static inline float float3_len(float4 v) { static inline float float4_len(float4 v) {
return sqrtf(float3_len2(v)); return sqrtf(float4_len2(v));
} }
// normalizes the float3 // normalizes the float4
static inline float4 float3_norm(float4 v) { static inline float4 float3_norm(float4 v) {
float len = float3_len(v); float len = float4_len(v);
return float3_div(v, len); return float4_div(v, len);
} }
// gets the dot product of two float3s // gets the dot product of two float4s
static inline float float3_dot(float4 v1, float4 v2) { static inline float float4_dot(float4 v1, float4 v2) {
return v1.x * v2.x + v1.y * v2.y +v1.z * v2.z + v1.w * v2.w; return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
} }