In Part I, I explained what the Sierpinski’s Triangle is, and how it is based on fractals. In this post, we will actually build the Sierpinski’s Triangle, reviewing the basic mathematical concepts behind the Triangle, then using Python to build it. This tutorial is based on Giles McCullen-Klein’s ‘Sierpinski’s Triangle’ section of his excellent ‘Python Programming Bootcamp‘ course on the 365 Data Science platform (also on Udemy):

To review, the Sierpinskis Triangle is a fractal, a self-replicating geometric pattern that exhibits intricate detail and self-similarity at different scales. It is named after the Polish mathematician Wacław Sierpiński, who first described the pattern in 1915.

The Sierpinski Triangle is formed by recursively subdividing an equilateral triangle into smaller equilateral triangles. The process begins with a single large equilateral triangle. Then, at each iteration, we:

- Divide the initial triangle into four smaller equilateral triangles by connecting the midpoints of each side.
- Remove the central triangle, leaving three smaller equilateral triangles that form a larger equilateral triangle.
- Repeat steps 1 and 2 for each of the remaining smaller triangles, continuing indefinitely.

As the number of iterations approaches infinity, the resulting pattern becomes an increasingly intricate set of triangles, with the final Sierpinski Triangle having an infinite number of triangles and a total area of zero.

The Sierpinski Triangle is an example of a deterministic fractal, meaning that it can be generated through a specific set of rules. It has been studied extensively in mathematics and has applications in areas such as computer graphics, geometry, and the study of complex systems.

To recap, here are our three basic mathematical formulas:

First Transformation:

x_{n+1} = 0.5x_n

y_{n+1} = 0.5y_n

Second Transformation:

x_{n+1} = 0.5x_n + 0.5

y_{n+1} = 0.5y_n + 0.5

Third Transformation:

x_{n+1} = 0.5x_n + 1

y_{n+1} = 0.5y_n

Next, we take these formulas and express them in Python:

```
from random import choice
def trans_1(p):
x = p[0]
y = p[1]
x1 = 0.5 * x
y1 = 0.5 * y
return x1,y1
def trans_2(p):
x = p[0]
y = p[1]
x1 = 0.5 * x + 0.5
y1 = 0.5 * y + 0.5
return x1,y1
def trans_3(p):
x = p[0]
y = p[1]
x1 = 0.5 * x + 1
y1 = 0.5 * y
return x1,y1
transformations = [trans_1,trans_2,trans_3]
a1 = [0]
b1 = [0]
a,b = 0,0
for i in range(100):
trans = choice(transformations)
a,b = trans((a,b))
a1.append(a)
b1.append(b)
```

Then we use Matplotlib to visualize our triangle (we’ll have to import matplotlib first):

```
import matplotlib.pyplot as plt
%matplotlib inline
plt.rc('figure',figsize=(16,16))
plt.plot(a1,b1,'o')
plt.savefig('my_figure.png')
```

as we can see, at 100 points our triangle barely exists. Let’s try again at 1000:

```
for i in range(1000):
trans = choice(transformations)
a,b = trans((a,b))
a1.append(a)
b1.append(b)
```

Ok, our triangle is starting to take shape. Let’s skip ahead and try again at one million:

Ta-dah! There we have it, our Sierpinski’s Triangle. The first time I saw the triangle appear in Giles’ lesson, I thought it was a miracle.

Part 3: Alternative Ways to Generate the Sierpinski’s Triangle