Averaging Quaternions and Vectors

Quaternion Averaging
This code will calculate the average rotation of various separate quaternions. If the total amount of rotations are unknown, the algorithm will still work. The foreach loop will output a valid average value with each loop cycle.

Note: this code will only work if the separate quaternions are relatively close to each other. Also note that it might not be necessary to normalize the end result.

This method is based on a simplified procedure described in this document: http://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/20070017872_2007014421.pdf Special thanks to "Titos" for doing the maths.

Vector Averaging
This code will calculate the average vector (which can be a position) of various separate vectors. If the total amount of vectors are unknown, the algorithm will still work. The foreach loop will output a valid average value with each loop cycle.