python 根据余弦定理计算两边的夹角

前面写过C#的。

import numpy

def GetAngle(sta_point, mid_point, end_point):
    ma_x = sta_point.X-mid_point.X
    ma_y = sta_point.Y-mid_point.Y
    mb_x = end_point.X-mid_point.X
    mb_y = end_point.Y-mid_point.Y
    ab_x = sta_point.X-end_point.X
    ab_y = sta_point.Y-end_point.Y
    ab_val2 = ab_x * ab_x + ab_y * ab_y
    ma_val2 = ma_x * ma_x + ma_y * ma_y
    mb_val2 = mb_x * mb_x + mb_y * mb_y
    cos_M = (ma_val2+mb_val2-ab_val2) / (2 * numpy.sqrt(ma_val2)*numpy.sqrt(mb_val2))
    angleAMB = numpy.arccos(cos_M)/numpy.pi * 180
    return angleAMB

 

上一篇:P1351 联合权值


下一篇:速算24点