简体   繁体   English

三点之间的角度

[英]angle between three points

I am trying to get the angle between three points on a plane using this formula: 我正在尝试使用此公式获取平面上三个点之间的角度:

cos-1((P122 + P132 - P232)/(2 * P12 * P13))

but occasionally I am getting a math domain exception. 但是偶尔我会遇到数学域异常。 here is the function: 这是函数:

P12 = math.sqrt((x1-x2)**2 + (y1-y2)**2)
P13 = math.sqrt((x1-x3)**2 + (y1-y3)**2)
P23 = math.sqrt((x2-x3)**2 + (y2-y3)**2)

if P12 ==0 or P23 ==0 or P13 ==0 :
    return 0
return math.acos((P12**2 + P13**2 - P23**2) / (2*P12*P13) )

where P1 is the vertex what could be going wrong here? P1是顶点,这里可能出什么问题?

Thanks 谢谢

You will sometimes get this error when the points are co-linear (or possibly very close to co-linear) 当这些点共线(或可能非常接近共线)时,有时会出现此错误

eg 例如

(-1,-1),(1,1),(100,100)

gives

P12 = 140.007142675 
P13 = 2.82842712475
P23 = 142.8355698

and

>>> (P12**2 + P13**2 - P23**2) / (2*P12*P13)
-1.0000000000901725

It's due to floating point errors and you'll end up passing a number slightly larger than 1 or slightly smaller than -1 to acos 这是由于浮点错误引起的,您最终会将一个稍微大于1或小于-1的数字传递给acos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM