Add add_vectors() and sub_vectors()

This commit is contained in:
hasufell 2014-05-10 20:17:12 +02:00
parent 2733b17af8
commit 723155a2ce
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
1 changed files with 56 additions and 0 deletions

View File

@ -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.