This is an illustration of how to calculate the cosine angle between two vectors formed by the 0, 20, and 0 points on a flat x-z plane surface and the light source situated at 20, 20, and 40, as shown in the following figure:
Calculate ON and OL vectors, as shown in the following code:
OL = L – O = (20, 20, 40) – (0, 0, 0) => (20-0), (20-0), (40-0) => (20, 20, 40) ON = N – O = (0, 20, 0) – (0, 0, 0) => (0, 20, 0)
The dot product between OL and ON is as follows:
OL . ON = |OL| * |ON| * cos(θ)
Using Equation 1:
OL .ON = (20*0) + (20*20) + (40*0) = 400
Using Equation 2:
|OL|*|ON| = [√ (20*20) + (20*20) + (40*40)] * [ √ (0*0) +(20*20)+(0*0)] = 979.79
Equating both equations, the result is shown in the following code:
400 = 979.79 * cos(θ);