【问题标题】:Coordinates of equally distanced n points on a circle in R?R中圆上等距n个点的坐标?
【发布时间】:2017-03-09 19:26:13
【问题描述】:

我想得到R中一个圆上等距n个点的坐标。

Mathematically the solution is: exp((2*pi * i)*(k/n)) 其中 0

有很多 SOF 问题可以处理这个问题。所有解决方案都在非 R 环境中:

Evenly distributing n points on a sphere(提出java、python解决方案)

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) 相反的常数。模拟 i 的技巧 sqrt(-1) 不起作用;错误:

[1] NaN 
Warning message: In sqrt(-1) : NaNs produced

【问题讨论】:

    标签: r geometry coordinates evenly


    【解决方案1】:
    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)
    

    【讨论】:

    • f(0/4)=0+0i 是原点,不在单位圆上。当我在你的 f 定义中将 exp(2*pi*i)*x 更改为 exp((2*pi*i)*x) 时,它起作用了
    • 抱歉,删除括号时出错。我会解决的。
    • 从功能上讲,sebastian-c 的答案是正确的,因为它包含复杂的坐标(例如 (0,i))。另一方面,sandipan 的答案是实用的,因为它隔离了复杂的坐标并呈现没有 i 的 (x,y) 数字。由于真的不确定接受哪一个作为答案,我选择了桑迪潘的。顺便说一句,非常感谢塞巴斯蒂安,因为他认为我是as.complex(-1)
    • 使用1i 会简单得多。无需乱用as.complexsqrt(-1)
    • @dww 我知道有类似的东西,但忽略了寻找,谢谢!
    【解决方案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')
    

    【讨论】:

      【解决方案3】:

      我们可以很简单地使用复数来实现这一点,但您需要使用正确的语法。一般来说,复数可以写成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))
      

      【讨论】:

      • 我将 ...Npoints+1)[-1]) 更改为 ...Npoints+1)[-(Npoints+1)]) 以完全匹配数学解 (01i 摆脱我们 sqrt(as.complex(-1)) 对我来说是一个非常新的想法。非常感谢。
      • 这真是太好了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      相关资源
      最近更新 更多