**Theory**

A unit vector is vector that has a length of one. Unit vectors are used to indicate directions. Any vector can be expressed as a product of a magnitude and a direction. The magnitude of a vector is its length. The following formula can be used to calculate the magnitude of a vector :

The unit vector of can be calculated using the following formula:

**Application**

The following algorithm calculates the length of vector. This algorithm takes in a Cartesian coordinate and returns a float representing the magnitude of the Cartesian coordinate.

function vectorLength(p1)

{

return Math.sqrt(p1.X* p1.X+ p1.Y* p1.Y+ p1.Z* p1.Z);

}

Listing 1 Vector Length

The following algorithm calculates the unit of vector a Cartesian coordinate. This algorithm takes in a Cartesian coordinate and returns a Cartesian coordinate representing the unit vector.

function unitVector(p1)

{

var length = vectorLength(p1);

return new Cartesian(p1.X/length,p1.Y/length,p1.Z/length);

}

Listing 2 Unit Vector

The following post has additional information on the Cartesian object: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!280.entry

### Like this:

Like Loading...