python中三点之间的角度-为什么会出现这种结果? [英] Angle between three points in python - Why this result?

查看:107
本文介绍了python中三点之间的角度-为什么会出现这种结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算点A,B和C之间的角度ABC.我知道数学很简单,但是我不明白为什么我的函数给出错误的结果.首先,这是代码(a包含列表[x, y, z])

    def angle(a, b, c):

        # Create vectors from points
        ba = [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
        bc = [c[0] - b[0], c[1] - b[1], c[2] - b[2]]

        # Normalize vector
        nba = sqrt(ba[0]**2 + ba[1]**2 + ba[2]**2)
        ba = [ba[0]/nba, ba[1]/nba, ba[2]/nba]

        nbc = sqrt(bc[0]**2 + bc[1]**2 + bc[2]**2)
        bc = [bc[0]/nbc, bc[1]/nbc, bc[2]/nbc]

        # Calculate scalar from normalized vectors
        scal = ba[0]*bc[0] + ba[1]*bc[1] + ba[2]*bc[2]

        # calculate the angle in radian
        angle = acos(scal)

此功能给出错误的结果.实际上,如果我将第二个向量从bc更改为cb,则可以得到很好的结果:

    cb = [b[0]-c[0], b[1]-c[1], b[2]-c[2]]

我不明白为什么我的第一个解决方案应该像我遵循数学一样能很好地工作并给出良好的结果...

解决方案

首先,您的代码是非Python语言的.这是一个建议:

def angle(a, b, c):

    # Create vectors from points
    ba = [ aa-bb for aa,bb in zip(a,b) ]
    bc = [ cc-bb for cc,bb in zip(c,b) ]

    # Normalize vector
    nba = sqrt ( sum ( (x**2.0 for x in ba) ) )
    ba = [ x/nba for x in ba ]

    nbc = sqrt ( sum ( (x**2.0 for x in bc) ) )
    bc = [ x/nbc for x in bc ]

    # Calculate scalar from normalized vectors
    scal = sum ( (aa*bb for aa,bb in zip(ba,bc)) )

    # calculate the angle in radian
    angle = acos(scale)

第二,您的代码可能返回正确的角度,但可能不是您期望的角度.

假设这种情况:

A-----C
|    /
|   /
|  /
| /
|/
B

您要计算的角度是B处的底角,而不是A处的左上角,这通常是人们在将三个向量(a,b,c)传递到返回角度的函数中时想要的. /p>

I tried to calculate the angle ABC between points A, B and C. I know the math are pretty basic but I don't understand why my function give the wrong result. First, here is the code (a contains a list [x, y, z])

    def angle(a, b, c):

        # Create vectors from points
        ba = [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
        bc = [c[0] - b[0], c[1] - b[1], c[2] - b[2]]

        # Normalize vector
        nba = sqrt(ba[0]**2 + ba[1]**2 + ba[2]**2)
        ba = [ba[0]/nba, ba[1]/nba, ba[2]/nba]

        nbc = sqrt(bc[0]**2 + bc[1]**2 + bc[2]**2)
        bc = [bc[0]/nbc, bc[1]/nbc, bc[2]/nbc]

        # Calculate scalar from normalized vectors
        scal = ba[0]*bc[0] + ba[1]*bc[1] + ba[2]*bc[2]

        # calculate the angle in radian
        angle = acos(scal)

This function gives wrong result. In fact, it gives the good result if I change the second vector from bc to cb:

    cb = [b[0]-c[0], b[1]-c[1], b[2]-c[2]]

I don't understand why, as if I follow math, my first solution should work well and give the good result...

解决方案

Firstly, your code is very non-pythonic. Here is a suggestion:

def angle(a, b, c):

    # Create vectors from points
    ba = [ aa-bb for aa,bb in zip(a,b) ]
    bc = [ cc-bb for cc,bb in zip(c,b) ]

    # Normalize vector
    nba = sqrt ( sum ( (x**2.0 for x in ba) ) )
    ba = [ x/nba for x in ba ]

    nbc = sqrt ( sum ( (x**2.0 for x in bc) ) )
    bc = [ x/nbc for x in bc ]

    # Calculate scalar from normalized vectors
    scal = sum ( (aa*bb for aa,bb in zip(ba,bc)) )

    # calculate the angle in radian
    angle = acos(scale)

Secondly, your code is probably returning the correct angle, but maybe not the angle you expected.

Assuming this scenario:

A-----C
|    /
|   /
|  /
| /
|/
B

The angle you're calculating is the bottom angle at B, not the top-left angle at A which is usually what people want when they pass three vectors (a,b,c) into a function that returns angles.

这篇关于python中三点之间的角度-为什么会出现这种结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