This is post #2 of my Vector Angle series. Make sure you check out the first post for a quick introduction!

Method #2: Change of Basis

Visually, this is my favorite method. The intuition behind this one is a bit clever. Rather than measure the length of a vector, perform a linear transformation to change the space we occupy. This trick allows us to define the space relative to that vector (pink), so that $\hat{i}$ equals the vector. Then, it’s as simple a task as getting the $\hat{i}$ $\hat{j}$ components of the orange vector. Once we have those two components, we can calculate the angle using using the lengths of the components and the guaranteed right angle formed by those components.

Now about this “guaranteed right angle”. So, it turns out this is only true if your basis space vectors ($\hat{i}$, $\hat{j}$) form a right angle. To ensure this is true, the transformation has to be an Affine Transformation (basically, it can only scale, translate, or rotate the space).

Practically, this just means that when we rotate/scale $\hat{i}$, we also need to rotate/scale $\hat{j}$ by that same amount.

Let’s do it

2D Vector Space

First, we define a 2x2 matrix that contains the transform we want. Because we are transforming $\hat{i}$ to be the same as the purple vector, we can just put the purple vector’s components in the first column of the matrix.

$$ \begin{bmatrix} v_\hat{i} & ?\\ v_\hat{j} & ? \end{bmatrix} $$

Alright, so we’ve defined $\hat{i}$’s transformation, now we just need to ensure $\hat{j}$ receives an equal amount of rotation/scaling.

Since we know that the two basis vectors will maintain a 90-degree angle, we can define the second basis vector to be the same amount, but with the $\hat{i}$ and $\hat{j}$ flipped, and the $\hat{j}$ coordinate negative.

$$ \begin{bmatrix} v_\hat{i} & -v_\hat{j}\\ v_\hat{j} & v_\hat{i} \end{bmatrix} $$

Great, let’s try applying this matrix to our vectors:

Oh no! This isn’t right. When we defined the transform, were trying to move $\hat{i}$ onto $v$, but we need to do the opposite. Visually, this is what it should be:

To get the actual matrix, we just need to invert the one we came up with first. Basically, instead of putting $\hat{i}$ onto $v$, put $v$ onto $\hat{i}$.

The formula for inverting a 2x2 matrix is:

$${\begin{bmatrix} a & b\\ c & d \end{bmatrix}}^{-1}=\frac {1} {ad-bc} \begin{bmatrix} d & -b\\ -c & a \end{bmatrix}$$

Let’s apply that to the matrix above:

$$ {\begin{bmatrix} v_\hat{i} & -v_\hat{j}\\ v_\hat{j} & v_\hat{i} \end{bmatrix}}^{-1} = \frac {1} {(v_\hat{i}*v_\hat{i})-(-v_\hat{j} * v_\hat{j})} \begin{bmatrix} v_\hat{i} & v_\hat{j}\\ -v_\hat{j} & v_\hat{i} \end{bmatrix} = \frac {1} {v_\hat{i}^2+v_\hat{j}^2} \begin{bmatrix} v_\hat{i} & v_\hat{j}\\ -v_\hat{j} & v_\hat{i} \end{bmatrix} $$

Great! Now that we have this transformation, we just need to apply it to vector $u$.

$$u \cdot \frac {1} {v_\hat{i}^2+v_\hat{j}^2} \begin{bmatrix} v_\hat{i} & v_\hat{j}\\ -v_\hat{j} & v_\hat{i} \end{bmatrix}$$ $$= \begin{bmatrix} u_\hat{i} \\ u_\hat{j} \end{bmatrix} \cdot \frac {1} {v_\hat{i}^2+v_\hat{j}^2} \begin{bmatrix} v_\hat{i} & v_\hat{j}\\ -v_\hat{j} & v_\hat{i} \end{bmatrix}$$

$$= \frac {1} {v_\hat{i}^2+v_\hat{j}^2} \begin{bmatrix} u_{i} * v_\hat{i} + u_{j} * -v_\hat{j} \\ u_{i} * v_\hat{j} + u_{j} * v_\hat{i} \end{bmatrix}$$

$$= \frac {1} {v_\hat{i}^2+v_\hat{j}^2} \begin{bmatrix} u_{i} * v_\hat{i} - u_{j} * v_\hat{j} \\ u_{i} * v_\hat{j} + u_{j} * v_\hat{i} \end{bmatrix}$$

Note: In the first video, the yellow and teal lines each represent a different component of the output.

Now that we have the vector transformed into the basis space, we can use our trig identities to solve it.

For this example, we have the two sides of a right triangle, so we can look back to SOH CAH TOA to see that we have the opposite and adjacent sides.

Therefore: $$\tan(\theta) = \frac{\text{opp}}{\text{adj}} = \frac {\frac {1} {v_\hat{i}^2+v_\hat{j}^2} (u_{i} * v_\hat{i} - u_{j} * v_\hat{j})} {\frac {1} {v_\hat{i}^2+v_\hat{j}^2} (u_{i} * v_\hat{j} + u_{j} * v_\hat{i})}$$

We can factor out the pesky $\frac {1} {v_\hat{i}^2+v_\hat{j}^2}$:

$$\tan(\theta) = \frac {u_{i} * v_\hat{i} - u_{j} * v_\hat{j}} {u_{i} * v_\hat{j} + u_{j} * v_\hat{i}}$$

$$\theta = \arctan{\bigg(\frac {u_{i} * v_\hat{i} - u_{j} * v_\hat{j}} {u_{i} * v_\hat{j} + u_{j} * v_\hat{i}}\bigg)}$$

What happens if the vectors form an obtuse angle?

As you can see in the above example, the angle we calculate is always acute.

If we aren’t measuring the correct angle, we can find it by subtracting to get the obtuse angle: $180 - \theta$.

Thankfully, we know that $v$ is equal to $\hat{i}$. So if $u$’s $\hat{i}$ component is negative, we know the two vectors form an obtuse angle.

$$ \theta=\begin{cases} \theta = \arctan{\bigg(\frac {u_{i} * v_\hat{i} - u_{j} * v_\hat{j}} {u_{i} * v_\hat{j} + u_{j} * v_\hat{i}}\bigg)}, & \text{if $u_{i} * v_\hat{i} - u_{j} * v_\hat{j} > 0$}\\ \theta = 180 - \arctan{\bigg(\frac {u_{i} * v_\hat{i} - u_{j} * v_\hat{j}} {u_{i} * v_\hat{j} + u_{j} * v_\hat{i}}\bigg)}, & \text{if $u_{i} * v_\hat{i} - u_{j} * v_\hat{j} < 0$}\\ \theta = 90, & \text{otherwise} \end{cases} $$

3D Vector Space

First, we define a 3x3 matrix that contains the transformation we want. Because we are transforming $\hat{i}$ to become the same value as the purple vector, we can just put the purple vector’s components in the first column of the matrix.

$$ \begin{bmatrix} v_\hat{i} & ? & ?\\ v_\hat{j} & ? & ?\\ v_\hat{k} & ? & ? \end{bmatrix} $$

Sadly, this is as far as we can get without the equation becoming complicated. Since we only have one vector, we now have an entire plane of possible choices for the second vector. It’s only after we arbitrarily choose this second vector that we can choose the third vector.

In this example, $v$ is the pink vector. The green and orange vector are the two we want to define.

This is a challenging problem, so we might tackle it in a later blog post.

<- Up Next

In the next posts, we’ll continue other methods of calculating the angle with some special approaches, along with visualizations of what exactly we’re doing when we apply linear algebra concepts to this problem.

<- Read the next post Previous ->