From 81935dea34c9ecff7774d0daf7bd424e8dc767b6 Mon Sep 17 00:00:00 2001 From: unset Date: Fri, 14 Feb 2025 15:08:11 +0100 Subject: [PATCH] add floating-point vectors --- src/util/vec/float2.h | 49 +++++++++++++++++++++++++++++++++++++++++ src/util/vec/float3.h | 50 ++++++++++++++++++++++++++++++++++++++++++ src/util/vec/float4.h | 51 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 src/util/vec/float2.h create mode 100644 src/util/vec/float3.h create mode 100644 src/util/vec/float4.h diff --git a/src/util/vec/float2.h b/src/util/vec/float2.h new file mode 100644 index 0000000..d9f4134 --- /dev/null +++ b/src/util/vec/float2.h @@ -0,0 +1,49 @@ +#pragma once + +#include + +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; +} diff --git a/src/util/vec/float3.h b/src/util/vec/float3.h new file mode 100644 index 0000000..8ca9499 --- /dev/null +++ b/src/util/vec/float3.h @@ -0,0 +1,50 @@ +#pragma once + +#include + +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; +} diff --git a/src/util/vec/float4.h b/src/util/vec/float4.h new file mode 100644 index 0000000..5a65a55 --- /dev/null +++ b/src/util/vec/float4.h @@ -0,0 +1,51 @@ +#pragma once + +#include + +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; +}