add floating-point vectors

This commit is contained in:
2025-02-14 15:08:11 +01:00
parent 12fc702f49
commit 3d36b26fd9
3 changed files with 150 additions and 0 deletions

49
src/util/vec/float2.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include <math.h>
typedef struct {
float x;
float y;
} float2;
// adds two float2s together
static inline float2 float2_add(float2 v1, float2 v2) {
return (float2){v1.x + v2.x, v1.y + v2.y};
}
// subtracts one float2 from another
static inline float2 float2_sub(float2 v1, float2 v2) {
return (float2){v1.x - v2.x, v1.y - v2.y};
}
// multiplies the float2 by some value
static inline float2 float2_mlt(float2 v, float n) {
return (float2){v.x * n, v.y * n};
}
// divides the float2 by some value
static inline float2 float2_div(float2 v, float n) {
return (float2){v.x / n, v.y / n};
}
// gets the squared length of float2
static inline float float2_len2(float2 v) {
return (v.x * v.x) + (v.y * v.y);
}
// gets the length of float2 (magnitude)
static inline float float2_len(float2 v) {
return sqrtf(float2_len2(v));
}
// normalizes the float2
static inline float2 float2_norm(float2 v) {
float len = float2_len(v);
return float2_div(v, len);
}
// gets the dot product of two float2s
static inline float float2_dot(float2 v1, float2 v2) {
return v1.x * v2.x + v1.y * v2.y;
}

50
src/util/vec/float3.h Normal file
View File

@@ -0,0 +1,50 @@
#pragma once
#include <math.h>
typedef struct {
float x;
float y;
float z;
} float3;
// adds two float3s together
static inline float3 float3_add(float3 v1, float3 v2) {
return (float3){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};
}
// subtracts one float3 from another
static inline float3 float3_sub(float3 v1, float3 v2) {
return (float3){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
}
// multiplies the float3 by some value
static inline float3 float3_mlt(float3 v, float n) {
return (float3){v.x * n, v.y * n, v.z * n};
}
// divides the float3 by some value
static inline float3 float3_div(float3 v, float n) {
return (float3){v.x / n, v.y / n, v.z / n};
}
// gets the squared length of float3
static inline float float3_len2(float3 v) {
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
}
// gets the length of float3 (magnitude)
static inline float float3_len(float3 v) {
return sqrtf(float3_len2(v));
}
// normalizes the float3
static inline float3 float3_norm(float3 v) {
float len = float3_len(v);
return float3_div(v, len);
}
// gets the dot product of two float3s
static inline float float3_dot(float3 v1, float3 v2) {
return v1.x * v2.x + v1.y * v2.y +v1.z * v2.z;
}

51
src/util/vec/float4.h Normal file
View File

@@ -0,0 +1,51 @@
#pragma once
#include <math.h>
typedef struct {
float x;
float y;
float z;
float w;
} float4;
// adds two float3s together
static inline float4 float3_add(float4 v1, float4 v2) {
return (float4){v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w};
}
// subtracts one float3 from another
static inline float4 float3_sub(float4 v1, float4 v2) {
return (float4){v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w};
}
// multiplies the float3 by some value
static inline float4 float3_mlt(float4 v, float n) {
return (float4){v.x * n, v.y * n, v.z * n, v.w * n};
}
// divides the float3 by some value
static inline float4 float3_div(float4 v, float n) {
return (float4){v.x / n, v.y / n, v.z / n, v.w / n};
}
// gets the squared length of float3
static inline float float3_len2(float4 v) {
return (v.x * v.x) + (v.y * v.y) + (v.z * v.z);
}
// gets the length of float3 (magnitude)
static inline float float3_len(float4 v) {
return sqrtf(float3_len2(v));
}
// normalizes the float3
static inline float4 float3_norm(float4 v) {
float len = float3_len(v);
return float3_div(v, len);
}
// gets the dot product of two float3s
static inline float float3_dot(float4 v1, float4 v2) {
return v1.x * v2.x + v1.y * v2.y +v1.z * v2.z + v1.w * v2.w;
}