Ultrashock Tutorials > Flash5 > Vectors  
 
by: Brandon Williams


 
Vectors
 

If there was one entity that has proved itself enormously useful in nearly every area of mathematics and physics, it would be the vector. The term "vector" was coined by an Irish mathematician named William Rowan Hamilton.

In math and science we come across quantities that only have a magnitude, meaning they can be expressed with just a number. For example, the mass of an object is expressed with merely a number. Such quantities are called scalars. They are numbers and nothing more.

However, some quantities need something more than just a magnitude. Take for example the velocity of a car. Not only can a car drive at 30 miles per an hour, but it also moves in a direction. The car could travel north, south, east, west, or some combination of the four. Quantities that can only be described with a magnitude and a direction are called vectors. The manipulation of vectors is what most of this article will be about.

Notation of Mathematical Quantities

The notation of the quantities introduced so far is quite important so that you can differentiate between scalars and vectors. A scalar will be written as a lower-case italic letter. A vector will be written as a lower-case bold letter. The case of the letter is important because later on when matrices and bases are introduced they will be written as upper-case bold letters.

Graphical Representation of a Vector

A vector is best draw as a directed line segment. In geometry you have line segments, which are just two points connected by a line. A directed line segment means that the regular line segment simply points in a direction.



That is pretty much the only way to visualize a vector: an arrow pointing in a direction. If we relate this picture back to the car example then the vector points in the direction that the car is moving and the length of the vector would be how fast the car is traveling.

The Anatomy of a Vector

In the picture of the vector that are a few terms to learn that relate to its anatomy.



The end of the vector without an arrow is called the tail, and the other end, with the arrow, is called the head. The length of the vector is called the magnitude, and the angle the vector makes with the horizontal is called the direction of the vector.

Vectors are Independent from Position

It is important to note that the "position" of a vector has no meaning or relevance. Does the "position of a car's velocity" really mean anything to you? When talking about forces, which happens to be a vector, it is sometimes important to know the point on a body that the force is being applied, but that merely introduces another vector rather than the position of a vector. All of the following vectors are equivalent:



A Vector as a Coordinate

Most of the times it is tedious and an overall pain to deal with vectors in terms of their magnitude and direction. For this reason we deal with vectors as a coordinate where the tail of the vector is placed at the origin (0, 0) and the head at some coordinate (x, y). When we think of vectors like this we can write them down in the following fashion:

v = <x, y>

The x and y scalars measure how much along the x- and y-axis you have to travel down before you reach the head of the vector.



This is how vectors will be represented for the rest of the tutorial. The scalars x and y are called the components of v along the x- and y-axis. Even though it is generally better to deal with vectors in terms of their components it is sometimes useful to find the vector's magnitude and direction from the componets. We can find the magnitude of the vector by realizing that the length is the distance from the origin to the head of the vector. Therefore we use the Pythagorean Theorem and treat the components of the vector as the legs of a right triangle and we get the following:
          _________
||v|| = \/ x2 + y2

The two bars on either side of v is used to denote that we are take the magnitude of the vector. The magnitude of the vector is usually called the norm of the vector, and is the name we will be using from this point forward.

To find the direction of a vector we once again take note that the vector is a right triangle with x and y as its legs. The angle q can be found by taking the arctangent of the ratio of y to x. This follows directly from the definition of tangent and arctangent.

q = arctan (y / x)

Just as it is useful to find the norm and direction of a vector from knowing the components, it is also useful to find the components of a vector from knowing its norm and direction. We can do this from knowing the sine and cosine of the direction. Here is a picture of the situation:



We know the values of q and ||v|| and we want to find the values of x and y. We also that from the above picture that the sine and cosine of q is defined as:
sin q = y / ||v||
cos q = x / ||v||

The x and y values from the above equations can be found by multiplying boths by ||v||. Doing that gives the final equations for finding the components of a vector given its direction and norm.

x = ||v|| cos q
y = ||v|| sin q

Vector Arithmetic

Just as we have done arithmetic with scalars since first grade, vectors have a form a arithmetic themselves. We will first do addition. Vector addition is usually done graphically first, but those methods are never used in programming so I won't waste your time. For us, and for every situation you will come accross, vector addition is simply the sum of the two vectors' components.

a = <x1, y1>
b = <x2, y2>
 
a + b = <x1 + x2, y1 + y2>

You can visualize vector addition one of two ways. If you keep the tail of both vectors at the origin then the sum of the two vectors goes from the origin to the vertex of a parallelogram created by a and b.



Another way, which is essentially the same as the above, is to place the tail of b on the head of a and connect the tail of a with the head of b. It may be a mouthful but it is quite simple.


You will never be asked to add to vectors by using the two above methods (outside of a math class) so do not worry about it. Nevertheless it is an interesting property.

Scaling a Vector

Arithmetic of scalars also involves multiplication. With vectors there is no one defined way of doing vector multiplication; instead there are three different ways. The first way is the multiplication of a vector with a scalar. To do this you multiply each component of the vector by the scalar.

