I’m a big fan of “exotic” math and programming techniques. There’s nothing I like more than seeing something common used in an uncommon way to do something that I didn’t know was possible.

In this post I share a technique that lets you use imaginary numbers (complex numbers more specifically) to be able to rotate vectors in 2d. This technique is so simple that you can even use it to rotate vectors by hand on paper!

## Quick Review: Imaginary and Complex Numbers

The imaginary number “i” is the square root of -1. Without using i, you can’t take the square root of a negative number because if the answer is negative, multiplying a negative by a negative is positive, and if the answer is a positive, multiplying a positive by a positive is still a positive.

But, using i, you CAN take the square root of negative numbers.

The square root of 25 is 5.

The square root of -25 is 5*i, or just 5i, which means “5 times the square root of -1”.

You can combine a real and imaginary number into something called a complex number like this: 3 + 5i

## Quick Review: Multiplying Complex Numbers

We’ll need to be able to multiply complex numbers together to do our rotations.

(3+5i)*(2+3i) = ???

Luckily, multiplying complex numbers together is as simple as using F.O.I.L. if you remember that from high school math class, it stands for First, Outer, Inner, Last.

Using that, we can multiply and then combine term, remembering that i^2 is -1:

(3+5i)*(2+3i) =

6 + 9i + 10i + 15i^2 =

6 + 19i + 15i^2 =

6 + 19i – 15 =

-9 + 19i

There we go, that’s all there is to multiplying complex numbers together!

## Getting Down To Business: Rotating

Let’s say that we want to rotate the vector (0,1) by the angle of the vector (1,1). To do that, we just convert the vectors to complex numbers, using the x axis as the real number component, and the y axis as the imaginary number component, then multiply them, and convert back to vectors.

That’s a mouth full, so here it is, step by step.

**1) Convert Vectors to Complex Numbers**

(0,1) = 0 + 1i = i

(1,1) = 1 + 1i = 1 + i

**2) Multiply the Complex Numbers**

i * (1 + i) =

i + i^2 =

i – 1 =

-1 + i

In the above we change i – 1 to -1 + i to make the next step easier. The real component of the complex number is the X axis and the imaginary component is the Y axis.

**3) Convert from Complex Number to Vector**

-1 + i =

-1 + 1i =

(-1, 1)

The diagram below shows this operation:

As you can see we rotated the purple vector (0,1) which has an angle of 90 degrees and length of 1, by the blue vector (1,1) which has an angle of 45 degrees and a length of sqrt(2), and as a result we got the tan vector (-1,1) which has an angle of 135 degrees and a length of sqrt(2). So, we essentially rotated the 90 degree angle by the 45 degree angle and ended up with a 135 degree angle.

Note that order of operations doesn’t matter, you could rotate the 90 degree angle by 45 degrees, or you could rotate the 45 degree angle by 90 degrees, either way you are going to end up with 135 degrees.

A caveat with this technique though is that when you do the rotation, the resulting vector’s length will be the the product of the two source vectors used; you won’t get a normalized vector out as a result unless your source vectors are normalized, or you normalize after you are done with your operations.

As another example, let’s rotate the vector (4,3) by the vector (-12,5).

The first step is to convert them to complex numbers:

(4,3) = 4 + 3i

(-12,5) = -12 + 5i

Then we multiply them:

(4 + 3i) * (-12 + 5i) =

-48 + 20i – 36i + 15i^2 =

-48 – 16i – 15 =

-63 – 16i

Then convert back to a vector to get: (-63, -16)

In the image, you can see that we started with the blue vector (4,3) which is about 37 degrees and has a length of 5.

We rotated that vector by the purple vector (-12,5) which is about 157 degrees and has a length of 13.

That gave us the tan vector of (-63, -16) which is about 194 degrees and has a length of 65.

The resulting vector has the rotations added, and the lengths multiplied together.

## Unrotating

If we multiply the complex numbers together to add rotations, does that mean that we divide the complex numbers to subtract rotations? Sort of…

If you want to subtract a rotation, you multiply the imaginary part of the vector you want to subtract by -1 (or just flip the sign) and then multiply as normal.

When you flip the sign of the imaginary part, this is actually called the “complex conjugate” but if that term scares you, feel free to ignore it ðŸ˜›

