2024-10-06 07:48:43 发布
网友
如果我有2个点(x0,y0),这是圆的中心,另一个点(x,y)(这是图像中圆边界上的红点)。我怎样才能得到圆点的角度?在
注意,它应该从[0360]返回一个以度为单位的角度。图像中的红点角度大约为70度。在
如何在python中实现这一点?在
谢谢
这似乎行不通。在
(dx, dy) = (x0-x, y-y0) angle = atan(float(dy)/float(dx)) if angle < 0: angle += 180
啊,很容易犯错误。atan返回弧度值,而不是度数。所以你需要把这个角度乘以180/pi以使它回到度。您还需要将您的dy更改为y0 - y,以便与您的dx保持一致。这是一些正确的代码。在
atan
180/pi
dy
y0 - y
dx
dx, dy = x0-x, y0-y angle_in_radians = atan2(dy,dx) # you don't need to cast to float angle_in_degrees = angle_in_radians * 180 / pi
你很亲密:-)
更改此项:
angle = atan(float(dy)/float(dx))
为此:
因为函数atan2(...) atan2(y, x) Return the arc tangent (measured in radians) of y/x. Unlike atan(y/x), the signs of both x and y are considered degrees()函数将弧度转换为度:degrees(...) degrees(x) Convert angle x from radians to degrees. 而且,正如Rich和Cody指出的,你需要修正你的dy计算。在
atan2(...) atan2(y, x) Return the arc tangent (measured in radians) of y/x. Unlike atan(y/x), the signs of both x and y are considered
degrees()函数将弧度转换为度:
degrees(...) degrees(x) Convert angle x from radians to degrees.
而且,正如Rich和Cody指出的,你需要修正你的dy计算。在
除了从弧度转换,请考虑使用atan2而不是atan。尽管atan对圆另一侧的点给出相同的答案,atan2将给出正确的角度,同时考虑dx和{}的符号。它需要两个参数:
atan2
angle = math.degrees(math.atan2(y0 - y, x0 - x)) % 360
请注意,atan2将返回介于-pi和{}之间的值,或者-180度和180度之间的值,因此{}将把结果移到所需的范围。在
-pi
啊,很容易犯错误。
atan
返回弧度值,而不是度数。所以你需要把这个角度乘以180/pi
以使它回到度。您还需要将您的dy
更改为y0 - y
,以便与您的dx
保持一致。这是一些正确的代码。在你很亲密:-)
更改此项:
为此:
^{pr2}$因为函数
degrees()函数将弧度转换为度:
而且,正如Rich和Cody指出的,你需要修正你的dy计算。在
除了从弧度转换,请考虑使用}的符号。它需要两个参数:
atan2
而不是atan
。尽管atan
对圆另一侧的点给出相同的答案,atan2
将给出正确的角度,同时考虑dx
和{请注意,}之间的值,或者-180度和180度之间的值,因此{}将把结果移到所需的范围。在
atan2
将返回介于-pi
和{相关问题 更多 >
编程相关推荐