The Dot Product (inner product)

 

This is one of, if not the most important operator(s) in geometry. It is a dimensionless operator in that it doesn't require a particular number of dimensions to be valid (unlike the cross product, which is only valid in 3d).

The text book definition is:

U.V = ||U||*||V||*cos θ

Which says that the dot product of two vectors is equal to the magnitude of each multiplied together all multiplied by the cosine of the angle between them. Of course, if you start out with unit vectors then the magnitude disappears from the equation (it is simply 1) and the answer is directly equal to the cosine of the angle between them.

This in itself is useful because we can then calculate the angle θ in radians by inverting the equation:

θ = cos-1U.V

or in code:

θ = acos(U.V)

 

The result of a dot product of two vectors can be visualised as being in either the positive or negative horizontal half space with the two poles being +1 and -1 with 0 at 90o either side:

 

So when two unit vectors point exactly the same way, the result of the dot product is 1 and when they are exactly opposite -1. The dot product returns 0 for any two vectors (unit or otherwise) at 90o to each other in some plane.

 

The really useful part comes when we realise that this geometric description also allows us to tell how much of a vector is in the direction of another, because when we know that we can calculate this new projected vector which is useful in all sorts of geometric problems, rotation being the most fundemental.

 

Projection

 

What is projection? It is the procedure of taking something in n dimensions and collapsing it down to the dimension below (n-1). So in 3d rendering we often want to project the 3d world onto the near plane of the camera. When we do a dot product we are projecting one vector onto another resulting in a scalar.

We start out with two vectors, one being unit (A) and the other not (V). A is the projection axis and the V the vector we want to project onto this axis. We can get the distance we need to travel in the direction of the axis in order to be as close as possible (on the axis) to the tip of the other vector, thus:

d = V.A

We can even get the location of the closest point on the axis:

P = A*d

 

Because A was a unit vector, the projection of V onto it resulted in a quantity with the same length units as V. I.e. If they both pointed in exactly the same direction the dot product tells us the length of V.

If both vectors were unit length we get the same result except that the lengths drop out of the equation and we are left with the cosine of the angle between the two vectors.

 

Optimisation

If neither vector is unit length we can still find d, but it will be in parametric units along A (i.e. from 0 to 1):

 

d = V . A / A . A

 

You can see why this works if you consider what happens when V = A. The first quantity is the squared length of both vectors and so is the second quantity; thereyby giving a value of 1, which corresponds to the end "time" along the axis. Similarly total squared length divided by half the squared length = 0.5, which is exactly what we expect.

Often this is more useful than having d in axis length units, which is nice.

 

Rotation

 

Projection is the fundemental principle which makes rotation possible. Rotation is a length preserving operation and you will be unsurprised to hear that unit vectors and thier dot products are the key.

We cannot start talking about rotation without first discussing the concept of spaces. Everything starts out in what I will refer to as world space. This is the coordinate system within which everything else sits and it has the identity matrix as its rotation; its axis are simply the world axis x and y. Everything that we draw in a simulation has to be drawn in the same space for it to make sense when we see it, therefore we often look for rotations that go into world space and other rotations which go back again. An object in our 2d world will have a rotation matrix which describes how to rotate from object space into world space. And if you draw the matrix it is easy to see what the numbers actually represent.

 

In 2d a rotation matrix M is defined by two orthogonal unit vectors which describe the up and rightwards directions in 2d space. The identity matrix in 2d is:

[1, 0]

[0, 1]

This simply says that to move rightwards one metre, you step 1 unit in x and 0 units in y. Similarly to go up one metre you step 0 units in x and 1 unit in y. Note that I'm going to be talking about rotation in this section rather than transformation since we are talking about Vectors rather than Points.

Two common rotations which you might want to do to a vector are: rotating it from world space into object space and the opposite, transforming from object space into world space.

Rotating a vector from world space into object space

V starts out in world space and we want to find its coordinates in object space of the matrix M:

 

V' = M*V

[R_x*V_x + R_y*V_y]    [R_x, R_y] [V_x]

[U_x*V_x + U_y*V_y] =  [U_x, U_y] [V_y]

or as vector operations:

V_x' = V . R

V_y' = V . U

Here we are projecting V onto our right and upwards vectors in order to find the new x and y components of V in the space of M.

Its easy to see in this applet what is going on - drag mouse to rotate basis.

 

 

You can see that when the basis matrix is identity V.R and V.U represent what we might think of as the x and y coordinates of V. However, now we know that what we have is actually two scalars which represent a linear combination of the basis vectors.  In matrix speak we have rotated V by the transpose of the matrix M to yield a vector V', in the space of M.

It is easy to see how we can find V' in world space now:

V_x' = V.R

V_y' = V.U

V = R*V_x' + U*V_y'

 

Rotating a vector from object space into world space

Now that we have shown that a vector in object space represents a linear combination of the basis vectors of the object's matrix, it will be no surprise that when we want to find a vector in world space (which was in object space) we just have to multiply the basis vectors of the matrix by the "weights" which are the vector's x and y components, as above and sum.

If we have a vector V in the object space of a rotation matrix M, we can rotate V about M and into world space:

(I have replaced M with right and up vectors R and U):

 

V' = R*V_x + U*V_y

 

or in matrix parlance:

 

V' = V*M

[V_x, V_y] [R_x, R_y]

           [U_x, U_y]

V_x' = V_x*R_x + V_y*U_x

V_y' = V_x*R_y + V_y*U_y

 

There is no applet to show this because you can see the result in the applet above - just imagine the sum of the vectors formed by the origin and the two points on each axis.

The two principles above also apply to matrices in a logical fashion; just by treating the matrix to rotate as a set of vectors and operate one at a time as above, remember the rows of the matrix are the basis vectors.

You should notice that in these two operations the order of matrix times vector is exchanged, this enables us to perfrom the operation without needing to explictly transpose one of the matrices in question.