Lesson 1 of 18

3D Dot Product

The Dot Product in 3D

The dot product of two vectors a=(ax,ay,az)\mathbf{a} = (a_x, a_y, a_z) and b=(bx,by,bz)\mathbf{b} = (b_x, b_y, b_z) is:

ab=axbx+ayby+azbz\mathbf{a} \cdot \mathbf{b} = a_x b_x + a_y b_y + a_z b_z

The result is a scalar — a single number, not a vector.

Geometric Interpretation

ab=abcos(θ)\mathbf{a} \cdot \mathbf{b} = |\mathbf{a}| \cdot |\mathbf{b}| \cdot \cos(\theta)

Where θ\theta is the angle between the vectors. This means:

  • If ab>0\mathbf{a} \cdot \mathbf{b} > 0: vectors point in a similar direction (θ<90°\theta < 90°)
  • If ab=0\mathbf{a} \cdot \mathbf{b} = 0: vectors are perpendicular (θ=90°\theta = 90°)
  • If ab<0\mathbf{a} \cdot \mathbf{b} < 0: vectors point in opposite directions (θ>90°\theta > 90°)

Applications

  • Checking orthogonality: two vectors are perpendicular if and only if their dot product is zero
  • Work: W=FdW = \mathbf{F} \cdot \mathbf{d} (force dot displacement)
  • Projections: computing how much one vector points along another

Examples

a\mathbf{a}b\mathbf{b}ab\mathbf{a} \cdot \mathbf{b}
(1,0,0)(0,1,0)0 (perpendicular)
(1,2,3)(4,5,6)4+10+18 = 32
(2,2,1)(2,2,1)$4+4+1 = 9 =

Your Task

Implement double dot3(double ax, double ay, double az, double bx, double by, double bz) that computes the 3D dot product.

TCC compiler loading...
Loading...
Click "Run" to execute your code.