Special

### Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! This bundle includes six back issues of the magazine -- all of year 20 in printed book and digital formats -- plus a one-year subscription (beginning with 21.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as \$35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Recent issues                     Article Preview

Issue 4.2

COLUMN

## Vector math

### A lesson in understanding vectors

Issue: 4.2 (November/December 2005)
Author: Thomas Reed
Author Bio: Thomas Reed has been programming as a hobbyist for more than 20 years, and fell in love with the Mac in 1984.
Article Description: No description available.
Article Length (in bytes): 8,740
Starting Page Number: 34
Article Number: 4216

Excerpt of article text...

Many software programs, especially graphics-intensive ones, make use of a mathematical concept called the "vector." People who are not math-oriented may find the vector intimidating. In truth, they are not particularly complex at all. It is the terminology, including concepts such as cross-products and normalized vectors, that is frightening. In this article, I hope to eliminate, or at least reduce, this fear of the vector.

For those who are not at all familiar with the vector, it is a mathematical idea that represents both a direction and a length, or magnitude. For example, a moving car's velocity has both a direction (e.g.,, north) and a magnitude (e.g.,60 mph). This velocity can easily be described using a vector. Conceptually, a vector can be imagined as an arrow with a length determined by its magnitude, as seen in Figure 1.

A vector is typically represented by computer programs using three quantities: the x, y, and z components of its magnitude, each of which is just a number (also known as a scalar). While this representation makes certain things, like finding the length of the vector, more complex, it simplifies other things. In particular, the vector can be thought of as three separate vectors, each pointing along an axis.

Consider the illustration in Figure 2. The vector v can be broken down into component vectors along each axis: vx, vy and vz. This makes it very easy to consider only one particular axis of the vector at a time. For example, in a physics simulation involving gravity, you have a force that changes the velocity vector only along one axis. This is easily done, since the component vector along that axis is readily available.

This representation of the vector also makes vector addition and subtraction easier. The result of adding or subtracting two vectors is a new vector with a different magnitude and direction. Figure 3 shows how to visualize this addition and subtraction. Fortunately, representing a vector as x, y, and z components makes this addition and subtraction easy -- simply add or subtract each component of the two vectors separately. In other words, for the example addition a + b = c from Figure 3, the x component of the resulting vector c, or cx would simply be ax + bx.

Unfortunately, something as simple as determining the length of a vector can become more of a challenge with this representation. We must turn to geometry -- specifically, the Pythagorean theorem, which states that the relationship between sides in a right triangle is a2 + b2 = c2. Take a look at Figure 4, which shows how a vector can be described in terms of two right triangles. Notice that three sides of these triangles, labeled a, b, and c in the figure, are simply the component vectors vx, vy and vz. Thus, with clever application of the Pythagorean theorem, we find that the length of a vector is described by the equation shown in Figure 4: sqrt( vx2 + vy2 + vz2).