Home About Me Notes Galleries Files Links
Cover Programming Maths Odd Bits
Cover Vectors
Cover Primitives Operations Matrix Operations Homogeneous Matrices

Operations in Vector Algebra

Addition

Vector addition is what you use to solve problems like:

A duck, with the aid of a jet ski, would head north at 12m/s if the water it was moving over was still. The water, however, is flowing at 4m/s to the southeast. What is the velocity of the duck, relative to the ground?

To solve this geometrically, choose a starting point and add all the vectors contributing to the ducks motion, end to end and see where the head of the last arrow ends up. Draw a new arrow from the starting point to the last arrow's head. That's the ‘true velocity’ of the duck.

    
The jet ski's (relative) velocity, J and the water velocity, W
The sum of the two velocities, V

To solve this numerically, starting with

J  = 
0
12
m/s
W =  4∠⁻45°  =  
4cos(⁻45°)
4sin(⁻45°)
m/s   ≈  
2.828
⁻2.828
m/s

simply add the terms from one vector to the corresponding term of the other, i.e.

V  =  J + W
V  ≈ 
0
12
m/s  +  
2.828
⁻2.828
m/s
V  ≈ 
2.828
9.172
m/s

Negation

To negate a vector, simply make it point the other way.

A A

Numerically,

if   A  = 
⁻1.2
12.5
7
   then   ⁻A  = 
1.2
⁻12.5
⁻7

Subtraction

