在给定两个角度的情况下,求三角形两边的长度,以及它们之间的边的长度

  • 本文关键字:三角形 之间 情况下 两个 python math
  • 更新时间 :
  • 英文 :


我试图根据这里的示例对函数进行建模,但使用相同的参数会得到不同的结果。

def getTwoSidesOfASATriangle(a1, a2, s):
'''
Get the length of two sides of a triangle, given two angles, and the length of the side in-between.
args:
a1 (float) = Angle in degrees.
a2 (float) = Angle in degrees.
s (float) = The distance of the side between the two given angles.
returns:
(tuple)
'''
from math import sin
a3 = 180 - a1 - a2
result = (
(s/sin(a3)) * sin(a1),
(s/sin(a3)) * sin(a2)
)
return result
d1, d2 = getTwoSidesOfASATriangle(76, 34, 9)
print (d1, d2)

感谢@Stef为我指明了正确的方向。以下是一个工作示例,以备将来对某人有所帮助。

def getTwoSidesOfASATriangle(a1, a2, s, unit='degrees'):
'''
Get the length of two sides of a triangle, given two angles, and the length of the side in-between.
args:
a1 (float) = Angle in radians or degrees. (unit flag must be set if value given in radians)
a2 (float) = Angle in radians or degrees. (unit flag must be set if value given in radians)
s (float) = The distance of the side between the two given angles.
unit (str) = Specify whether the angle values are given in degrees or radians. (valid: 'radians', 'degrees')(default: degrees)
returns:
(tuple)
'''
from math import sin, radians
if unit is 'degrees':
a1, a2 = radians(a1), radians(a2)
a3 = 3.14159 - a1 - a2
result = (
(s/sin(a3)) * sin(a1),
(s/sin(a3)) * sin(a2)
)
return result

最新更新