From 8145fbe95bebef9517dd3c864e07f96b60a4fcaf Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Thu, 8 Feb 2024 07:52:58 -0800 Subject: Vector min max. --- include/math/vec2.h | 14 ++++++++++++-- include/math/vec3.h | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/math/vec2.h b/include/math/vec2.h index 5a74705..5a56a94 100644 --- a/include/math/vec2.h +++ b/include/math/vec2.h @@ -57,11 +57,21 @@ static inline vec2 vec2_scale(vec2 v, R s) { return (vec2){v.x * s, v.y * s}; } /// Compare two vectors for equality. static inline bool vec2_eq(vec2 a, vec2 b) { return a.x == b.x && a.y == b.y; } +/// Compare two vectors for inequality. +static inline bool vec2_ne(vec2 a, vec2 b) { return !(vec2_eq(a, b)); } + /// Return the absolute value of the vector. static inline vec2 vec2_abs(vec2 v) { return (vec2){rabs(v.x), rabs(v.y)}; } -/// Compare two vectors for inequality. -static inline bool vec2_ne(vec2 a, vec2 b) { return !(vec2_eq(a, b)); } +/// Return the component-wise minimum of two vectors. +static inline vec2 vec2_min(vec2 a, vec2 b) { + return (vec2){.x = min(a.x, b.x), .y = min(a.y, b.y)}; +} + +/// Return the component-wise maximum of two vectors. +static inline vec2 vec2_max(vec2 a, vec2 b) { + return (vec2){.x = max(a.x, b.x), .y = max(a.y, b.y)}; +} /// Return the vector's squared magnitude. static inline R vec2_norm2(vec2 v) { return v.x * v.x + v.y * v.y; } diff --git a/include/math/vec3.h b/include/math/vec3.h index d8e1248..ee6d460 100644 --- a/include/math/vec3.h +++ b/include/math/vec3.h @@ -61,14 +61,24 @@ static inline bool vec3_eq(vec3 a, vec3 b, R eps) { return R_eq(a.x, b.x, eps) && R_eq(a.y, b.y, eps) && R_eq(a.z, b.z, eps); } +/// Compare two vectors for inequality. +static inline bool vec3_ne(vec3 a, vec3 b, R eps) { + return !(vec3_eq(a, b, eps)); +} + /// Return the absolute value of the vector. static inline vec3 vec3_abs(vec3 v) { return (vec3){rabs(v.x), rabs(v.y), rabs(v.z)}; } -/// Compare two vectors for inequality. -static inline bool vec3_ne(vec3 a, vec3 b, R eps) { - return !(vec3_eq(a, b, eps)); +/// Return the component-wise minimum of two vectors. +static inline vec3 vec3_min(vec3 a, vec3 b) { + return (vec3){.x = min(a.x, b.x), .y = min(a.y, b.y), .z = min(a.z, b.z)}; +} + +/// Return the component-wise maximum of two vectors. +static inline vec3 vec3_max(vec3 a, vec3 b) { + return (vec3){.x = max(a.x, b.x), .y = max(a.y, b.y), .z = max(a.z, b.z)}; } /// Return the vector's squared magnitude. -- cgit v1.2.3