#### Magnitude and Scaling

It is very common to want to scale a vector. Above we scaled the Diff vector to 25% of its original length. This simple scaling is done by multiplying each element of the vector by the scale factor. Another common requirement is to create a copy of a vector that has unit length. This is called a normalized vector. We can do this by dividing each element of the vector by the vector’s current length or magnitude. Fortunately the system has two API functions that can help us here mathVecMagnitude() and mathVecNormalize() each of which takes a vector and the number of elements in the vector as parameters. Lastly, you may want to create a vector of a specific length using the same direction as some given vector. This is similar to scaling the vector, except that we would want to normalize the vector first and then scale it to the final desired size.

Now create a function for scaling vectors called mathVecScale(). This is a function you will use again and again and again so let’s store it on a new Utilities page. This page will be like a toolbox that can easily be copied into new programs. You should create all of your generalized “tools” here.

To create the new page, click on New Page in the outline view at the left side of the IDE. In the Page Name field enter Utilities and click on the Create button. You should now create a new function matVecScale(). Insert the following code on the Utilities page.

This function has four parameters, the resulting vector, the original vector, the scaling size and a flag to indicate whether we should normalize the vector before scaling it. You can use this function to handle all three of these scaling tasks. Note that mathVecNormalize() normalizes a vector “in-place” so if you want a normalized copy of a vector without changing the original, you can use this function with a mag of 1.0 and true for the norm parameter. Review the code to make sure you understand it before proceeding and you have your first souvenir tool.