[英]Coordinates of equally distanced n points on a circle in R?
I want to get the coordinates of the equally distanced n points on a circle in R. 我想得到R中圆圈上等距 n点的坐标。
Mathematically the solution is : exp((2*pi * i)*(k/n)) where 0 <= k < n 数学上解决方案是 :exp((2 * pi * i)*(k / n))其中0 <= k <n
There are many SOF questions to handle this problem. 有许多SOF问题可以解决这个问题。 All the solutions are in non-R environments:
所有解决方案都在非R环境中:
Evenly distributing n points on a sphere (java, python solutions presented) 均匀分布球体上的n个点 (java,python解决方案)
Generating points on a circle (non-R solution) 在圆上生成点 (非R解)
calculate pixel coordinates for 8 equidistant points on a circle (python solution) 计算圆上8个等距点的像素坐标 (python解)
drawing points evenly distributed on a circle (non-R solution) 绘图点均匀分布在圆上 (非R解)
How to plot points around a circle in R (no equally distancing) 如何在R中围绕圆圈绘制点 (没有相同的距离)
Coordinates of every point on a circle's circumference (non-R solution) 圆周上每个点的坐标 (非R解)
Coordinates of points dividing circle into n equal halves in Pebble 在Pebble中将圆分为n等分的坐标
How to efficiently draw exactly N points on screen? 如何在屏幕上有效地绘制N个点? (python solution)
(python解决方案)
Approximate position on circle for n points (non-R solution) n点圆的近似位置 (非R解)
Determining Vector points on a circle 确定圆上的矢量点
What I did for solution: 我为解决方案做了什么:
# 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))
Complex number i is not defined in R. There is no such constant as opposite to pi (3.14). 复数i在R中没有定义。没有与pi相反的常数(3.14)。 The trick sqrt(-1) to similate i does not work;
使用sqrt(-1)来模拟我不起作用; the error:
错误:
[1] NaN
Warning message: In sqrt(-1) : NaNs produced
we can use complex numbers to achieve this quite simply, but you need to use the correct syntax. 我们可以使用复数来实现这一点,但是您需要使用正确的语法。 in general, complex numbers can be written as
ai + b
(eg 3i + 2
). 通常,复数可以写为
ai + b
(例如3i + 2
)。 If there is only an imaginary component, we can write just ai
. 如果只有一个虚构的组件,我们可以只写一个
ai
。 So, imaginary one is simply 1i
. 所以,想象一个就是
1i
。
Npoints = 10
points = exp(pi * 1i * seq(0, 2, length.out = Npoints+1)[-1])
plot(points)
If, for any reason, you need to translate from a complex to a Cartesian plane, you can extract the real and imaginary components using Re()
and Im()
. 如果由于任何原因,您需要从复数平面转换为笛卡尔平面,则可以使用
Re()
和Im()
提取实部和虚部。
points.Cartesian = data.frame(x=Re(points), y=Im(points))
f <- function(x){
i <- sqrt(as.complex(-1))
exp(2*pi*i*x)
}
> f(0/4)
[1] 1+0i
> f(1/4)
[1] 0+1i
> f(2/4)
[1] -1+0i
> f(3/4)
[1] 0-1i
Having said that, couldn't you find equally spaced points on a circle without resorting to complex numbers? 话虽如此,你不能在不借助复杂数字的情况下在圆上找到等间距的点吗?
eq_spacing <- function(n, r = 1){
polypoints <- seq(0, 2*pi, length.out=n+1)
polypoints <- polypoints[-length(polypoints)]
circx <- r * sin(polypoints)
circy <- r * cos(polypoints)
data.frame(x=circx, y=circy)
}
eq_spacing(4)
x y
1 0.000000e+00 1.000000e+00
2 1.000000e+00 6.123032e-17
3 1.224606e-16 -1.000000e+00
4 -1.000000e+00 -1.836910e-16
plot(eq_spacing(20), asp = 1)
Yo can try this too (and avoid complex arithmetic) to have points on the unit circle on the real plane: 你也可以尝试这个(并避免复杂的算术)在真实平面上的单位圆上有点:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.