Chaos Game Sierpinski Triangle

The following figure is a representation of the fractal known as the Sierpinski Triangle. It was drawn using the chaos game technique with 100,000 iterations. The way this technique works is:

  1. A random starting point is chosen;
  2. The next point is placed half-way between the current point and one of the three (selected randomly) fixed exterior vertices of the triangle;
  3. Step two is repeated as many times as desired.

This works because every valid point that is part of the fractal has another valid point half-way between itself and each of the exterior corners. So the point may bounce around for a few iterations, but once it gets close to any valid point on the fractal, all of the subsequent points generated by the rules will also be on the fractal (or approximately so); this system functions as an attractor.

Sierpinski Triangle drawn with 100,000 points

I generated this figure using the statistical software R. Here is the code:

# A script for drawing the "Sierpinski Triangle" fractal using the "chaos game" technique

# Initialize settings
N.pts <- 100000 #Number of trials
Ini.x <- runif(1,0,1) #Initial Location
Ini.y <- runif(1,0,1)
i <- 1 #index counter
Pts.x <- numeric(N.pts)
Pts.y <- numeric(N.pts)

Pts.x[1] <- Ini.x
Pts.y[1] <- Ini.y

# Main Loop
while (i < N.pts){
  Die <- sample((1:6),size=1, replace=T) #Roll a fair die to pick a vertex
  #Go half-way from current point to selected vertex for next point
  if (Die <= 2){
    Pts.x[(i+1)] <- 0.5*(Pts.x[i] + 0) #Vertex 1
    Pts.y[(i+1)] <- 0.5*(Pts.y[i] + 0)
    }else{ 
    if (Die <= 4){
      Pts.x[(i+1)] <- 0.5*(Pts.x[i] + 0.5) #Vertex 2
      Pts.y[(i+1)] <- 0.5*(Pts.y[i] + (sqrt(3)/2))
      }else{ 
      Pts.x[(i+1)] <- 0.5*(Pts.x[i] + 1) #Vertex 3
      Pts.y[(i+1)] <- 0.5*(Pts.y[i] + 0)
    }
  }
    
  i <- i + 1 #increment index counter
}

#Plotting
plot(Pts.x,Pts.y,type="p",pch=".",xlim=c(0,1),ylim=c(0,1))

I believe this technique could be used to generate similar fractal polygons with different numbers of sides (e.g. squares or hexagons) by changing the number and coordinates of vertices and (maybe?) the fractional distance between them where the next point is placed.

Permalink