a = <x, y>
 
a * s = <x * s, y * s>

Graphically this is only a scaling of the vector. If you multiply a vector by the scalar 2 then the new vector is twice the length of the old vector. Also, mutiplying a vector with a positive scalar does not change the direction of the vector. But, if you multiply by a negative number it will flip the vector, thus changing the direction.



With this definition of scalar-vector multiplication formed we can also define vector subtraction. Vector subtraction can be thought of the same as vector addition except for one small difference: the vector being subtracted from another is negated (multiplied by negative one). Something like this:
a - b = a + (-b)

Therefore we define vector subtraction like this:

a = <x1, y1>
b = <x2, y2>
 
a - b = <x1 - x2, y1 - y2>

We can represent this graphically just as we did will vector addition:



If you were to draw out some more vectors and perform subtraction on them there is an important property that you will see. If we position the vectors a and b so that they share the same point for their tale then b minus a can be interperted as the vector pointing from b's head to a's head.

There are two vectors that are given special names. These vectors have a length of one and point int the direction of the increasing x- and y-axis. Mathematically, they are defined like this:
i = <1, 0>
j = <0, 1>

Many times it is convient to write a vector as a sum of its i and j components. This is done by scaling the i and j vectors until their sum is equal to the vector you are trying to obtain. For example, the vector <3, 2> could be written as 3i + 2j. In general, a vector <x, y> can also be written as xi + yj. Individually i and j are called unit vectors, and grouped together they are called the standard basis vectors. Bases will be something covered in another tutorial and you will see why that name is given to these vectors.

Unit Vectors

From the arithmetic of scalars you know that if you divide a number by itself you get the number one. Well, a similar operation can be done to vectors except the outcome is much more useful. If you multiply a vector by the reciprocal of its norm then you get what is called a unit vector. A unit vector has a norm of one.

v = <x, y>
 
u = (1 / ||v||) * v
 
           ________
  = (1 / \/ x2 + y2) * v

Unit vectors are very useful in many different situations. The first peculiarity that we will look at with unit vectors are its components. By looking at the definition of a unit vector above we can see that the individual components come out like this:

           ________
ux = x / \/ x2 + y2
           ________
uy = y / \/ x2 + y2

Therefore the components of a unit vector are obtained by dividing each component by the norm of the vector. Doing this is actually the same as doing the "opposite over hypotenuse" or "adjacent over hypotenuse" familiar with sine and cosine. This means that the x-components of a unit vector is the cosine of the vector's direction, and the y-component of a unit vector is the sine of the vector's direction.

u = <cos q, sin q>
Many times it is best to represent the "direction" of a vector with the vector's unit vector rather than its angle with the horizontal. For example, what if you wanted a vector in the same direction as v = <1, 2> but with a magnitude of four exactly. You may start of by finding the angle that v makes with the horizontal, then find the sine and cosine of that angle, and finally multiplying the sine and cosine by the new length to find the components of the new vector. However, with what we know about unit vectors we can simply take the unit vector of v, and scale it by four, thus giving us a new vector with a length of four.
          _ 
||v|| = \/5
           _
u = (1 / \/5) * <1, 2>
 
  = <.447, .894>
 
w = 4 * u
 
  = <1.788, 3.578>

The vector w is now in the same direction as v except with a magnitude of four. You can double check that by find the norm of w.

In general, you can change the length of vector v to a by doing this:

          v
w = a * -----
        ||v||

Dot Product

Another form of multiplication for vectors is called the dot product. The dot product is defined as follows:

v = <vx, vy>
w = <wx, wy>
 
v · w = vxwx + vywy

Note that the dot product of two vectors is a scalar. A dot product does not return a vector.

The best geometric interpretation of the dot product is the angle between two vectors. The dot product relates the cosine of the angle between two vectors with the following equation:

v · w = ||v|| * ||w|| * cos q

Although we are skipping all mathematical rigor for this tutorial it can be said that the above is derived from the law of cosines.

By re-arranging the above equation we can explicitly solve for the cosine of the angle between two vectors:

             v · w
cos q = ---------------
         ||v|| * ||w||

Finally, we can also use the dot product to determine whether or not two vectors are perpendicular (orthogonal). Othogonal is used when two things meet at a right angle (90 degrees or p/2 radians). Plugging in the known angle into the dot product we see this:

v · w = ||v|| * ||w|| * cos 90
 
      = ||v|| * ||w|| * 0

      = 0

This implies that if the dot product of two vectors is zero then they are orthogonal. This is a very important property and it will be used later on.

Projection

A common situation that occurs when dealing with vectors is find the component of one vector along another. This is known as a projection. There are four types of projection that can be very useful: parallel scalar projection, parallel vector projection, perpendicular scalar projection, and perpendicular vector projection.

