From b7be56cd9d90347daa59f007ef311f6fe92fae3a Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 2 Oct 2018 00:09:22 -0700 Subject: v2f: add pointer result variants it can be convenient to supply and return a result pointer, making for more composable vector operations. --- v2f.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/v2f.h b/v2f.h index 5d9d561..4867035 100644 --- a/v2f.h +++ b/v2f.h @@ -30,24 +30,56 @@ static inline v2f_t v2f_add(const v2f_t *a, const v2f_t *b) } +static inline v2f_t * pv2f_add(const v2f_t *a, const v2f_t *b, v2f_t *res) +{ + *res = v2f_add(a, b); + + return res; +} + + static inline v2f_t v2f_sub(const v2f_t *a, const v2f_t *b) { return (v2f_t){a->x - b->x, a->y - b->y}; } +static inline v2f_t * pv2f_sub(const v2f_t *a, const v2f_t *b, v2f_t *res) +{ + *res = v2f_sub(a, b); + + return res; +} + + static inline v2f_t v2f_mult(const v2f_t *a, const v2f_t *b) { return (v2f_t){a->x * b->x, a->y * b->y}; } +static inline v2f_t * pv2f_mult(const v2f_t *a, const v2f_t *b, v2f_t *res) +{ + *res = v2f_mult(a, b); + + return res; +} + + static inline v2f_t v2f_mult_scalar(const v2f_t *v, float scalar) { return (v2f_t){ v->x * scalar, v->y * scalar }; } +static inline v2f_t * pv2f_mult_scalar(const v2f_t *v, float scalar, v2f_t *res) +{ + *res = v2f_mult_scalar(v, scalar); + + return res; +} + + static inline float v2f_dot(const v2f_t *a, const v2f_t *b) { return a->x * b->x + a->y * b->y; @@ -66,6 +98,14 @@ static inline v2f_t v2f_normalize(const v2f_t *v) } +static inline v2f_t * pv2f_normalize(const v2f_t *v, v2f_t *res) +{ + *res = v2f_normalize(v); + + return res; +} + + static inline v2f_t v2f_lerp(const v2f_t *a, const v2f_t *b, float t) { v2f_t lerp_a, lerp_b; @@ -77,6 +117,14 @@ static inline v2f_t v2f_lerp(const v2f_t *a, const v2f_t *b, float t) } +static inline v2f_t * pv2f_lerp(const v2f_t *a, const v2f_t *b, float t, v2f_t *res) +{ + *res = v2f_lerp(a, b, t); + + return res; +} + + static inline v2f_t v2f_nlerp(const v2f_t *a, const v2f_t *b, float t) { v2f_t lerp = v2f_lerp(a, b, t); @@ -84,4 +132,12 @@ static inline v2f_t v2f_nlerp(const v2f_t *a, const v2f_t *b, float t) return v2f_normalize(&lerp); } + +static inline v2f_t * pv2f_nlerp(const v2f_t *a, const v2f_t *b, float t, v2f_t *res) +{ + *res = v2f_nlerp(a, b, t); + + return res; +} + #endif -- cgit v1.2.3