如何从中心点得到点的角度?

2024-10-06 07:48:43 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有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

enter image description here


Tags: 图像if单位float中心边界角度angle
3条回答

啊,很容易犯错误。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))

为此:

^{pr2}$

因为函数

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和{}的符号。它需要两个参数:

angle = math.degrees(math.atan2(y0 - y, x0 - x)) % 360

请注意,atan2将返回介于-pi和{}之间的值,或者-180度和180度之间的值,因此{}将把结果移到所需的范围。在

相关问题 更多 >