向左为负,向右为正,标准坐标系,返回-180-180度
import math
AB = [0 ,0 ,5 ,-1]
CD = [0 ,0 ,4.5 ,4.5]
EF = [0 ,0 ,-2 ,6]
PQ = [0 ,0 ,-6 ,-1]
def angle(v1, v2):
dx1 = v1[2] - v1[0]
dy1 = v1[3] - v1[1]
dx2 = v2[2] - v2[0]
dy2 = v2[3] - v2[1]
angle1 = math.atan2(dy1, dx1)
angle1 = -int(angle1 * 180 /math.pi)
if angle1<0:
angle1=360+angle1
# print(angle1)
angle2 = math.atan2(dy2, dx2)
angle2 =- int(angle2 * 180 /math.pi)
if angle2 < 0:
angle2 = 360 + angle2
print(angle1,angle2)
included_angle = angle1 - angle2
if abs(included_angle) > 180:
included_angle=included_angle/abs(included_angle)*(360-abs(included_angle))
else:
included_angle*=-1
return included_angle
ang1 = angle(AB, CD)
print("AB和CD的夹角")
print(ang1)
ang1 = angle(CD,AB)
print("CD和AB的夹角"
|