The parallel scalar projection is like dropping the shadow of one vector onto another. The parallel vector projection is basically the same except it is a vector in the direction of the vector being projected onto with the norm of the scalar projection. The perpendicular scalar projection is the shortest distance from the head of one vector to the vector being projected onto. The perpendicular vector projection has a norm equal to the perpendicular scalar projection and is orthogonal to the vector being projected onto.

The picture below will help most with developing what these quantities mean. Fortunately all are very simple to derive. To start off with we will find the parallel and perpendicular projection equations. First here is a picture:



The notation "proj b a" is used to denote the parallel scalar projection of a onto b, and "perp b a" is used to denote the perpendicular projection of a onto b. Since the triangle made up of the parallel and perpendicular projections and a is a right triangle we can use what we know about cosine to solve for proj b a:
// We can start by defining the cosine of the angle
between the two vectors in two different ways a · b cos q = --------------- ||a|| * ||b|| proj b a cos q = ---------- ||a|| // Set the equations equal to each other and solve for proj b a

a · b proj b a --------------- = ---------- ||a|| * ||b|| ||a||

// Solve for proj b a and simplify
a · b
proj b a = ------- ||b||

Therefore, the parallel scalar projection of a onto b is simply their dot product divided by b's magnitude. Knowing this you can then solve for the perpendicular scalar projection by using the Pythagorean Theorem since you know two sides of the right triangle.

             _____________________ 
perp b a = \/ ||a||2 + (proj b a)2

Later on we are going to define another operator on vectors that will help us with a more simple equation for calculating the perpendicular projection.

The two scalar projections we have calculated so far are merely the norms of the vector projections. The vector projections will add a direction to the scalar projections we have given equations to so far. We will denote the vector projections in the same way as the scalar projections except "proj" and "perp" will now be in bold. The easiest to do is the parallel vector projection since it is in the direction of b. To calculate the vector projection scale the unit vector of b by the scalar projection:

                        b
proj b a = proj b a * -----
                      ||b||
  
            a · b      b
         = ------- * -----
            ||b||    ||b||

And the parallel vector projection has been calculated.

Something similar can be done to find the perpendicular vector projection. First, we must find a unit vector in the direction of the perpendicular projection. This is equivalent to the parallel vector projection minus a (remember the property of vector subtraction such that the difference is a vector pointing from one vector head to another). Therefore the perpendicular unit vector can be expressed in the rather cumbersome equation:

      proj b a - a
u = ----------------
    ||proj b a - a||

Finally, scaling this unit vector by the perpendicular scalar projection you get the final equation for calculating the perpendicular vector projection (this equation is very long but still simple):

 
                        proj b a - a
perp b a = perp b a * ----------------
                      ||proj b a - a||
 
             _____________________     proj b a - a
         = \/ ||a||2 + (proj b a)2 * ----------------
                                     ||proj b a - a||

Cross Product

The final operation that will be discussed for vectors with called the cross product; it is like another way of multiplying vectors together. Remember that the dot product was also like multiplication for two vectors except it returned a scalar value. This time, a cross product returns a vector. Usually the cross product is reserved for three dimensional vectors. Not much is going to be said about 3D vectors because they are the exact same as 2D. The addition, subtraction, scalar multiplication, and dot product rules still hold. The only difference is that there are now three components <x, y, z> instead of two.

The cross product is defined in a rather long equation:

v = <vx, vy, vz>
w = <wx, wy, wz>
 
v ´ w = < vywz - vzwy, vzwx - vxwz, vxwy - vywx >

One of the most important properties of the cross product is that the vector it outputs is orthogonal to both v and w. You can verify this by taking the dot products of the cross product with each of the vectors v and w and seeing that they are zero.

Another geometric interpration is that the cross product relates the sine of the angle between two vectors with the following equation:

||v ´ w|| = ||v|| * ||w|| * sin q
 
          ||v ´ w||
sin q = --------------
        ||v|| * ||w||
Now that we have a way of calculating the sine of the angle between two vectors we can find the perpendicular scalar (and vector) projection much easier. We can derive a new, and easier, equation for calculating the perpendicular scalar projection by looking at the picture from above again:
// We can start by defining the sine of the angle
  between the two vectors in two different ways ||a ´ b|| sin q = --------------- ||a|| * ||b|| perp b a sin q = ---------- ||a|| // Set the equations equal to each other and solve for perp b a

||a ´ b|| perp b a --------------- = ---------- ||a|| * ||b|| ||a||

// Solve for perp b a and simplify
||a ´ b||
perp b a = --------- ||b||

You can now use this new calculation of the perpendicular projection to scale the unit perpendicular vector and you wil get your perpendicular vector projection.

That is the end of this tutorial. You may be wondering where you would ever use this nonsense, but that is to come in the future. An upcoming tutorial on Ultrashock will be on making a camera viewing system for 3D rather than the usual coordinate rotations seen. To do this we will use nearly everything we have done so far, plus introduce a few new concepts from Linear Algebra (matrices and basis).

 

 

 
©2001 Ultrashock.com Inc. - All rights reserved