This code takes a 3D vector and calculates its angle with respect to the x-axis in the xy-plane.

The output is the angle theta in radians

import numpy as np

def calculate_angle(vector): 'Define unit vector along x-axis' runit = np.array([1, 0, 0]) 'Calculate dot product between vector and unit vector' dot = (vector * runit[None,:])[:,:,1] 'Calculate radius of vector' r = np.sqrt(np.sum(vector**2, axis=-1)) 'Calculate angle theta in radians' t = np.arccos(dot/r) return t

Example usage

vector = np.array([[3, 4, 0], [-1, 1, 0]]) theta = calculate_angle(vector) print(theta) # Output: [0.92729522 2.03444394]

Python Function to Calculate 3D Vector Angle with X-Axis

原文地址: https://www.cveoy.top/t/topic/oRjp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录