返回值范围0-360
import numpy as np def angle(x1, y1, x2, y2): if x1 == x2: return 90 if y1 == y2: return 180 k = -(y2 - y1) / (x2 - x1) # 求反正切,再将得到的弧度转换为度 result = np.arctan(k) * 57.29577 # 234象限 if x1 > x2 and y1 > y2: result += 180 elif x1 > x2 and y1 < y2: result += 180 elif x1 < x2 and y1 < y2: result += 360 print("直线倾斜角度为:" + str(result) + "度") return result if __name__ == '__main__': x1, y1 = (306, 319) x2, y2 = (205, 331) angle(x1, y1, x2, y2)