Python 给定两个角,求三角形两边的长度,以及它们之间边的长度

Python 给定两个角,求三角形两边的长度,以及它们之间边的长度,python,math,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) =

我试着用相同的参数来模拟一个函数,但得到的结果不同

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

sin
cos
在Python中使用弧度,而不是度。行
a3=180-a1-a2
向我表明,您认为它们使用度。首先必须将角度转换为弧度。尝试执行
a3=数学弧度(180-a1-a2)
。在开始使用函数之前,最好先阅读函数文档: