Warm tip: This article is reproduced from serverfault.com, please click

python-在给定两个角度的情况下,获取三角形的两个边的长度,以及两个角之间的边的长度

(python - Get the length of two sides of a triangle, given two angles, and the length of the side between them)

发布于 2020-11-25 16:29:27

我正在尝试根据此处示例对函数建模,但是使用相同的参数却得到了不同的结果。

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)
Questioner
m3trik
Viewed
0
m3trik 2020-11-29 02:25:29

感谢@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