【问题标题】:python math.acos inverse cosine issuespython math.acos 反余弦问题
【发布时间】:2014-12-30 20:38:24
【问题描述】:

我需要找到一个平面上只有三个坐标点的三角形的角度。在等式末尾的常规三角函数中,我会使用:

cos = (a ** 2) - (b ** 2) - (c ** 2) / -2 * b * c

我使用了** 运算符,而sideAsideBsideC 是三角形边的长度。

我目前正在使用math.acos() 来查找角度,但我遇到了数学域错误。 math.acos() 是用于我所理解的反余弦的正确函数吗?

这是我的程序的代码摘录:

x = 100
y = 100
centerX = x + 50
centerY = y + 50

if event.type == MOUSEMOTION:
            mousex, mousey = event.pos
            sideA = math.sqrt((x - mousex)**2)+((y - mousey)**2)
            sideB = math.sqrt((centerX - mousex)**2)+((centerY - mousey)**2)
            sideC = math.sqrt((centerX - x)**2)+((centerY - y)**2)
            cos = float(sideA**2)-(sideB**2)-(sideC**2)/(-2*(sideB*sideC))
            angle = math.acos(cos)
            print angle

我做错了什么?当我将程序中的数字输入计算器时,我得到了正确的角度。

【问题讨论】:

    标签: python math trigonometry


    【解决方案1】:

    你的问题是你的代码格式太糟糕了,你看不到你正在做的带括号的错误。

    错误 1

    例如,这一行:

    sideA = math.sqrt((x - mousex)**2)+((y - mousey)**2)
    

    正确格式化后,如下所示:

    sideA = math.sqrt((x - mousex) ** 2) + ((y - mousey) ** 2)
    

    当您删除多余的括号时,您可以更清楚地看到发生了什么:

    sideA = math.sqrt((x - mousex) ** 2) + (y - mousey) ** 2
    

    您只是将您的一个边的正方形传递给math.sqrt(),然后将第二边的正方形添加到它。应该是:

    sideA = math.sqrt((x - mousex) ** 2 + (y - mousey) ** 2)
    

    甚至更好:

    sideA = math.hypot(x - mousex, y - mousey)
    

    错误 2

    然后这一行:

    cos = float(sideA**2)-(sideB**2)-(sideC**2)/(-2*(sideB*sideC))
    

    有一个类似的问题 - 你在前三个术语周围缺少括号,你只是将 C 边的平方除以 2bc。应该是:

    cos = (sideA ** 2 - sideB ** 2 - sideC ** 2) / ( -2 * sideB * sideC)
    

    解决方案

    由于上述原因,您没有正确计算余弦,因此您传递给 math.acos() 的内容超出了余弦的允许范围(余弦将始终在 @987654330 的范围内@),所以它给了你那个域错误。打印出你的价值观会帮助你看到你得到了一些非常奇怪的东西,在这里。

    这是您的程序的固定和工作版本,修改为直接为 mousexmousey 设置值:

    #!/usr/bin/env python
    
    import math
    
    x, y = 100, 100
    centerX, centerY = x + 50, y + 50
    mousex, mousey = 100,150 
    
    sideA = math.hypot(x - mousex, y - mousey);
    sideB = math.hypot(centerX - mousex, centerY - mousey)
    sideC = math.hypot(centerX - x, centerY - y)
    cosA = (sideB ** 2 + sideC ** 2 - sideA ** 2) / (2 * sideB * sideC)
    angle = math.acos(cosA)
    
    print "sideA: %.2f, sideB: %.2f, sideC: %.2f" % (sideA, sideB, sideC)
    print "cosA: %.6f" % (cosA)
    print "angle: %.2f radians, %.2f degrees" % (angle, math.degrees(angle))
    

    哪个输出:

    paul@horus:~/src/sandbox$ ./angle.py
    sideA: 50.00, sideB: 50.00, sideC: 70.71
    cosA: 0.707107
    angle: 0.79 radians, 45.00 degrees
    paul@horus:~/src/sandbox$ 
    

    我冒昧地稍微重新安排你的余弦规则计算,以消除对分母取反的需要。

    【讨论】:

    • 从未注意到我没有接受答案。你值得那个代表!
    【解决方案2】:

    是的,math.acos 是反余弦的正确函数。

    您的错误在其他地方,但如果没有您输入的确切样本,我们无法为您提供太多帮助。哪一行产生错误?错误信息是什么?

    【讨论】:

    • 错误出现在 math.acos (cos) 行 感谢您对我的测试试验的帮助 x、y 和 centerX centerY 是常数 mouseX mouseY 是屏幕上的鼠标 xy 线我跑的是使用 58 和 102 线作为 mouseX 和 mouseY,我得到 17.39(四舍五入)作为我的答案,但是如果我在我的程序中运行这些数字,我会在 acos 上得到一个错误任何想法?
    • 不,我不知道,因为您的描述再次含糊不清。请给我们一个最小的复制案例——几行代码我们可以在我们的计算机上实际运行,除了 Python 之外没有任何先决条件,并产生您遇到的错误。
    猜你喜欢
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    相关资源
    最近更新 更多