next up previous contents
Next: Fixed size vector products Up: Fixed size vectors Previous: Fixed size vector subtraction   Contents

Rescaling a fixed size vector

There are similar functions to multiply a 3-vector by a scalar
      double ds;

      gan_vec3_scale_q ( &v3x, ds, &v3z ); /* macro */
      v3z = gan_vec3_scale_s ( &v3x, ds ); /* function call */
      gan_vec3_scale_i ( &v3x, ds ); /* macro, result in-place in v3x */
to divide a 3-vector by a (non-zero) scalar
      gan_vec3_divide_q ( &v3x, ds, &v3z ); /* macro */
      v3z = gan_vec3_divide_s ( &v3x, ds ); /* function call */
      gan_vec3_divide_i ( &v3x, ds ); /* macro, result in-place in v3x */
to negate a 3-vector
      gan_vec3_negate_q ( &v3x, &v3z ); /* macro */
      v3z = gan_vec3_negate_s ( &v3x ); /* function call */
      gan_vec3_negate_i ( &v3x ); /* macro, result in-place in v3x */
and to scale a 3-vector to unit length (2-norm)
      gan_vec3_unit_q ( &v3x, &v3z ); /* macro */
      v3z = gan_vec3_unit_s ( &v3x ); /* function call */
      gan_vec3_unit_i ( &v3x ); /* macro, result in-place in v3x */
The last guarantees than the total squared element of the vector returned by gan_vec3_sqrlen() will be one; see below.

Error detection: If zero is passed as the scalar value to the ..._divide_[qi]() routines, NULL will be returned, while the ..._divide_s() routines will abort the program. You should add tests for division by zero before calling any of the ..._divide_[qsi]() routines. Similarly, the ..._unit_[qsi]() routines will fail if the input vector contains only zeros. If this is a possibility then the program should check beforehand.



2006-03-17