繁体   English   中英

将x和y转换为弧度角

[英]Convert x and y to angle in radians

我使用以下代码创建带有随机rho和theta的点:

set.seed(1)
rho <- sqrt(runif(1, 0.0, 1.0))
theta <- runif(1, 0, 2*pi)

获得rho=0.515theta=2.338

我可以分别使用-0.3580.371获得x=rho*cos(theta)y=rho*sin(theta)的x和y值

但是,如果我正在执行逆过程

   r<-sqrt(x^2+y^2)

结果与rho相同,但是

   a<-atan(y/x)

我得到的结果不同于theta。

你能告诉我我做错了吗?

您有x < 0y/x = -1.036811 < 0 现在,这意味着theta只能位于第二或第四象限。

tan(-z)=-tan(z)=tan(2*pi-z)=tan(pi-z)=w ,则-zpi-z2*pi-z都等于atan(w) ,解在z不是唯一的。

atan(y/x)
#[1] -0.8034692 

-0.8034692是一种解决方法

pi+atan(y/x)
#[1] 2.338123

2*pi+atan(y/x)
#[1] 5.479716

也是解决方案。

c(tan(atan(y/x)), tan(pi+atan(y/x)), tan(2*pi+atan(y/x)))
# [1] -1.036811 -1.036811 -1.036811

如果我们有兴趣找到解0<theta<pi则唯一的候选解是pi+atan(y/x)=2.338123

最好使用atan2

atan2(y, x)
#[1] 2.338364

(几乎)等于theta

这里有更多讨论。

暂无
暂无

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

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