Uniformly distribute x points inside a circle

后端 未结 2 1970
温柔的废话
温柔的废话 2021-02-01 06:40

I would like to uniformly distribute a predetermined set of points within a circle. By uniform distribution, I mean they should all be equally distanced from each other (hence a

2条回答
  •  囚心锁ツ
    2021-02-01 07:35

    Stumbled across this question and the answer above (so all cred to user3717023 & Matt).
    Just adding my translation into R here, in case someone else needed that :)

    library(tibble)
    library(dplyr)
    library(ggplot2)
    
    sunflower <- function(n, alpha = 2, geometry = c('planar','geodesic')) {
      b <- round(alpha*sqrt(n))  # number of boundary points
      phi <- (sqrt(5)+1)/2  # golden ratio
    
      r <- radius(1:n,n,b)
      theta <- 1:n * ifelse(geometry[1] == 'geodesic', 360*phi, 2*pi/phi^2)
    
      tibble(
        x = r*cos(theta),
        y = r*sin(theta)
      )
    }
    
    radius <- function(k,n,b) {
      ifelse(
        k > n-b,
        1,
        sqrt(k-1/2)/sqrt(n-(b+1)/2)
      )
    }
    
    # example:
    sunflower(500, 2, 'planar') %>%
        ggplot(aes(x,y)) +
        geom_point()
    

提交回复
热议问题