5

I want to get the coordinates of the equally distanced n points on a circle in R.

Mathematically the solution is: exp((2*pi * i)*(k/n)) where 0 <= k < n

There are many SOF questions to handle this problem. All the solutions are in non-R environments:

Evenly distributing n points on a sphere (java, python solutions presented)

Generating points on a circle (non-R solution)

calculate pixel coordinates for 8 equidistant points on a circle (python solution)

drawing points evenly distributed on a circle (non-R solution)

How to plot points around a circle in R (no equally distancing)

Coordinates of every point on a circle's circumference (non-R solution)

Coordinates of points dividing circle into n equal halves in Pebble

How to efficiently draw exactly N points on screen? (python solution)

Approximate position on circle for n points (non-R solution)

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). The trick sqrt(-1) to similate i does not work; the error:

[1] NaN 
Warning message: In sqrt(-1) : NaNs produced
Community
  • 1
  • 1
Erdogan CEVHER
  • 1,788
  • 1
  • 21
  • 40

3 Answers3

6

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 (e.g. 3i + 2). If there is only an imaginary component, we can write just ai. So, imaginary one is simply 1i.

Npoints = 20
points = exp(2i * pi * (1:Npoints)/Npoints)
plot(points)

enter image description here

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().

points.Cartesian = data.frame(x=Re(points), y=Im(points))
dww
  • 30,425
  • 5
  • 68
  • 111
4

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

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
3
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)

enter image description here

sebastian-c
  • 15,057
  • 3
  • 47
  • 93
  • f(0/4)=0+0i is origin, not on the unit circle. When I changed `exp(2*pi*i)*x` to `exp((2*pi*i)*x)` in your f definition, it worked – Erdogan CEVHER Oct 27 '16 at 08:05
  • 1
    Sorry, I made an error when removing brackets. I'll fix that. – sebastian-c Oct 27 '16 at 08:08
  • Functionally, sebastian-c's answer is true since it includes complex coordinates (e.g. (0,i)). On the other hand, sandipan's answer is practical since it isolates complex coordinates and presents (x,y) numbers with no i's. Being really undecided which one to accept as an answer, I chose sandipan's. By the way, many thanks to sebastian since he thought me `as.complex(-1)` – Erdogan CEVHER Oct 27 '16 at 08:22
  • 1
    Much simpler would be to use `1i`. No need to muck around with `as.complex` or `sqrt(-1)` – dww Oct 27 '16 at 12:21
  • @dww I knew there was something like that but neglected to look for it, thanks! – sebastian-c Oct 27 '16 at 13:13