我已经写了一个函数来计算三个点之间的余弦,正弦和角度,我有x和y坐标-点1 (x1,y1),点2 (x2,y2)和点3 (x3,y3)。我已经编写了这个函数,并试图对其进行测试,但我对它的准确性并不完全有信心。有没有人知道我的计算有没有错?
def path_angle_degree(x1, y1, x2, y2, x3, y3):
u = (x2 - x1, y2 - y1)
v = (x3 - x2, y3 - y2)
norm_u = math.sqrt(u[0] * u[0] + u[1] * u[1])
norm_v = math.sqrt(v[0] * v[0] + v[1] * v[1])
# this conditional is to check there has been movement between the points
if norm_u < 0.001 or norm_v < 0.001:
return (None, None, None)
prod_n = norm_u * norm_v
dot_uv = u[0] * v[0] + u[1] * v[1]
cos_uv = dot_uv / prod_n
# fixes floating point rounding
if cos_uv > 1.0 or cos_uv < -1.0:
cos_uv = round(cos_uv)
radians = math.acos(cos_uv)
sin_uv = math.sin(radians)
degree = math.degrees(radians)
return (cos_uv, sin_uv, degree)在直接路径上调用此函数的示例如下:
print(path_angle_degree(6,6,7,6,8,6))非常感谢!
发布于 2015-04-16 15:57:05
除了昂贵之外,sin_uv是不稳定的,并且不能处理大量的边缘情况。改用叉积(您只需要z分量)。
此外,你会发现在计算乘积之前对u和v进行归一化会更简单、更便宜。
有了cos_uv和sin_uv之后,使用atan2获取角度。
发布于 2015-04-16 17:25:41
您可能需要查看arctan2()函数。
https://stackoverflow.com/questions/29667990
复制相似问题