# Dot product

The dot product measures how similar two vectors are. It's one of the most commonly used operations in games. Taking the dot product of two unit vectors (vectors with a magnitude of 1) yields a real number in the range of \([-1, 1]\). So long as both vectors are unit length, the dot product results follow these rules:

- The dot product is
**positive**if the vectors point in the**same direction** - The dot product is
**negative**if the vectors point in**opposite directions** - The dot product is
**zero**if the vectors are**perpendicular**

To take the dot product of two vectors, multiply like components and take the sum of the results:

$$ \vec{A} \cdot \vec{B} = \sum^{n}_{i=0} A_{i} B_{i} = (A_0 B_0 + A_1 B_1 + ... A_n B_n) $$

The dot product of between two vectors \(\vec{A}\) and \(\vec{B}\) is equal the length of \(\vec{A}\) times the length of \(\vec{B}\) times the cosine angle between the two vectors. \(\vec{A} \cdot \vec{B} = \|\vec{A}\| \|\vec{B}\| cos(\theta)\). The length and angle of vectors will be covered in the next two sections.

In the above demo, the green and blue vectors always have a magnitude of 1, because of this they will always point somewhere on a circle with a radius of 1. Drag the green and blue arrows around and note the value of the dot product in the upper left printed in orange.

Like most vector operations, implemnting the dot product is trivial

float Dot(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; } float Dot(vec3 a, vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } float Dot(vec4 a, vec4 b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }