Fractal Fern
Apr 5, 2013
1 minute read

Let's plant a Barnsley fern.

library(ggplot2)

z <- matrix(c(0, 0), nrow = 2)
x <- c()
y <- c()

for (i in 1:40000) {
  
  r <- runif(1)
  
  if (r < .01) {
    z <- matrix(c(0, 0, 0, .16), nrow = 2, byrow = T) %*% z
  }
  
  else if (r < .86) {
    z <- matrix(c(.85, .04, -.04, .85), nrow = 2, byrow = T) %*% z + c(0, 1.6)
  }
  
  else if (r < .93) {
    z <- matrix(c(0.2, -.26, .23, .22), nrow = 2, byrow = T) %*% z + c(0, 1.6)
  }
  
  else {
    z <- matrix(c(-.15, .28, .26, .24), nrow = 2, byrow = T) %*% z + c(0, .44)
  }
  
  x[i] <- z[1]
  y[i] <- z[2]
  
}

qplot(x, y, size = I(.5)) + theme_bw() + opts(axis.line = element_blank(), 
      axis.text.x = element_blank(), axis.text.y = element_blank(), 
      axis.ticks = element_blank(), axis.title.x = element_blank(),
      axis.title.y = element_blank()) + scale_x_continuous(breaks = NULL) + 
      scale_y_continuous(breaks = NULL) + ggtitle("Fractal Fern")
	  

Pretty.