# Learning Objectives

- Create a new class called Quaternion
- Overload operators two define operations on quaternions
- Define a method for rotating about an arbitrary axis through the origin via quaternions

# Introduction

We have already established methods of rotating 3D objects around the 3 major axes using matrices. We also saw that rotating about an arbitrary axis in 3D required 7 matrix transformations. This process can be made more efficient via the use of quaternions

## Quaternion Class

Before we can accomplish our rotation we need to create a class called Quaternion, so that we can store and manipulate quaternions. Remember that a quaternion has the form

(1)This is sometimes expressed as a scalar/vector pair as follows

(2)where $\vec{v}=<b,c,d>$. So in your Quaternion class, have the constructor take a float and a Vector3D.

## Overloading Operators

We want to be able to manipulate our quaternions via all the defined operations. Given that $q_1=[s_1,\vec{v_1}]$ and $q_2=[s_2,\vec{v_2}]$, overload operators to achieve the following

- Addition / Subtraction

- Scalar Multiplication

- Quaternion Multiplication

- Magnitude

- Conjugate

- Inverse

## Rotations

Next we want to create a method on our Quaternion class that will take as parameters a quaternion representing the axis of rotation and an angle representing the angle of rotation. This method will be called by the quaternion representing our point and return a new quaternion representing our rotated point.

To rotate a point whose position is represented by the vector $\vec{p}$ by an angle $\theta$ (Using the right hand rule), we first create the quaternion $P=[0,\vec{p}]$ to represent this point. Now we create a quaternion to represent our axis of rotation. If our axis of rotation passes through the origin and is parallel to the **unit** vector $\vec{l}$, then our corresponding rotation quaternion is going to be $Q=[\cos \frac{\theta}{2},\vec{l} \sin \frac{\theta}{2}]$. To find the quaternion $P_r$ representing our rotated point, we do the following,

As mentioned in the text, this is equivalent to

(10)# Prelab Questions and Exercises

Using the quaternion $p=[1,<0,2,-3>]$ and $q=[2,<4,-1,1>]$

- Calculate $p+q$
- Calculate $p-q$
- Calculate $5p$
- Calculate $pq$
- Calculate $|p|$
- Calculate $p^*$
- Calculate $p^{-1}$

# Laboratory Procedures

1. Create a Quaternion class with the following overloaded operations

- Addition / Subtraction
- Scalar Multiplication
- Quaternion Multiplication
- Magnitude
- Conjugate
- Inverse

2. Create a method on the Quaternion class for rotating a point about an arbitrary axis through the origin

3. Create a program that asks the user for two quaternions and a scalar

- report the sum of the two quaternions
- report the difference of the two quaternions
- report the scalar times each quaternion
- report the product of the two quaternions
- report the magnitude of each quaternion
- report the conjugate of each quaternion
- report the inverse of each quaternion

4. Test your code using the questions from the pre-lab, and your pre-lab results.

5. Execute the above program, for your instructorâ€™s verification. Initials:_

6. Create a program that asks the user for the coordinates of a point, the components of a vector and an angle in degrees

- report the coordinates of the new point obtained by rotating the given point about the line through the origin parallel to the given vector by the given angle.

7. Test your code using the questions from the pre-lab, and your pre-lab results.

8. Execute the above program, for your instructorâ€™s verification. Initials:_

# Postlab Questions

- What cautions if any must be taken in operating on quaternions?
- What cautions if any must be taken in rotating using quaternions?