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

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

I'm trying to model a function off of the example here, but I'm getting a different result using the same parameters.

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

Thanks to @Stef for pointing me in the right direction. Here is the working example in case it is helpful to someone in the future.

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