Generative Leaves

Classic graphing fun!

visualization
EPPS6356
assignments
Author

Kyndall Brown

Published

September 28, 2022

For this assignment, I used this code to generate a few pieces of art! Below is the code I used and a small selection of artistic works. :)

# Title Fall color
# Credit: https://fronkonstin.com

# Install packages

install.packages("gsubfn")
install.packages("tidyverse")
library(gsubfn)
library(tidyverse)

# Define elements in plant art
# Each image corresponds to a different axiom, rules, angle and depth

# Leaf of Fall

axiom="X"
rules=list("X"="F-[[X]+X]+F[+FX]-X", "F"="FF")
angle=22.5
depth=6


for (i in 1:depth) axiom=gsubfn(".", rules, axiom)

actions=str_extract_all(axiom, "\\d*\\+|\\d*\\-|F|L|R|\\[|\\]|\\|") %>% unlist

status=data.frame(x=numeric(0), y=numeric(0), alfa=numeric(0))
points=data.frame(x1 = 0, y1 = 0, x2 = NA, y2 = NA, alfa=90, depth=1)


# Generating data
# Note: may take a minute or two

for (action in actions)
{
  if (action=="F")
  {
    x=points[1, "x1"]+cos(points[1, "alfa"]*(pi/180))
    y=points[1, "y1"]+sin(points[1, "alfa"]*(pi/180))
    points[1,"x2"]=x
    points[1,"y2"]=y
    data.frame(x1 = x, y1 = y, x2 = NA, y2 = NA,
               alfa=points[1, "alfa"],
               depth=points[1,"depth"]) %>% rbind(points)->points
  }
  if (action %in% c("+", "-")){
    alfa=points[1, "alfa"]
    points[1, "alfa"]=eval(parse(text=paste0("alfa",action, angle)))
  }
  if(action=="["){
    data.frame(x=points[1, "x1"], y=points[1, "y1"], alfa=points[1, "alfa"]) %>%
      rbind(status) -> status
    points[1, "depth"]=points[1, "depth"]+1
  }

  if(action=="]"){
    depth=points[1, "depth"]
    points[-1,]->points
    data.frame(x1=status[1, "x"], y1=status[1, "y"], x2=NA, y2=NA,
               alfa=status[1, "alfa"],
               depth=depth-1) %>%
      rbind(points) -> points
    status[-1,]->status
  }
}

ggplot() +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),
               lineend = "round",
               color="burlywood3", # Set your own Fall color?
               data=na.omit(points)) +
  coord_fixed(ratio = 1) +
  theme_void() # No grid nor axes

After running the code, it created this leaf:

I created a few more leaves by changing the code. For my next leaf, I changed the color to sienna2 and altered the code like this:

angle=20
depth=6

The resulting leaf looked like this:

It’s interesting how the rest of the leaf was unchanged by it added this extra leaf on the side, I didn’t expect that!

I made one final leaf by changing the color to lightcyan and altering the code like this:

axiom="X"
rules=list("X"="F-\[\[X\]+X\]+F\[+FX\]-X", "F"="FF+1")
angle=23
depth=7

I changed the angle to 23, but also the depth to 7 and added +1 to “F” in the axiom variable. The result was shocking:

It’s amazing how a simple change can produce such large effects!