Tag Archives: dot product

The dot product

Formula:  dx1 * dx2 + dy1 * dy2 + dz1 * dz1

dx1 is the delta between the two x coordinates (last – first) of vector V1.
dx2 is the delta between the two x coordinates (last – first) of vector V2.
dy1 is the delta between the two y coordinates (last – first) of vector V1.
dy2 is the delta between the two y coordinates (last – first) of vector V2.
dz1 is the delta between the two z coordinates (last – first) of vector V1.
dz2 is the delta between the two z coordinates (last – first) of vector V2.

Check of 90 angles

The dot product is actually a representation of the cosine. When the internal vector of two vectors angle is 90 degrees the dot product is always zero.

Project point on vector

Project P on vector V1

Projection can be done using the dot product.

You can chose to normalize the vector, this is how I learned it.

Normalizing a vector requires you to make its length 1 but keeping its direction.

length = sqrt(  ((vx2 - vx1) * (vx2 - vx1)) + ((vy2 - vy1) * (vy2 - vy1))  +( (vz2 - vz1) * (vz2 - vz1)) );

vx2 = vx1 + (vx2 - vx1) / length;

vy2 = vy1 + (vy2 - vy1) / length;

vz2 = vz1 + (vz2 - vz1) / length;

Note: It is often more efficient to calculate the delta’s up front.

The dot vector can be used to project a point on a vector (line).

The result will be 3 * 1 + 2 * 0  + 0 * 0 = 3

Now vector |V1| multiplied with 3 will point to the projection point.

Alternative

Alternatively do not normalize the vector. In that case the result will be 3 * 4 + 2 * 0  + 0 * 0 = 12.

When you look at the original formula you can divide it by the length of V1 (4) to get the offset relatively to the |V1| vector.

To get the offset to the original vector divide by the length again. Which will give offset 1.0. Useful thing is you will save the square.

For a 2d vector and a point the formula becomes:

(vx2 – vx1) * (px -vx1) + (vy2 – vy1) * (py – vy1)

——————————————————-

((vx2 – vx1) *  (vx2 – vx1)) +  ( (vy2 – vy1) * (vy2 – vy1))

Lastest update in May 2011, inital post in May 2011