-1

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)
m3trik
  • 333
  • 2
  • 13
  • 1
    `sin` and `cos` use radians in Python, not degrees. The line `a3 = 180 - a1 - a2` indicates to me that you think they use degrees. You have to convert the angle to radians first. Try doing `a3 = math.radians(180 - a1 - a2)`. It's good to read the documentation for functions before you start using them: https://docs.python.org/3/library/math.html – Random Davis Nov 25 '20 at 16:32

1 Answers1

0

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
m3trik
  • 333
  • 2
  • 13