Vector Fun: projection of a vector on another

I just realized that I used a technique – projecting a vector on a line given by another vector – in my last post on ray-tracing that I did not justify in any way.

Look at this picture:

Projection

Given vector a and b we are looking for vector b'. We saw in the dot-product article, that we have

 a \cdot b = \left| a \right| \left| b \right| cos \alpha

and of course

 cos \alpha = \frac { \left| b' \right| } { \left| b \right| }

combining these we get

 a \cdot b = \left| a \right| \left| b' \right|

and therefore as

 b' = \frac {\left| b' \right| }{ \left| a \right| } a

finally

 b' = \frac { a \cdot b}{ {\left| a \right|}^{2}} a

which reduces to

 b' = (b \cdot n) n

in the case that  n = a is a unit-vector. This is the result we lacked.