5. Maths Vector Dot Product

Vector Dot Product

Here we are going to be looking at the infamous dot product AKA scalar product.

The dot product crops up all over computer graphics. You can use it to compare directions, work out angles, calculate diffuse lighting, test whether vectors are perpendicular and a whole heap of other useful things.

Quick correction from the old version of this tutorial: the dot product does not create a surface normal. The cross product is the operation normally used to generate a vector perpendicular to two other vectors. The dot product gives us a single number – a scalar – which tells us how much two vectors point in the same direction.

That is a much better meaning than my original “I couldn’t really find a meaning to what the dot product actually is” explanation 🙂

The Formula

There are a couple of useful ways of writing a dot product equation.

For two 2D vectors a = [a1, a2] and b = [b1, b2]:

a . b = a1 * b1 + a2 * b2

And geometrically:

a . b = |a| |b| cos(theta)

Where theta is the angle between vectors a and b.

If the angle between a and b is 90 degrees, then cos(90) is 0, so the dot product is 0. That gives us a very handy test for perpendicular vectors.

Let’s Have A Little Play With That

Say we have the vectors:

AB = [4, -2]

BC = [-1, -2]

And we want to find out if AB is perpendicular to BC. We now have to do the dot product. We could use the angle formula if we already knew the angle and lengths, but for these component values the first equation is basically plug n play: whack in the numbers and let the calculator do its thing.

So to find the dot product we get:

AB . BC

AB . BC = (4 * -1) + (-2 * -2)

AB . BC = -4 + 4

AB . BC = 0

Therefore AB is perpendicular to BC.

And that is all there really is to the infamous dot product. Once you know what that single number is telling you, it is a rather simple concept really.

Questions or corrections? If something doesn’t work or you spot something I’ve got wrong, leave a comment below. These tutorials have been around a long time, so useful corrections are always welcome!

  • March 25, 2010