**The Devil is in the Data**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Wacław Sierpiński was a mathematical genius who developed several of the earliest fractals. The Sierpiński triangle is an easy to conceptualise geometrical figure but it hides a fascinating mathematical complexity. Start by drawing an equilateral triangle and draw another one in its centre. Then draw equilateral triangles in the four resulting triangles, and so on, *ad infinitum*.

The original Sierpinski triangle will eventually disappear into Cantor dust, a cloud of ever shrinking triangles of infinitesimal size. The triangle is self-similar, no matter how far you zoom in, the basic geometry remains the same.

## The Chaos Game

A fascinating method to create a Sierpinski Triangle is a chaos game. This method uses random numbers and some simple arithmetic rules. Sierpinski Triangles can be created using the following six steps:

- Define three points in a plane to form a triangle.
- Randomly select any point on the plane.
- Randomly select any one of the three triangle points.
- Move half the distance from your current position to the selected vertex.
- Plot the current position.
- Repeat from step 3.

This fractal is an implementation of chaos theory as this random process attracts to a complex ordered geometry. The game only works with random numbers and when selecting random vertices of the triangle.

## Sierpinski Triangle Code

This code implements the six rules in R. The code first initializes the triangle, defines a random starting point and then runs a loop to place random dots. The R plot engine does not draw pixels but uses characters, which implies that the diagram is not as accurate as it could be but the general principle is clear. The x(11) and Sys.sleep() commands are used to plot during the for-loop.

# Sierpinsky Triangle # Initialise triangle p <- c(0, 500, 1000) q <- c(0, 1000, 0) x11() par(mar = rep(0, 4)) plot(p, q, col= "red", pch = 15, cex = 1, axes = FALSE) # Random starting point x <- sample(0:1000, 1) y <- sample(0:1000, 1) # Chaos game for (i in 1:10000) { Sys.sleep(.001) n <- sample(1:3, 1) x <- floor(x + (p[n] - x) / 2) y <- floor(y + (q[n] - y) / 2) points(x, y, pch = 15, cex = 0.5) }

This algorithm demonstrates how a seemingly chaotic process can result in order. Many other versions of chaos games exist, which I leave to the reader to play with. If you create your own versions then please share the code in the comment box below.

The post The Sierpinski Triangle: Visualising infinity in R appeared first on The Devil is in the Data.

**leave a comment**for the author, please follow the link and comment on their blog:

**The Devil is in the Data**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.