怎么把atan转换成atan2? [英] How to convert from atan to atan2?

查看:130
本文介绍了怎么把atan转换成atan2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如果要从atan()函数创建atan2()的效果,该如何处理?例如,如果我具有笛卡尔坐标(x,y),并想使用atan()函数找到(x,y)的反正切值,以便它在所有象限中都有效,那么无论x或y的符号如何,我可以不使用atan2()函数来做到这一点吗?在笛卡尔和极坐标之间进行转换的示例中:

  def cartopol(x,y):r =(x ** 2 + y ** 2)** 0.5p = math.atan(y/x)返回(r,p) 

使用atan()函数当前无法使用此公式返回正确的弧度角,那么必须更改什么?

简而言之, math.atan 仅适用于第1和第4象限.对于第2和第3象限(即x<0),则您必须添加或减去pi(180度)才能到达象限1和4,并找到相应的theta值.在代码中,这意味着

  if(y< 0和x< 0):打印(math.atan(y/x)-math.pi)省略号(y> 0和x< 0):打印(math.atan(y/x)+ math.pi) 

有关更多参考,请查看 atan2 .其他两个用例是 x>0 ,与 atan 一起使用非常好.

In Python, if one wanted to create the effect of atan2() from the atan() function, how would one go about this? For example, if I had cartesian coordinates (x, y) and wanted to find the arctan of (x, y) using the atan() function such that it worked in all quadrants, so regardless of the sign of x or y, how could I do this without using the atan2() function? In the example of converting between cartesian and polar:

def cartopol(x, y):
    r = (x**2 + y**2)**0.5
    p = math.atan(y/x)
    return(r,p)

This formula for returning the correct angle in radians would not currently work using the atan() function, so what must change?

解决方案

In a nutshell, math.atan only works for quadrants 1 and 4. For quadrants 2 and 3 (i.e. for x < 0), then you'd have to add or subtract pi (180 degrees) to get to quadrants 1 and 4 and find the respective value for the theta. In code, it means that

if (y < 0 and x < 0): 
    print(math.atan(y/x) - math.pi)
elif (y > 0 and x < 0): 
    print(math.atan(y/x) + math.pi)

For more reference, take a look at atan2. The other two use cases are those in which x > 0, which works well with atan.

这篇关于怎么把atan转换成atan2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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