使用 sohcahtoa(三角函数)查找边的未知长度

Finding an unknown length of a side using sohcahtoa (Trigonometry)

我正在尝试使用 sohcahtoa(我们在学校教过)创建一个函数来计算直角三角形中未知边的长度。

我是这样开始的。

def trigonometry(angle, side_length, side_respective_to_angle, unknown):
    sohcahtoa = [
        ['opposite', 'hypotenuse', 'adjacent']
        [[], math.sin(angle), math.tan(angle)]
        [math.sin(angle), [], math.cos(angle)]
        [math.tan(angle), math.cos(angle), []]
    ]

所以现在我可以使用以下方法将我的边和未知边匹配为正弦、余弦或正切:

index1 = sohcahtoa[0].index(side_respective_to_angle)
index2 = sohcahtoa.index(unknown)
function = sohcahtoa[index1+1][index2]

然后我试图弄清楚程序将如何使用 sohcahtoa 的顺序重新排列方程式。例如:

[如果 side_respective_to_angle 是斜边,未知是相反的]

函数(角度)=未知/side_respective_to_angle

函数(角度)* side_respective_to_angle = 未知

这就是我卡住的地方。有人可以帮助我完成上述任务吗?

我想这就是你想要的:

def trigonometry(angle, side_length, side_respective_to_angle, unknown):
    sohcahtoa = [
        ['opposite', 'hypotenuse', 'adjacent'],
        [None, math.sin, math.tan],
        [math.sin, None, math.cos],
        [math.tan, math.cos, None]
    ]
    index1 = sohcahtoa[0].index(side_respective_to_angle)
    index2 = sohcahtoa[0].index(unknown)
    function = sohcahtoa[index1 + 1][index2]
    return (side_length / function(math.radians(angle))
        if side_respective_to_angle == 'opposite' or (side_respective_to_angle == 'adjacent' and unknown == 'hypotenuse')
        else function(math.radians(angle) * side_length))

trigonometry(30, 2, 'hypotenuse', 'opposite') 的结果是 1(或多或少)。

您必须为 index1index2 获取第 [0] 个元素的 index。此外,您必须首先将 angle 转换为 radians,因为这是 math 函数所期望的。另外请注意,我只将函数放入列表中,而不是函数的结果。无需事先全部计算。

但是除了嵌套列表,您还可以使用嵌套字典,使代码更容易理解,并且不需要那些索引偏移量。您也可以将运算符 */) 放入字典中。

from operator import mul, truediv
import math
def trigonometry(angle, side_length, side_respective_to_angle, unknown):
    o, h, a = 'opposite', 'hypotenuse', 'adjacent'
    sohcahtoa = {o: {h: (math.sin, truediv), a: (math.tan, truediv)},
                 h: {o: (math.sin, mul),     a: (math.cos, mul)},
                 a: {o: (math.tan, mul),     h: (math.cos, truediv)}}
    function, op = sohcahtoa[side_respective_to_angle][unknown]
    return op(side_length, function(math.radians(angle)))

我知道这不是你想要的,但我有点粗暴地为你强加了每一种可能性。

import math

# trig(angle=30, side=2, known='hyp', unknown='opp')
# This would want to find the length of a leg of a triangle
# that has a hypotenuse of 2, and an angle between the
# hypotenuse and another leg of 30 (degrees)

# sin(T) = opp / hyp
# cos(T) = adj / hyp
# tan(T) = opp / adj

def trig(angle, side, known, unknown):
    angle = math.radians(angle)
    # Unknown poss:
        # hyp
        # opp
        # adj
    if known == 'hyp':
        if unknown == 'opp':
            return known * math.sin(angle)
        elif unknown == 'adj':
            return known * math.cos(angle)

    elif known == 'opp':
        if unknown == 'hyp':
            return known / math.sin(angle)
        elif unknown == 'adj':
            return known / math.tan(angle)

    elif known == 'adj':
        if unknown == 'hyp':
            return known / math.cos(angle)
        elif unknown == 'opp':
            return known * math.tan(angle)