TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Angle Between 3D Vectors

3D Geometry
Medium

Compute the angle in radians between two 3D vectors. First obtain their cosine:

c=v⋅w∥v∥2∥w∥2c=\frac{\mathbf{v}\cdot\mathbf{w}}{\lVert\mathbf{v}\rVert_2\lVert\mathbf{w}\rVert_2}c=∥v∥2​∥w∥2​v⋅w​

Then recover the angle:

θ=arccos⁡(c)\theta=\arccos(c)θ=arccos(c)

Clamp ccc to [−1,1][-1,1][−1,1] before applying arccos to protect against floating-point error. If either vector has zero norm, the angle is undefined, so return np.nan. Otherwise return a Python float in [0,π][0,\pi][0,π].

Loading visualization...

Examples

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

Output: 1.570796

Explanation: Orthogonal vectors have cosine zero and an angle of pi divided by two.

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

Output: 0

Hint 1

Use np.dot(v, w) for the numerator and squared sums for both norms.

Hint 2

Pass np.clip(cosine, -1.0, 1.0) to np.arccos.

Requirements

  • Compute both Euclidean norms and the dot product
  • Return np.nan when either norm is zero
  • Clamp the cosine before applying np.arccos
  • Return a Python float

Constraints

  • Both inputs contain exactly three numeric values
  • Use NumPy only
Try Similar Problems
Normalize 3dVector Norm 3dDot ProductRotate Around ZHomogeneous Transform

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Angle Between 3D Vectors

3D Geometry
Medium

Compute the angle in radians between two 3D vectors. First obtain their cosine:

c=v⋅w∥v∥2∥w∥2c=\frac{\mathbf{v}\cdot\mathbf{w}}{\lVert\mathbf{v}\rVert_2\lVert\mathbf{w}\rVert_2}c=∥v∥2​∥w∥2​v⋅w​

Then recover the angle:

θ=arccos⁡(c)\theta=\arccos(c)θ=arccos(c)

Clamp ccc to [−1,1][-1,1][−1,1] before applying arccos to protect against floating-point error. If either vector has zero norm, the angle is undefined, so return np.nan. Otherwise return a Python float in [0,π][0,\pi][0,π].

Loading visualization...

Examples

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

Output: 1.570796

Explanation: Orthogonal vectors have cosine zero and an angle of pi divided by two.

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

Output: 0

Hint 1

Use np.dot(v, w) for the numerator and squared sums for both norms.

Hint 2

Pass np.clip(cosine, -1.0, 1.0) to np.arccos.

Requirements

  • Compute both Euclidean norms and the dot product
  • Return np.nan when either norm is zero
  • Clamp the cosine before applying np.arccos
  • Return a Python float

Constraints

  • Both inputs contain exactly three numeric values
  • Use NumPy only
Try Similar Problems
Normalize 3dVector Norm 3dDot ProductRotate Around ZHomogeneous Transform

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.