dot = vector runitNone1 # dot product r = npsqrt npsum vector2 axis= -1 # radius t = dotr # theta
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
原文地址: https://www.cveoy.top/t/topic/hotV 著作权归作者所有。请勿转载和采集!