## 4.4 EXTRA: Creating fractals from random processes

Below are examples of so-called Iterated Function Systems, copy the code and run it in R (Matlab scripts are here)

Try to understand what is going on in the two examples below. - How does the structure come about? We are drawing random numbers! - What’s the difference between the Siepinsky Gasket and the Fern?

### 4.4.1 A Triangle

# Sierpinski Gasket using Iterated Function Systems
#
# Module Dynamical and Nonlinear Data analysis and Modeling
#
# May 2008
# Fred Hasselman & Ralf Cox

require(dplyr)

x = 0                  # Starting points
y = 0

# Emppty plot
plot(x,y, xlim=c(0,2), ylim=c(0,1))

for(i in 1:20000){      # This takes some time: 20.000 iterations

coor=runif(1)       # coor becomes a random number between 0 and 1 drawn from the uniform distribution

# Equal chances (33%) to perform one of these 3 transformations of x and y
if(coor<=0.33){
x=0.5*x
y=0.5*y
points(x,y,pch=".", col="green") #plot these points in green
}

if(between(coor,0.33,0.66)){
x=0.5*x+0.5
y=0.5*y+0.5
points(x,y, pch=".", col="blue") # plot these points in blue
}

if(coor>0.66){
x=0.5*x+1
y=0.5*y
points(x,y, pch=".", col="red") #plot these points in red
}
} # for ...

### 4.4.2 A Fern

# Barnsley's Fern using Iterated Function Systems
#
# Module Dynamical and Nonlinear Data analysis and Modeling
#
# May 2008
# Fred Hasselman & Ralf Cox

require(dplyr)

x = 0                  # Starting points
y = 0

# Emppty plot
plot(x,y, pch=".", xlim=c(-3,3), ylim=c(0,12))

for(i in 1:50000){      # This takes some time: 20.000 iterations

coor=runif(1)       # coor becomes a random number between 0 and 1 drawn from the uniform distribution

if(coor<=0.01){                  #This transformation 1% of the time
x = 0
y = 0.16 * y
points(x,y, pch=".", col="green3")
}

if(between(coor,0.01, 0.08)){   #This transformation 7% of the time
x = 0.20 * x - 0.26 * y
y = 0.23 * x + 0.22 * y + 1.6
points(x,y, pch=".", col="green2")
}

if(between(coor,0.08,0.15)){   #This transformation 7% of the time
x = -0.15 * x + 0.28 * y
y =  0.26 * x + 0.24 * y + 0.44
points(x,y, pch=".", col="springgreen3")
}

if(coor>0.15){      #This transformation 85% of the time
x =  0.85 * x + 0.04 * y
y=  -0.04 * x + 0.85 * y + 1.6
points(x,y, pch=".", col="springgreen2")
}

} # for ...

### 4.4.3 The fractal / chaos game

These Iterated Function Systems also go by the name of ‘the fractal game’ and are used in computer science, the gaming industry, graphic design, etc.

EXTRA-EXTRA: This Wikipedia page on Barnsley’s fern has some good info on the topic. At the end they display Mutant varieties. Try to implement them!

You can by now probably guess that the these simple rules can be described as constraints on the degrees of freedom of the system. Like with the models of growth we simulated, the rules of the fractal game can be made dependent on other processes or events. A great example are the so-called fractal flames implemented in a screen-saver called electric sheep, which combines genetic algorithms, distributed computind and user input (“likes”) to create intruiging visual patterns on your computer screen.3

1. Use at your own risk! You will find yourself silently staring at the screen for longer periods of time.