Snippets

Stuart Morrison Chaos games

Updated by Stuart Morrison

File chaos_games Deleted

  • Ignore whitespace
  • Hide word diff
-
-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

File chaos_games.r Added

  • Ignore whitespace
  • Hide word diff
+
+shapes <- function(num, iter){
+  # initialise a vector for starting points
+  basex <- rep(0, num)
+  basey <- rep(0, num)
+  
+  # Create starting points
+  for (i in 1:num){
+    basex[i] = 10 * cos(2 * pi * i / num)
+    basey[i] = 10 * sin(2 * pi * i / num)
+  }
+  
+  # initialise vectors for each draw
+  iterx <- rep(0, iter)
+  itery <- rep(0, iter)
+  
+  # draw random uniforms to pick each point
+  iterx[1] <- runif(n = 1, min = min(basex), max = max(basex))
+  itery[1] <- runif(n = 1, min = min(basey), max = max(basey))
+  
+  # pick which starting point to move towards
+  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)))
+  }
+  
+  # create a data frame for output
+  start_points <- data.frame(x = basex, y = basey)
+  iter_points <- data.frame(point = (1:iter), x = iterx, y = itery)
+  
+  return(iter_points)
+}
Created by Stuart Morrison

File chaos_games Added

  • Ignore whitespace
  • Hide word diff
+
+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.