# [Python] Sierpiński Triangles

In real life a right-angled triangle is very unlikely to have a square on its hypotenuse.

– H. F. Ellis Photo by Will Mu from Pexels

When talking about fascinating geometric patterns, Sierpiński Triangles indeed stand among the most fascinating ones. Named after the Polish mathematician Waclaw Sierpiński, who first coined the term in 1915, they are a fractal pattern that is created by recursively removing triangles from a larger one. The Sierpiński triangle is an example of a fractal, which is a pattern that repeats itself at different scales.

To create a Sierpiński Triangle, one may start with a large equilateral triangle. This triangle is then divided into four smaller equilateral triangles by connecting the midpoints of each side. The middle triangle is then removed, leaving three smaller triangles. The same process is then applied to each of the remaining triangles, with the middle triangle being removed each time. This process goes on indefinitely, resulting in a pattern that becomes increasingly intricate as more triangles are removed.

The recursive nature of the process is the reason as to why these triangles look the way they do. Each time a triangle is removed, the resulting pattern is a smaller version of the original pattern. This creates a self-similar pattern that repeats on different scales.

The Sierpiński triangle is also a popular example of a self-replicating pattern, meaning that it can be used to create copies of itself. This property has been used in the development of computer graphics and digital imaging. There are many interesting facts about the Sierpiński triangles. For example, the pattern can be created in many different ways, including using computer algorithms, physical models, or even by folding paper.

Here we will have a look at a cool algorithm which ends up creating this pattern. You can find the python codes here:

``````
# Sierpiński Triangles
# @CodedAbacus

# libraries
import random # for random edge selection

# The edges
edges = [[0, 0], [4, 8], [8, 0]]

# The initial random point
r = [3, 1]

# function to find the middle points
def middle(p1, p2):
return [(p1 + p2) / 2, (p1 + p2) / 2]

print(middle([0,0], [2,2]))

# initialize storage lists for x- and y coordinates
x = [0, 4, 8, 3]
y = [0, 8, 0, 1]

# the algorithm
# the total number of points
n = 25000

for _ in range(n):
# Step 1: Choose an edge at random
edge = random.choice(edges)

# Step 2: find the middle point
r = middle(r, edge)

# update the x- and y storage lists
x.append(r)
y.append(r)

# plot the results
import matplotlib.pyplot as plt

# plot the results
plt.scatter(x, y)

``````

The result of running the following algorithm looks like this: The result of executing the above mentioned code, which shows an example of Sierpiński Triangles.

Fascinating, isn’t it? You will find the explanation of the code and further information in the following Youtube video: