From 723155a2ce3cdcb738b85825463dd494abca0afb Mon Sep 17 00:00:00 2001 From: hasufell Date: Sat, 10 May 2014 20:17:12 +0200 Subject: [PATCH] Add add_vectors() and sub_vectors() --- src/vector.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/vector.c b/src/vector.c index ad32130..7894a71 100644 --- a/src/vector.c +++ b/src/vector.c @@ -60,6 +60,62 @@ bool vector_product(vector *a, vector *b, vector *c) return true; } +/** + * Add vector a to vector b and store the result in c, + * such as: c = a + b. + * This function is aliasing safe. + * + * @param a vector + * @param b vector + * @param c vector [out] + * @return true/false for success/failure + */ +bool add_vectors(vector *a, vector *b, vector *c) +{ + vector a_tmp, + b_tmp; + + if (!a || !b) + return false; + + copy_vector(a, &a_tmp); + copy_vector(b, &b_tmp); + + c->x = a_tmp.x + b_tmp.x; + c->y = a_tmp.y + b_tmp.y; + c->z = a_tmp.z + b_tmp.z; + + return true; +} + +/** + * Substract vector b from vector a and store the result in c, + * such as: c = a - b. + * This function is aliasing safe. + * + * @param a vector + * @param b vector + * @param c vector [out] + * @return true/false for success/failure + */ +bool sub_vectors(vector *a, vector *b, vector *c) +{ + vector a_tmp, + b_tmp; + + if (!a || !b) + return false; + + copy_vector(a, &a_tmp); + copy_vector(b, &b_tmp); + + c->x = a_tmp.x - b_tmp.x; + c->y = a_tmp.y - b_tmp.y; + c->z = a_tmp.z - b_tmp.z; + + return true; +} + /** * Normalize a vector into a unit vector * of length 1. This function is aliasing safe.