As an example of unrotation, let’s take the vector (5,1) and subtract the vector (2,2) from it to see what we get.

The first step is to convert them into complex numbers.

(5,1) = 5 + i

(2,2) = 2 + 2i

Next up, we are going to flip the imaginary part of the vector we want to subtract.

2 + 2i becomes 2 – 2i

Then, we multiply as per normal:

(5 + i) * (2 – 2i) =

10 – 10i + 2i – 2i^2 =

10 – 8i + 2 =

12 – 8i

Finally, we convert back to a vector to get (12, -8).

In the image above, we started with the blue vector (5,1) which is about 11 degrees and has a length of sqrt(26).

We then unrotated by the purple vector (2,2) which is 45 degrees and has a length of sqrt(8).

That gave us the tan vector as a result (12,-8) which is about -34 degrees and has a length of sqrt(208).

Our resulting vector is the result of the second vector’s angle subtracted from the first vector’s angle, but the length of the vectors are still multiplied together, not divided.

## Unrotating to Get Vector Length

As a fun aside, if you unrotate a vector by itself, the result will be that the imaginary components (the Y component) will disappear, and in the result, the real component (the x component) will be the squared length of the vector.

It’s easy enough to calculate the squared length of a vector by adding x^2 and y^2 but this is an interesting property.

In fact, in the last post I mentioned CORDIC math using imaginary numbers to rotate vectors to try and find sine, cosine, etc. This is related to how it does that work. It basically rotates or unrotates a vector by smaller and smaller angles iteratively, based on the sign of the y (imaginary) component to try and get y to zero, which leaves the answer in the x component. It also has some other magic sprinkled in to where it only has to deal with integer math.

Hopefully before too long I’ll be able to make a CORDIC blog post and talk more about that in detail.

## Example Code

Ok so this theory stuff is all well and good but how about some code?

Before we get to that I want to give the naked formulas for rotating or unrotating vectors.

Given two vectors (A.x, A.y) and (B.x, B.y)…

Rotating A by B to get C:

C.x = A.x*B.x – A.y*B.y

C.y = A.x*B.y + A.y*B.x

Note: In the resulting vector C, the angles will be added, but the vector lengths will be multiplied.

Unrotating A by B to get C, we just flip the sign of any terms that contain B.y:

C.x = A.x*B.x + A.y*B.y

C.y = -A.x*B.y + A.y*B.x

Note: In the resulting vector C, the angles will be subtracted, but the vector lengths will be multiplied.

Below is some sample code, along with the output, showing our examples above working correctly.

#include <stdio.h> struct SVector { float x; float y; }; void Rotate (const SVector &A, const SVector &B, SVector &C) { C.x = A.x * B.x - A.y * B.y; C.y = A.x * B.y + A.y * B.x; } void Unrotate (const SVector &A, const SVector &B, SVector &C) { C.x = A.x * B.x + A.y * B.y; C.y = -A.x * B.y + A.y * B.x; } void print (const SVector &V) { printf("(%0.2f,%0.2f)", V.x, V.y); } int main(int argc, char **argv) { { SVector testA = {0.0f, 1.0f}; SVector testB = {1.0f, 1.0f}; SVector testC = {0.0f, 0.0f}; Rotate(testA, testB, testC); printf("Rotating "); print(testA); printf(" by "); print(testB); printf(" = "); print(testC); printf("\n"); } { SVector testA = {4.0f, 3.0f}; SVector testB = {-12.0f, 5.0f}; SVector testC = {0.0f, 0.0f}; Rotate(testA, testB, testC); printf("Rotating "); print(testA); printf(" by "); print(testB); printf(" = "); print(testC); printf("\n"); } { SVector testA = {5.0f, 1.0f}; SVector testB = {2.0f, 2.0f}; SVector testC = {0.0f, 0.0f}; Unrotate(testA, testB, testC); printf("Unrotating "); print(testA); printf(" by "); print(testB); printf(" = "); print(testC); printf("\n"); } return 0; }

Program output:

## More Info

Check out these links for more details:

Better Explained: A Visual, Intuitive Guide to Imaginary Numbers

Better Explained: Intuitive Arithmetic With Complex Numbers