正如其他人所说,atan2 需要两个参数才能正确确定输出角度的象限...
但是,它仍然输出[-pi,pi] 之间的角度,这并不总是有用的(第一和第二象限为正[0,pi];第三和第四象限为负[-pi,0])。
可以定义一个 atan 函数,该函数返回 [0,2pi] 中的角度,如 theodore panagos 所示。
改进 theodore panagos 的回答,这里是一个使用 numpy 的 python 版本
import numpy
# defining the atan function
myatan = lambda x,y: numpy.pi*(1.0-0.5*(1+numpy.sign(x))*(1-numpy.sign(y**2))\
-0.25*(2+numpy.sign(x))*numpy.sign(y))\
-numpy.sign(x*y)*numpy.arctan((numpy.abs(x)-numpy.abs(y))/(numpy.abs(x)+numpy.abs(y)))
#testing
u = numpy.array([[numpy.sqrt(3.0)/2.0,0.5], # expected: 30
[0.5,numpy.sqrt(3.0)/2.0], # expected: 60
[0.0,1.0], # expected: 90
[-0.5,numpy.sqrt(3.0)/2.0], # expected: 120
[-numpy.sqrt(3.0)/2.0,0.5], # expected: 150
[-1.0,0.0], # expected: 180
[-numpy.sqrt(3.0)/2.0,-0.5], # expected: 210
[-0.5,-numpy.sqrt(3.0)/2.0], # expected: 240
[0.0,-1.0], # expected: 270
[0.5,-numpy.sqrt(3.0)/2.0], # expected: 300
[numpy.sqrt(3.0)/2.0,-0.5], # expected: 330
[1.0,0.0]]) # expected: 0 or 360
theta = myatan(u[:,0],u[:,1])
print(theta * 180.0/numpy.pi) # converting to degrees
输出:
[ 30. 60. 90. 120. 150. 180. 210. 240. 270. 300. 330. 0.]
它并没有精确地输出 360,但它会一直到它,然后它会按预期循环