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

Basic Entities Used in Vector Algebra

Scalars

Scalars are quantities with magnitude, but no direction. They're just single numbers. Length, speed, pressure, mass, temperature, area, radius, and orgone density are all scalar quantities. Traditionally, names for scalars are written in plain characters:

Let the surface area of a tin of Spam,  A  =  340cm².

Vectors

Vectors are quantities with both magnitude and direction, if they have any magnitude at all. Displacement, velocity, angular velocity, force and momentum are all vector quantities. Traditionally, names for vectors are written in boldface:

Let the velocity of an English swallow laden with coconut shells,  V  =  0.2m/s, 23° east from north with a rate of decent of 0.7m/s.

Let the velocity of a brick,  Vbrick =  11m/s east, 0.5m/s up, 3.2m/s north.

Usually, the Cartesian form is the most convenient way to express vectors. Given a Cartesian coordinate space in which x is east, y is up and is up and z is north:

Vbrick 
11
0.5
3.2
m/s

This is an example of column vector notation. This notation is more comman than row vector notation because equations involving column vectors are more readable than the mess of ticker tape that comes from using row vectors. The choice of one of these two notations is not just cosmetic. Matrix equations have to be written differently for each. Just pretend row vectors don't exist. Much.

Sometimes it's convenient to express the Cartesian form of a vector in a paragraph of text. You can do this by transposing the vector so it becomes a row vector and appending a superscript T to show that it should be transposed again. The brick's velocity can be written as [11  0.5  3.2]Tm/s.

Vectors are commonly two or three dimensional. Four dimensional ones are common in Homogeneous 3D coordinates, vaguely alluded to much later, on a different page somewhere.

Vectors are represented graphically as arrows like this:


A Vector

Now you know what a vector is, have a peek at the vector Operations page to get a feel for the sort of things you can do with vectors.


Principal Axis Vectors

A principal axis vector, e.g. <x> is named after the principal axis it's parallel to (the x axis for our example) and has a length of exactly one unit.

For 2D space,  <x> = [1 0]  and  <y> = [0 1].
For 3D space,  <x> = [1 0 0],  <y> = [0 1 0]  and  <z> = [0 0 1].
In mathspeak, these vectors form a ‘basis’, in particular an ‘orthonormal’ one. A basis is a collection of vectors combined in various proportions to span the entirety of some space. Orthonormal means the vectors are mutually 90° to each other and each is one unit in length.

Principal axis
vectors in 2D
Principal axis
vectors in 3D

These symbols for the principal axis vectors are not universal. They are more commonly known as i, j and k.


Unit Vectors

Unit vectors are always one unit in length, like principal axis vectors, but can point in any direction. They are used to indicate direction, or used in pairs or triplets to form a new basis, a local coordinate system. The orientation of an aircraft may be described as three ‘orientation vectors’ X,Y,Z in some Cartesian space:

Define space such that x is east, y is up and is up and z is north. The aircraft is flying at 30° south from east, with neither pitch nor roll. Its orientation is defined by three vectors: X is starboard, Y is up in level flight, and Z is forward.

There's a coffee cup aboard this
plane somewhere. Can you find it?
             
X = 
⁻0.5
0
⁻0.866
,  Y = 
0
1
0
,  Z = 
0.866
0
⁻0.5

In the local coordinate system of the aircraft, where x is starboard, y is up in level flight and z is forward, a coffee cup may be located at

Clocal 
⁻1.5
0.6
⁻11.2
metres               The coffee cup is on an armrest on the left window seat, at the back of the plane.              

In the coordinate system in which the aircraft is flying in, the cup is displaced from the coordinate centre of the craft by

Cabs  =  
⁻8.95
0.6
6.9
metres               The cup is northwest from the coordinate centre of the craft. The cup is also higher than the aircraft's coordinate centre by 60 centimetres.              

This is an example of back-projection, the mysterious art of converting a displacement or direction vector expressed in a local coordinate system to a vector expressed in the coordinate system immediately outside. Don't worry about how the calculation was done just yet. Just remember that it can be done.


