Problems
Loading...
1 / 1

Angle Between 3D Vectors

3D Geometry
Medium

Compute the angle (in radians) between two 3D vectors v and w. Return θ[0,π]θ∈[0,π] Handle zero vectors gracefully.

Angle Formula:

cosθ=vwvwθ=arccos(cosθ)\cos\theta = \frac{v \cdot w}{\lVert v \rVert \, \lVert w \rVert} \\ \theta = \arccos(\cos\theta)
Loading visualization...

Examples

Input: v = [1, 0, 0], w = [0, 1, 0]

Output: ≈ 1.571 (π/2)

Input: v = [1, 2, 3], w = [2, 4, 6]

Output: ≈ 0.0

Hint 1

Check if either norm <10−10, if so return np.nan.

Hint 2

Use np.clip() before np.arccos() to avoid numerical errors.

Requirements

  • Inputs: v,w as lists or arrays of shape (3,)
  • Return scalar float: angle in radians
  • Use dot product and norms
  • Clamp cosine value to [−1,1] before arccos
  • If either vector has zero norm: return np.nan

Constraints

  • Absolute error tolerance: 10−6
  • NumPy only; time limit: 200ms
Try Similar Problems