About the cross product and normalization

Hey there,

Recently, I had a conversation about the cross product and if it requires normalization when used to find perpendicular axes. The topic I am going to discuss might be basic knowledge for many; the thing is that if you are just starting with computer graphics you are so overwhelmed with mathematics that you tend to not observe the details in everything. Alright, there may be some unknown words in the first sentence so let’s break it down.

Cross product

The cross product is a binary operation on two vectors in three-dimensional space and is denoted by the symbol ×. Given two linearly independent vectors a and b, a × b is a vector that is perpendicular to both a and b. Let s be the result of the cross of a and b, then we can have the following equations for it:

  • First is the result expressed in coordinate notation where we can get the x, y and z of the result s by combining the coordinates of a and b.
Equation 1
  • Then we have the result expressed in terms of the input vector lengths (magnitudes), the sine of the angle between them and a vector that is perpendicular to them. If we have all of them, then we can get s.
Equation 2

In most situations we don’t have all that is required for equation 2, so we calculate the result using equation 1 and use that result to extract information from equation 2. As I’ve mentioned before, we can use the cross product to find a perpendicular vector of two other vectors. We manage this by combining these two equations.

Normalization

The vectors that may exist can have arbitrary lengths; that means we can have many vectors pointing in the same direction but their end points will be different points in space. Imagine a half line from point X to infinity. All the vectors that start from X (the red disk) and their end points are on the half line (the colorful disks), point in the same direction. A normalized vector is a vector that has length equal to 1, same as a unit vector.

A not so intuitive depiction of vectors. A better approach is to have the colorful disks as arrow tips.

For every possible direction, there exists only one normalized vector that describes it. Assuming the half line from before, only one of the possible vectors on the half line has exactly length of 1. But they all have something more in common: we can get the normalized vector from any of the “unnormalized” vectors on the half line! This is done simply by dividing every coordinate of the vector by its length. For every vector u, we can get the normalized vector û:

On to the discussion!

Let’s recall one of the equations for the cross product:

n is a perpendicular unit vector of a and b. Let’s assume that we supply into the equation two vectors x and y which are unit vectors too. We observe that the cross product returns the perpendicular vector scaled by the lengths of the initial vectors and the sine of their angle. Since we assumed that x and y are unit vectors, then their length is 1 and the result of the cross is n scaled by only the sine of the angle between x and y. Following this reasoning we have more observations:

  • If x and y are perpendicular, then sin(θ) is 1 or -1 and n is the unit vector. × y = 1 * 1 * 1 * n (Remember: sin(90 degrees) = 1 and sin(270 degrees) = -1)
  • If x and y are not perpendicular, then n is not a unit vector because the sine takes values in the range (-1, 1). Notice that this is an open range, meaning that the values -1 and 1 are not in the possible values of sin(θ) in this case.

With these two bullets we can distinguish the various cases in our calculations and remove the normalization of the cross product results that we know they are already unit based on their inputs.

Notice again: all the above are for unit vectors as inputs. If even one of the inputs is not normalized, then we always normalize the result of the cross product! I leave it as exercise to figure out why.

But why does all this matter?

In computer graphics, everything in its simplest form is expressed using linear algebra and that is full of points and vectors. In order to compute whichever quantity that requires direction vectors, these have to be normalized (most of the time) in order to have the correct results. A simple example:

We have a car moving with constant speed and in time t=1s it is at point P(1, 2) expressed in meters. The speed of the car is u=20m/s and is moving in the direction given by the vector v=(2, 0). The goal is to find the point that the car will be in time t=2s. Let’s look at it.

What this direction vector v tells us is that the car is moving along the X axis. If we were to employ our simple math skills, we would take the point P and add to it v scaled (multiplied) by the difference in time dt=2-1=1s and the speed.

If we actually did that, the point P2 we would get is

P2 = P+v*dt*u = (1,2) + (2, 0) * 1 * 20 = (1, 2) * (40, 0) = (41, 2)

Hmmm, that doesn’t seem right. We only moved for only one second and our speed is 20m/s. Why did the car move for 40 meters instead of 20 in our direction? That’s because our direction vector is not normalized and it contains a scale that we didn’t account for. If we normalized v, we would get v’=(1, 0) and applying it in our car movement equation we now get:

P2 = P + v’*dt*u = (1, 2) + (1, 0) * 1 * 20 = (21, 2) which is correct!

And that’s why we need to be sure about the nature of the values that our variables take. One simple solution that can free us of any tedious thinking is to normalize all our vectors and be done with it. This way we will be sure to have the correct results but we would kick our program’s performance in the back down a hill.

Fun fact: The length (magnitude) of the cross product of two vectors is 2 times the area of the triangle that is formed using those two input vectors. And there you have it, a simple way to take the area of a triangle if you already have calculated the cross product of two of its edges.

The above is derived from the fact that the length equals to the area of the parallelogram that is formed using the two input vectors.

Thanks for reading till now and hope I’ve cleared a bit some of the confusion behind the cross product.

Further and more in-depth reading for the cross product: an article from the site “Better Explained”.

Have great holidays and a Happy New Year!