Often you will want to know the position of one object relative to the position of another, as if that other object was the epicentre of the universe. Sometimes you will want to know how far (and in which direction) an object was displaced, given its old and new coordinates (a set of coordinates to describe an object's coordinates is often written as a position vector).

Let's say you have a sausage that was once at P0 but is now at P1. Evidently, the sausage has been displaced. The displacement vector D can be calculated easily:

D = P1P0

As with simple maths, P1P0 has the same value as P1 + ⁻P0. Subtracting a vector is the same as adding the negative of that vector.


In calculating P1P0 geometrically, you may find that you've drawn D as an arrow that starts from the origin and extends to the right. That is still the correct value for D. vectors in themselves have no position, only size and direction. A drawing of a vector does by necessity have a position, and it's up to the person drawing the graph to position (drawings of) vectors like D where they will most easily be understood.


Length

Finding the length of a vector, also known as its norm, is straightforward. First find the square of all the terms (multiply each term by itself), add the squares together, then find the square root of the sum. It's basically the Pythagoras rule.

The length of a 3D vector,  ||A||  =  sqrt(Ax² + Ay² + Az²)

Let  A  = 
⁻2
0.3
1
||A||  =  sqrt( ⁻2×⁻2 + 0.3×0.3 + 1×1 )
||A||  =  sqrt(4 + 0.09 + 1)  =  sqrt(5.09)
||A||  ≈  2.256

The square root function is computationally expensive, and many useful vector equations use the length squared of a vector, which is a much easier quantity to compute:

The square of the length of a 3D vector,  ||A||2  =  Ax² + Ay² + Az²

Be careful not to confuse the ‘length of’ operator, written as double bars around a vector, with the ‘absolute value of’ operator, which is written as single bars around a scalar quantity.


Scaling

When a vector is multiplied by a scalar, the vector grows by a factor of that scalar. Growing by a factor of less than one is commonly called ‘shrinking’.


Most often, scaling is applied to unit vectors so that the length of the resulting vector is the same as the scale factor. The direction of a gun barrel can be represented as a unit vector, and the initial velocity of a bullet fired from it is simply the direction vector scaled by the muzzle speed of the bullet.

The procedure for scaling a vector is trivial. Simply multiply each term by the scalar.

A 3D vector scaled by s,  sA  =  
sAx
sAy
sAz
Let  A  =  
⁻4
1.2
⁻5
Let   s   =  2.5

sA  = 2.5 
⁻4
1.2
⁻5
  =  
2.5×⁻4
2.5×1.2
2.5×⁻5
sA  = 2.5 
⁻10
3
⁻12.5

Normalisation

Often you will have a vector of some length, representing the relative position of one object from another, say, and want to turn it into a direction vector, i.e. a vector with a length of 1. Scaling a vector so that becomes one unit in length is called normalisation. The figure below shows some vectors and their normalised counterparts.


To normalise a vector, scale it by the inverse of its length. The inverse of a number is just 1 divided by that number.

normalised(A)  =  
1
||A||
A

A vector of zero length cannot be normalised, since you cannot meaningfully divide any number by zero.

Be careful to note that the concept of normalised vectors is different to the concept of normal vectors. Normal vectors are often normalised but not always. Normalised vectors aren't necessarily normal to anything.


Dot Product

The dot product is used just about everywhere in vector maths and 3D graphics programming. It's incredibly useful and it's easy to compute. A dot product of two vectors can be used to:

The dot product of two vectors is a scalar. This scalar is a product of the lengths of both the vectors and the cosine of the angle between them.

A dot product of two vectors,  d  A·B
=  ||A|| ||B||cosθ
where θ (‘theta’) is the smallest angle between the vectors

The dot product is trivial to compute. Multiply each term of one vector with the corresponding term of the other and find the sum of the products of these multiplications.

A dot product of two 3D vectors,  d  A·B
=  AxBx + AyBy + AzBz

The cosine factor of the dot product is often needed for tests to determine if something is close enough to being in the same direction as something else. By rearranging one of the above equations:

cosθ  =  
1
||A|| ||B||
A·B

If it is known the vectors A and B are of unit length, then the part of the equation that strips the lengths of the vectors from the result of the dot product can be eliminated:

For unit vectors,  cosθ  =  A·B

A cosine can be regarded as a measure of how parallel two unit vectors are. If the cosine is 1, the vectors are perfectly parallel. If the cosine is 0, the vectors are perpendicular. If the cosine is ⁻1, the vectors are perfectly antiparallel, i.e. pointing in opposite directions. If the vectors are 45° apart, say, the cosine will be about 0.7071.

To find the angle a cosine represents, you must use the trigonometric function arc-cosine, or inverse-cosine.

     if  cosθ  =  0.866
then  θ = cos⁻¹(0.866)  =  30°

Trigonometric functions like cos and cos⁻¹ are transcendental and therefore computationally expensive. Fortunately, the need to express angles directly is rare. Many useful functions that would use an angle tend to just want the cosine of it anyway.


Testing for Direction Within a Given Angle

An acceptance or rejection cone can be created by having:

To find if any vector is pointing in a direction 'inside' the cone, normalise it if it's not already a unit vector, and find the dot product of it and the cone's direction vector. If the result is a number greater than the acceptance cone's cosine, the vector is 'in'.

To find if any two vectors are even slightly pointing in opposite directions, ie greater than 90° apart, simply examine the sign of the dot product of the two vectors and see if it's less than 0. It's not necessary that the vectors be normalised for this test.


Finding Tangential and Normal Components of a Vector

The altitude of a point above a plane, where 'up' is perpendicular to the plane, can easily be found if the plane's normal and the position of any point that lies on the plane is known.

Let there be some point. Its position is given by the vector P.
A plane with a normal vector, N, passes through another point, C.

The problem's a bit simpler to solve if we move the coordinate system so that its origin is located where C is. To do this, subtract C from all position vectors:

P′  =  PC
C′  =  CC,  the new origin
 

The altitude of P′ is simply a dot product of P′ and the plane's (unit-length) normal:

The altitude of P′,  v  =  P·N

Note that v is just a scalar. We really want a vector that describes the component of P′ normal to the plane. This component is simply the plane's (unit) normal vector scaled by v:

The normal component of P′,  V  =  vN

V can be said to be a shadow of P′ cast onto N.

Now we want the component of P′ tangential to the plane. Since P′ is the sum of its normal and tangential components and we know the value of P′ and V, the normal component, the tangential component can be calculated by vector subtraction:

The tangential component of P′,  H  =  P′ − V

Here's an example illustrating the use of component vectors:

Given say, the velocity of a perfectly bouncy ball, it's easy to calculate its new velocity when it strikes a surface for which a normal vector is defined. Simply calculate the normal and tangential components of the original velocity, negate the normal component and reassemble these components to form the new velocity.

Let the incident velocity be  T.
Let the surface normal be a unit vector,  N.

The normal component of  TV  =  (T·N)N
The tangential component of  TH  =  TV

The incident velocity, expressed as the sum of its components is  H + V

The reflected velocity,  R  H + ⁻V
=  
T − (T·N)N
 + ⁻(T·N)N
T − (T·N)N − (T·N)N
T − 2(T·N)N 

Projection of a Vector onto Local Axes

Imagine a local 3D coordinate system with its origin centred on the absolute coordinate system's origin, but with its orientation defined by three (mutually perpendicular) axis vectors X,Y,Z. Imagine also a point whose position vector P is measured in absolute coordinates. We want to project P onto P′, a vector that gives the position of the same point but in terms of the local coordinate system. To do this, simply find the dot product between P and each of the axis vectors and assemble the products to form the projected vector, P′:

P  =  
P·X
P·Y
P·Z

Each dot product represents the sign and length of the 'shadow' the vector P casts on a local axis.

If we knew the position of the point in local coordinates (P′) and we wanted to calculate its position in absolute coordinates (P), we would simply scale the local axes by the terms of the local position vector and add them together:

P  =  P′x(X) + P′y(Y) + P′z(Z)

This trick is called back projection.

Often local coordinates of interest don't have their origins neatly positioned over the absolute one and must be temporarily 'moved' to the origin for the above equations to work. To project vectors into a displaced local coordinate space, subtract the local origin's position from the vectors before performing the projection. To backproject vectors from such a space into the absolute space, add the local origin's position to the vectors after backprojecting them.

This tedious mucking about, adding and subtracting vectors during changes of coordinate systems, can be avoided by using homogenous matrices, discussed much later.


Perpendicular (2D Operator)

In some obscure texts, a superscripted upside-down T appears after the name of a 2D vector. This operator usually means 'the vector rotated by +90°. Remember that rotation is always positive in the same direction the x axis would rotate by 90° to coincide with the y axis, in a coordinate system of any hand, in 2D or 3D.

Numerically, the perpendicular operator swaps the terms of the vector and then negates the x component:

if  A   =  
7
2
then  A  =  
⁻2
7

In general:

A   =  
⁻Ay
Ax

One way of defining the perpendicular operator is the determinant mnemonic, which is actually an abuse of mathematics, having vector quantities for some of the elements of the matrix. Determinants are beyond the scope of these vector-maths-for-vegetables pages but the mnemonic may still prove useful to vegetables who happen to be able to evaluate determinants using Cramer's Rule, say:

A   =  
Ax    Ay
<x><y>
=  
⁻Ay
Ax

Cross Product (3D)

The cross product of two vectors yields a vector that is perpendicular to both vectors and has a length that is a product of the lengths of both the vectors and the sine of the angle between them. Use the left or right hand rule to find the direction of the cross product as if you were finding the z axis, given the direction of the x and y axes.

The cross product operator is used extensively in phsyical simulation. Imagine a billiard ball floating in space. A force F of some magnitude and direction acts on the ball at r, relative to the ball's centre of mass (which, for a billiard ball, is at its geometric centre). How much torque (a 'twisting' force) is imposed on the ball? In which axis will the ball start to spin?

The torque vector is calculated as a cross product of r and F:

τ  =  r × F

The torque vector, τ, if it has any size, will be in a direction perpendicular to both r and F, indicating the axis about which the billiard ball will start to spin. The length of τ is the amount of torque in Newton-metres, if you're using metric units. Note that the torque effect is greatest when r and F are 90° apart, and zero when r and F lie on the same axis. Pushing a body towards its centre of mass does not make it spin.

I chose a billiard ball for this example because it has a simple inertia tensor and can be trusted to spin on the same axis as its angular momentum, the thing you really change when you apply a torque to a body.

The cross product formula can be memorised using the determinant mnemonic shown below. It's only a mnemonic because you're not supposed to stick vectors inside matrices at all, let alone expect to find a meaningful determinant of such a amtrix.

A × B   
=   
Ax  Ay  Ax
BxByBz
<x><y><z>
=   <x>
Ay  Az
ByBz
  −   <y>
Ax  Az
BxBz
  +   <z>
Ax  Ay
BxBy
=   <x>(AyBz − AzBy)  −  <y>(AxBz − AzBx)  +  <z>(AxBy − AyBx) 
=   
AyBz − AzBy
AzBx − AxBz
AxBy − AyBx

Don't worry if you have no idea what a determinant is or how to calculate one in general. You probably won't have to till you want to invert a matrix or something horrible like that.

Remember that the cross product operation is not commutative; B×A is not equal to A×B. In fact, B×A = ⁻A×B.

Many people consider the cross product to be a geometric operation, that East × North = Up whether you use a right or left handed coordinate system. In this case, the above cross product formula must be negated to produce the correct result in left-handed space. That would mean, though, that <x> × <y> = ⁻<z>. You might want to avoid this issue by keeping to right handed coordinate systems.


Matrix Operations

Matrix operations, in particular matrix multiplication is discussed on the next page.