r - 将 x 和 y 转换为以弧度为单位的角度

标签 r coordinates trigonometry angle points

我使用以下代码创建一个带有随机 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.358x=rho*cos(theta)y=rho*sin(theta) 获取 x 和 y 值0.371 分别

但是,如果我执行相反的过程

   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 ,然后-z , pi-z , 2*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

关于r - 将 x 和 y 转换为以弧度为单位的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42540092/

相关文章:

regex - 从字符串中替换 "/?"

C# WPF OxyPlot x, y 应该具有相同的缩放比例

Python:如何将包含行列格式坐标的文本文件读入 x-y 坐标数组?

Python 正弦和余弦给出错误的值

math - 反射?我该怎么做?

r - .Call 的 PACKAGE 参数如何工作?

r - 无法使用来自 tidyr 的新 pivot_wider() 填充缺失值

r - 错误 : Must supply a symbol or a string as argument

java - (Geotools 库)如何转换韩国坐标(EPSG :5179) to Decimal Degree coordinates (EPSG:4326)

android - 使用 SoundPool 生成 10 到 20000Hz 之间的正弦波