余弦定理

0x01 定义

余弦定理(law of cosines):
是描述三角形中三边长度与一个角的余弦值关系的数学定理

0x02 公式

已知△ABC中,AB=c,BC=a,AC=b,则有:

a2=b2+c22bccosAa^2 = b^2 + c^2 - 2bc\cos{A}
b2=a2+c22accosBb^2 = a^2 + c^2 - 2ac\cos{B}
c2=a2+b22abcosCc^2 = a^2 + b^2 - 2ab\cos{C}

cosA=b2+c2a22bc\cos{A} = \frac{b^2 + c^2 - a^2}{2bc}
cosB=a2+c2b22ac\cos{B} = \frac{a^2 + c^2 - b^2}{2ac}
cosC=a2+b2c22ab\cos{C} = \frac{a^2 + b^2 - c^2}{2ab}

0x03 推导过程

在任意△ABC中
做AD⊥BC.
∠C所对的边为c,
∠B所对的边为b,
∠A所对的边为a

BD=acosBBD = a \cdot \cos{B}
CD=asinBCD = a \cdot \sin{B}
AD=ABBD=cacosBAD = AB - BD = c - a \cdot \cos{B}

根据勾股定理:

AD2+CD2=AC2AD^2+CD^2=AC^2
(cacosB)2+(asinB)2=b2(c - a \cdot \cos{B})^2 + (a \cdot \sin{B})^2 = b^2
c22accosB+a2cos2B+a2sin2B=b2c^2 -2ac\cos{B} + a^2\cos{^2B} + a^2\sin{^2B} = b^2
c22accosB+a2(cos2B+sin2B)=b2c^2 -2ac\cos{B} + a^2(\cos{^2B} + \sin{^2B}) = b^2
c22accosB+a2=b2c^2 -2ac\cos{B} + a^2 = b^2
2accosB=a2+c2b22ac\cos{B} = a^2 + c^2- b^2
cosB=a2+c2b22accos{B} = \frac{a^2 + c^2- b^2}{2ac}

0x04 程序实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import math

a = 3.9
b = 3.3
c = 4.3

def angle(a, b, c):
cosA = (b**2 + c**2 - a**2)/(2*b*c)
return math.degrees(math.acos(cosA))

angA = angle(a, b, c)
angB = angle(b, c, a)
angC = angle(c, a, b)

print(f'angle A = {angA}')
print(f'angle B = {angB}')
print(f'angle C = {angC}')
print(f'angle A + angle B + angle c = {angA + angB + angC}')

输出结果:

angle A = 60.04661306603643
angle B = 47.149833002137065
angle C = 72.80355393182653
angle A + angle B + angle c = 180.0