Erd*_*HER 5 geometry r coordinates evenly
我想得到R中圆上等距离的n个点的坐标。
数学上的解决方案是: exp((2*pi * i)*(k/n)) 其中 0 <= k < n
有很多 SOF 问题可以解决这个问题。所有解决方案都在非 R 环境中:
在球体上均匀分布n个点(提供java、python解决方案)
在圆上生成点(非 R 解决方案)
计算圆上8个等距点的像素坐标(python解决方案)
绘制均匀分布在圆上的点(非R解)
如何在R中绘制圆周围的点(没有等距)
圆圆周上每个点的坐标(非 R 解)
如何高效地在屏幕上精确绘制N个点?(Python解决方案)
n 点在圆上的近似位置(非 R 解)
我为解决方案所做的:
# For 4 points, 0<=k<4
exp((2*pi*sqrt(-1))*(0/4)); exp((2*pi*sqrt(-1))*(1/4)); exp((2*pi*sqrt(-1))*(2/4)); exp((2*pi*sqrt(-1))*(3/4))
Run Code Online (Sandbox Code Playgroud)
R 中没有定义复数 i。不存在与 pi (3.14) 相反的常数。模拟 i 的 sqrt(-1) 技巧不起作用;错误:
[1] NaN
Warning message: In sqrt(-1) : NaNs produced
Run Code Online (Sandbox Code Playgroud)
我们可以使用复数来非常简单地实现这一点,但您需要使用正确的语法。一般来说,复数可以写成ai + b(例如3i + 2)。如果只有虚数部分,我们可以只写ai。所以,虚数就是简单的1i。
Npoints = 20
points = exp(2i * pi * (1:Npoints)/Npoints)
plot(points)
Run Code Online (Sandbox Code Playgroud)
如果出于任何原因,您需要从复数平面转换为笛卡尔平面,则可以使用Re()和提取实部和虚部Im()。
points.Cartesian = data.frame(x=Re(points), y=Im(points))
Run Code Online (Sandbox Code Playgroud)
您也可以尝试这个(并避免复杂的算术),以便在实平面上的单位圆上有点:
n <- 50 # number of points you want on the unit circle
pts.circle <- t(sapply(1:n,function(r)c(cos(2*r*pi/n),sin(2*r*pi/n))))
plot(pts.circle, col='red', pch=19, xlab='x', ylab='y')
Run Code Online (Sandbox Code Playgroud)