R中圆圈上等距n点的坐标?

时间:2016-10-27 07:48:50

标签: r geometry coordinates evenly

我想获得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

3 个答案:

答案 0 :(得分:4)

我们可以使用复数来简单地实现这一点,但您需要使用正确的语法。通常,复数可以写为ai + b(例如3i + 2)。如果只有一个虚构的组件,我们只能写ai。所以,虚构的只是1i

Npoints = 10
points = exp(pi * 1i * seq(0, 2, length.out = Npoints+1)[-1])
plot(points)

enter image description here

如果由于任何原因,您需要从复杂平面转换为笛卡尔平面,则可以使用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)

enter image description here

答案 2 :(得分:2)

哟也可以尝试这个(避免复杂算术)在真实平面上的单位圆上有点:

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')

enter image description here