Snippets

Stuart Morrison Chaos games

You are viewing an old version of this snippet. View the current version.
Revised by Stuart Morrison ec81884

library(ggplot2)

shapes <- function(num, iter){
  basex <- rep(0, num)
  basey <- rep(0, num)
  for (i in 1:num){
    basex[i] = 10 * cos(2 * pi * i / num)
    basey[i] = 10 * sin(2 * pi * i / num)
  }
  #
  iterx <- rep(0, iter)
  itery <- rep(0, iter)
  #
  iterx[1] <- runif(n = 1, min = min(basex), max = max(basex))
  itery[1] <- runif(n = 1, min = min(basey), max = max(basey))
  #
  pick <- ceiling(runif(n = iter, min = 0, max = 1) * num)
  for (i in 2:iter){
    iterx[i] <- iterx[(i-1)] + ((basex[pick[i]] - iterx[(i-1)]) * ((num - 2) / (num - 1)))
    itery[i] <- itery[(i-1)] + ((basey[pick[i]] - itery[(i-1)]) * ((num - 2) / (num - 1)))
  }
  #
  start_points <- data.frame(x = basex, y = basey)
  iter_points <- data.frame(point = (1:iter), x = iterx, y = itery)
  
  return(iter_points)
}

triangle <- shapes(3, 2500)
square <- shapes(4, 2500)
pentagon <- shapes(5, 2500)

tri <- ggplot(triangle, aes(x = x, y = y)) + 
  geom_point() +
  theme_classic()

sq <- ggplot(square, aes(x = x, y = y)) +
  geom_point() +
  theme_classic()

penta <- ggplot(pentagon, aes(x = x, y = y)) +
  geom_point() +
  theme_classic()

tri
sq
penta
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.