Normal Vectors

Normal vectors are perpendicular to something, usually a line in 2D space or a plane in 3D space. Technically. normal vectors can have any length greater than zero. In practice they are often scaled to unit length to simplify the equations that utilise them. The process of forcing a vector to a length of 1 is called normalisation, a confusing term since normal vectors don't have to be of unit length and normalised vectors aren't necessarily intended to be normal to anything.

Normals are often assigned to the faces or vertices on 3D polygon meshes, pointing outward. Lighting calculations use the normals and the direction of a light source to determine the lit colour associated with each normal.

     
Face normals used for flat shading
 
     
Vertex normals used for smooth shading
 

Face normals are easy to calculate given any two adjacent sides of a face and needn't be stored. Vertex normals can't be adequately deduced from a polygon mesh, since many different curves could fit one mesh. Consider the four central vertex normals in the dome mesh example above. By making these normals point more upward then outward, the dome would appear to have steeper sides but a much more flattened top. Mathematically speaking, normals are undefined for vertices. In computer graphics, these normals must be taken from the ideal curve the mesh is designed to approximate.


Half-spaces

Normals are sometimes used to define the orientation of half-spaces. A half-space is a line in 2D or a plane in 3D space that divides all of space into two regions, ‘this’ side and the ‘other’ side. A half space can be represented by a point that lies on the plane and a unit vector normal to the plane, or by the general equation for the plane.

                   
A half-space Three half-spaces A convex polyhedron
defined by half-spaces

A convex polyhedron (a solid shape with every pair of adjacent faces having an angle of less than 180° between them, measured from the inside) can be represented as a set of intersecting half-spaces. The viewing volume of most 3D rendering systems, a truncated frustrum of a pyramid is represented by six half-spaces. One for the near clipping plane, one for the far one, and four for the sides.


Six half-spaces define a typical viewing frustrum. Viewing frustrums,
like the ‘rooms’ in portal renders, have the normals pointing inward.

Some computer games store the geometry of the world in a binary space partition tree. Some bit of a wall's face in the game has a half-space associated with it that divides all of space into two regions. In each of those regions, another face is attached to a half-space that divides that region into two subregions. Once every single face has its own half-space, it's easy to render a scene without needing to sort the polygons into front-to-back order. The need for sorting is eliminated by the fact that all objects on the same side of a half-space as the observer are never obscured by any objects on the other side.

BSP trees can also be used for efficient collision detection.


The General Form of a Line Equation

Most people familiar with graphs and gradients will have no trouble looking at a graph like

and coming up with a line equation like
y  = 
2
3
x + 1
but are often puzzled when they see a maths lecturer write for the same graph,
x + 3y  =  3.

This is the general form of the line equation. Maths people like this form because it doesn't favour just one axis like y and does not have trouble representing vertical lines. (Try expressing x = 2 with an equation of the form “y = …”)

To write a line equation in the general form, first find a vector that is normal (i.e. perpendicular) to the line. For our example line, [⁻2  3]T is a such a vector. Use the vector's terms as coefficients for x and y so you have part of the general equation worked out:

⁻2x + 3y  =  something.

If ‘something’ was zero, the line would pass through the origin. We know it passes through the point (0, 1). By making x = 0 and y = 1 and solving for ‘something’, we see that ‘something’ is 3.


The General Form of a Plane Equation

The general form of a plane equation in 3D is trivially different to the general form of a line equation in 2D:

ax + by + cy  =  d.

If a plane passes through C and N is normal to it,

Nx(x − Cx) + Ny(y − Cy) + Nz(z − Cz)  =  0
or more neatly,
Nxx + Nyy + Nzz  =  NxCx + NyCy + NzCz.

Left and Right Handed Coordinate Spaces

With three mutually perpendicular axis vectors in 3D space, there are two different ways to arrange them: Left handed and Right handed. A coordinate space of one hand cannot be rotated, uniformly scaled or moved in any way that makes all of its axes lie over the corresponding axes of a coordinate space of the other hand.

