## Working with Vectors

The power of vectors stems from the fact that we can easily manipulate and combine them. Before we can do that, though, we need to define how we represent vectors:

Now, that wasn't a big surprise; we've done that a gazillion times already. Every vector has an x and a y component in our 2D space (yes, we'll be staying in two dimensions in this chapter). We can also add two vectors:

c = a + b = (a.x, a.y) + (b.x, b.y) = (a.x + b.x, a.y + b.y)

All we need to do is add the components together to arrive at the final vector. Try it out with the vectors given in Figure 8-1. Say we take Bob's position, p = (3,2), and add his velocity, v = (-2,3). We arrive at a new position, p' = (3 + -2, 2 + 3) = (1,5). Don't get confused by the apostrophe behind the p here, it's just there to denote that we have a new vector p. Of course, this little operation only makes sense when the units of the position and velocity fit together. In this case we assume the position is given in meters (m) and the velocity is given in meters per second (m/s), which fits perfectly fine.

Of course, we can also subtract vectors: c = a - b = (a.x, a.y) - (b.x, b.y) = (a.x - b.x, a.y - b.y)

Again, all we do is combine the components of the two vectors. Note, however, that the order in which we subtract one vector from the other is important. Take the rightmost image in Figure 8-1, for example. We have a green Bob at pg = (1,4) and a red Bob at pr = (6,1), where pg and pr stand for position green and red respectively.. When we take the distance vector from green Bob to red Bob, we calculate the following:

Now that is strange. That vector is actually pointing from red Bob to green Bob! To get the direction vector from green Bob to red Bob, we have to reverse the order of subtraction:

If we want to find the distance vector from a position a to a position b, we use the following general formula:

In other words, we always subtract the start position from the end position. That's a little confusing at first, but if you think about it, it makes absolute sense. Try it out on some graph paper!

We can also multiply a vector by a scalar (remember, a scalar is just a single value): a' = a * scalar = (a.x * scalar, a.y * scalar)

We multiply each of the components of the vector by the scalar. This allows us to scale the length of a vector. Take the direction vector in Figure 8-1 as an example. It's specified as d = (0,-1). If we multiply it with the scalar s = 2, we effectively double its length: d x s = (0,-1 x 2) = (0,-2). We can of course make it smaller as well, by using a scalar less than 1—for example, d multiplied by s = 0.5 creates a new vector d' = (0,0.5).

Speaking of length, we can also calculate the length of a vector (in the units it's given in): |a| = sqrt(a.x*a.x + a.y*a.y)

The |a| notation just tells us that this represents the length of the vector. If you didn't sleep through your linear algebra class at school, you might recognize the formula for the vector length. It's the Pythagorean theorem applied to our fancy 2D vector. The x and y components of the vector form two sides of a right triangle, and the third side is the length of the vector. Figure 8-2 illustrates this.

Figure 8-2. Pythagoras would love vectors too

The vector length is always positive or zero, given the properties of the square root. If we apply this to the distance vector between the red and green Bob, we can figure out that they are

|pr - pg| = sqrt(5*5 + -3*-3) = sqrt(25 + 9) = sqrt(34) ~= 5.83m

|pr - pg| = sqrt(5*5 + -3*-3) = sqrt(25 + 9) = sqrt(34) ~= 5.83m apart from each other (if their positions are given in meters). Note that if we calculated \pg - pr|, we'd arrive at the same value, as the length is independent of the direction of the vector. This new knowledge also has another implication: when we multiply a vector with a scalar, its length changes accordingly. Given a vector d = (0,-1) with an original length of 1 unit, we can multiply it by 2.5 and arrive at a new vector with a length of 2.5 units.

We discussed that direction vectors usually don't have any units associated with them. We can make them have a unit by multiplying them with a scalar—for example, we can multiply a direction vector d = (0,1) with a speed constant s = 100 m/s to get a velocity vector v = (0 x 100,1 x 100) = (0,100). It's therefore always a good idea to let our direction vectors have a length of 1. Vectors with a length of 1 are called unit vectors. We can make any vector a unit vector by dividing each of its components by its length:

Remember that \d\ just means the length of the vector d. Let's try it out. Say we want a direction vector that points exactly northeast: d = (1,1). It might seem that this vector is already unit length, as both components are 1, right? Wrong:

We can easily fix that by making the vector a unit vector:

d' = (d.x/|d|, d.y/|d|) = (1/|d|, 1/|d|) ~= (1/1.44, 1/1.44) = (0.69, 0.69)

This is also called normalizing a vector, which just means that we make it have a length of 1. With this little trick we can create a unit-length direction vector out of a distance vector, for example. Of course, we have to watch out for zero-length vectors, as we'd have division by zero in that case!

0 0