## Normalization and Unit Vectors

A lot of graphics-related math is easier to do when you deal with percentages. If you think of an iPhone screen as being one unit long by one unit wide with the center of the screen being (0.5, 0.5), you don’t have to think about what the screen’s pixel resolution is or what iteration of the iPhone you’re on. This philosophy applies to many other equations in graphics programming, which is why an important mathematical concept you should be aware of is the process of vector normalization.

Much of graphics programming works under the assumption that you are working with percentages and that you will not have a value larger than 1. If you have a right triangle with unit values of 3, 4, and 5, in order to perform algorithms on this triangle, you need to determine proportionally how these values scale. The hypotenuse has a value of 1, while the other two sides are some percentage of that hypotenuse. To find that ratio, you divide each side by the value of the hypotenuse. So instead of the triangle being (3, 4, 5), the triangle is normalized to (3/5, 4/5, 1).

Vector normalization is useful for determining direction. One common use of the dot product, for example, is to calculate the cosine of an angle between a light ray and the surface normal, as shown in Figure 4.4. This calculation is done only after the vectors are normalized.

**Figure 4.4** Normalizing a vector

To normalize a vector, you perform the Pythagorean theorem on the components of the vector. That is, you square each component, add them together, and take the square root of that number. In our (3, 4, 0) vector, 3^{2} = 9, 4^{2} = 16, and 0^{2} = 0, so 9 + 16 + 0 = 25. The square root of 25 is 5, which is the length of the vector. To get the normalized coordinates, you divide each component by the length. This tells you what percentage of the overall vector each component makes up.

Normalizing a vector doesn’t change the shape or the angle of the vector. It simply scales the vector up or down. Picture a 45-45-90 degree right triangle. There are certain rules about that triangle that hold true no matter what. Two of the three sides will always be the same length regardless of whether that length is 10 units or 42 units. Making the hypotenuse one unit and every other side a percentage of that unit makes the hypotenuse easier to do mathematical processes on. A lot of units associated with your coordinate system are arbitrary. The important thing is to make sure that the proportions of the sides and the angles of the triangle are consistent.