
IntroductionThe dot product is a value expressing the angular relationship between two vectors. In this article we will learn how this value is calculated, its mathematical significance, and several ways in which this function is useful in 3D applications. Calculating the Dot ProductA dot product is a scalar value that is the result of an operation of two vectors with the same number of components. Given two vectors A and B each with n components, the dot product is calculated as: A · B = A_{1}B_{1} + ... + A_{n}B_{n} The dot product is thus the sum of the products of each component of the two vectors. For example if A and B were 3D vectors: A · B = A.x * B.x + A.y * B.y + A.z * B.z A generic C++ function to implement a dot product on two floating point vectors of any dimensions might look something like this:
This sample code is provided solely for the purpose of showing a generic function to clarify how the dot product is calculated; DirectX provides several implementations of this function for you, as you will see further on, though if you did need to write your own function (for example if using C++ without the D3DX libraries) you would likely just write separate functions to handle the vector types commonly used (2D,3D,4D) as inline code. So what does it mean?Earlier we said that the dot product represents an angular relationship between two vectors, and left it at that. Now we'll take a closer look at what this value represents. Let's say we have two vectors, A and B, as shown to the left. The values A and B represent the lengths of vectors A and B, respectively, and Θ is the angle between the two vectors. The dot product of vectors A and B will have the following relationship to these values: A · B = A * B * cos(Θ) That is to say, the dot product of two vectors will be equal to the cosine of the angle between the vectors, times the lengths of each of the vectors.
Given the characteristics of the cosine function, we can deduce three possible conditions:
The above characteristics are true for any vectors with nonzero length. In addition, there is a special case when both vectors are unit vectors, that is, vectors with a length of one (1.0). In this case, the lengths of the vectors does not contribute to the equation, simplifying to: A · B = A * B * cos(Θ) In this case, the dot product is equal to the cosine of the angle between the vectors. Thus the angle between unit vectors can be calculated as: Θ = acos(A · B)
Angles between nonunit vectors (vectors with lengths not equal to 1.0) can be calculated either by first normalizing the vectors, or by dividing the dot product of the nonunit vectors by the length of each vector.
Taking the dot product of a vector against itself (i.e. A · A) results in a value equal to the square of the vector's length. This is a familiar portion of the distance equation, d=sqrt(x*x+y*y+z*z).
If one were to take the
dot product of a unit vector A and a second vector B of any nonzero length, the
result is the length of vector B projected in the direction of vector A (see
illustration to left). This is used in a number of ways, such as collision
response and conversion of vectors from one coordinate system to another (this
forms the basis of matrix transformations). DirectX ImplementationsDirectX Graphics provides several implementations of the dot product function:

Visitors Since 1/1/2000:
