我想获得R中圆圈上等距离 n点的坐标。
Mathematically the solution is: exp((2 * pi * i)*(k / n))其中0 <= k <1。 Ñ
有许多SOF问题可以解决这个问题。所有解决方案都在非R环境中:
Evenly distributing n points on a sphere(java,python solutions present)
Generating points on a circle(非R解决方案)
calculate pixel coordinates for 8 equidistant points on a circle(python解决方案)
drawing points evenly distributed on a circle(非R解决方案)
How to plot points around a circle in R(没有同样的距离)
Coordinates of every point on a circle's circumference(非R解决方案)
Coordinates of points dividing circle into n equal halves in Pebble
How to efficiently draw exactly N points on screen?(python解决方案)
Approximate position on circle for n points(非R解决方案)
Determining Vector points on a circle
我为解决方案做了什么:
# 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))
复数i在R中没有定义。没有与pi相反的常数(3.14)。使用sqrt(-1)来模拟我不起作用;错误:
[1] NaN
Warning message: In sqrt(-1) : NaNs produced
答案 0 :(得分:4)
我们可以使用复数来简单地实现这一点,但您需要使用正确的语法。通常,复数可以写为ai + b
(例如3i + 2
)。如果只有一个虚构的组件,我们只能写ai
。所以,虚构的只是1i
。
Npoints = 10
points = exp(pi * 1i * seq(0, 2, length.out = Npoints+1)[-1])
plot(points)
如果由于任何原因,您需要从复杂平面转换为笛卡尔平面,则可以使用Re()
和Im()
提取实部和虚部。
points.Cartesian = data.frame(x=Re(points), y=Im(points))
答案 1 :(得分:2)
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
话虽如此,你不能在一个圆圈上找到等间距的点而不诉诸复杂的数字吗?
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)
答案 2 :(得分:2)