1. 程式人生 > 其它 >向量與X軸的夾角

向量與X軸的夾角

class vectorAngle():
def __init__(self, vec):
self.x, self.y = vec[0], vec[1]

def vecAttr(self):
# 判斷點所在的象限
# N
# W O E
# S
ref = 0.
x, y = self.x, self.y
if x == ref:
if y == ref:
res = 'OO'
elif y > ref:
res = 'ON'
else:
res = 'OS'
elif x > ref:
if y == ref:
res = 'OE'
elif y > ref:
res = 'EN'
else:
res = "ES"
else:
if y == ref:
res = "OW"
elif y > ref:
res = "WN"
else:
res = "WS"
return res

def vecAngle(self):
flag = self.vecAttr()
x, y = self.x, self.y
if flag == "OO":
angle = 0.
elif flag == "OE":
angle = 0.
elif flag == "EN":
angle = np.arctan(y/x)
elif flag == "ON":
angle = np.pi / 2
elif flag == "WN":
angle = np.pi - np.arctan(abs(y/x))
elif flag == "OW":
angle = np.pi
elif flag == "WS":
angle = np.pi + np.arctan(abs(y/x))
elif flag == "OS":
angle = np.pi * 1.5
else:
angle = np.pi * 2.0 - np.arctan(abs(y/x))

return round(angle, 3)