A handy mnemonic for constructing a right handed space is to straighten the fingers of your right hand and point them in the direction you want the x axis to point. Curl your fingers by 90° to point in the direction of the y axis. Stick out your thumb. That's where the z axis must point.


Use your right hand like this to
define a right handed space.

Right handed spaces are most commonly used by mathematicians, physicists and architects. Three popular right handed spaces are:

x is right, y is up and z is back towards the viewer;
x is right, y is away from the viewer and z is up; and
x is east, y is north and z is up.

Some people (game programmers, perhaps?) prefer left handed spaces instead. Most often, the axes are oriented such:

x is right, y is up and z is away from the viewer; or
x is starboard, y is up and z is foward.

A useful left handed world coodinate orientation is:

x is east, y is south and z is up.

Such an orientation makes the addressing of a 2D array representing a height map of a simulated world a bit easier to code, since y increases for each successive row of the array. Arrays are traditionally written left to right, top to bottom.

It's vital that once you choose between left handedness or right handedness for your coordinate systems, you stay with that choice. Changing hands within an application can be very messy.

If you intend to model electromagnetism, it's a very wise move to stick with a right handed coordinate system. If you really want to use a left handed system, you'll have to reverse the sign of parts of some formulas that deal with three perpendicular vectors or perhaps change the definition of charge or current or magnetism. It's not a very nice thing to do and you'll probably get headaches doing it.

It's possible to model classical mechanics in a left handed system, but the display of torque vectors, for instance will appear backwards to a physicist. You really don't want to frustrate a physicist whose help you might need to debug your simulation program.


Rotation vectors

Angular velocity (rate of spin), angular momentum angular acceleration, torque and even orientation can each be described with a vector. Consider an angular velocity vector w (‘omega’). The length of w is the rate of spin of some body, usually measured in radians per second (there are about 2p » 6.283185307 radians in a circle). w points in the same direction as the axis about which the body is spinning.

The rotation vectors illustrated above are drawn for a right handed space. With your right hand, curl your fingers in the direction of rotation and stick out your thumb. Your thumb will point in the direction of the rotation vector. For left handed spaces, use your left and instead.

The neat thing about rotation vectors is that they can be added together. See the operations page if you don't already know how to add vectors. Say you're flying an aircraft due north. Consider two manoeuvres: A 90° roll to the left and a 90° upward pitch. If the roll is followed by the pitch, the plane would be flying due west with its starboard wing pointing up. If instead, the pitch is followed by the roll, the plane would be flying straight up with its starboard wing pointing south. What if the two manoeuvres were performed simultaneously? Using a left handed space where x is starboard, y is up and z is foward:

Mroll-left 
90°
,  Mpitch-up 
⁻90°

Mcombined 
⁻90°
90°
  ≈  127.3° about an axis that runs port-forward (45° left of forward). 

After the simultaneous manoeuvres, the plane would be flying upside down, flying in a direction that is partly west, slightly north and mostly up. The starboard wing would be mostly in the southeast direction.

This kind of rotation vector is great for dealing with simultaneous torques upon an object, though it's more often the case that sequences of rotations need to be modelled. Matrix multiplication, discussed later, can be used to perform such sequences.


Matrices

Matrices are vectors on steroids. The single most common use for a matrix is to apply some transformation to a bunch of vectors, i.e. move, scale, skew or rotate them. The neat thing is that all these operations can be combined in any sequence and expressed as a single matrix. The transformation is effected by matrix multiplication, which will be discussed in detail later. It's important to note that most matrices have an inverse, another matrix that can be used to undo whatever the first matrix did to the vectors.

Matrices look a bit like vectors, except they can have many rows and many columns. A vector is actually a special case of a matrix. Here's a 3×3 matrix:

I
1  0  0
010
001

This is an identity matrix. It's a bit special. when any 3D vector or 3×3 matrix is multiplied by I, nothing happens in pretty much the same way nothing happens when you multiply any number by 1. Much more interesting matrices are described on the following pages.


Now What?

Read the pages on Operations and Matrix operations to find about stuff you can do with matrices and how to impress girls at parties.