# Sierpinski Triangle, drawn by a Turtle

By Monty, 18th April 2015

This is a classic fractal drawn with a recursion algorithm and Turtle graphics. The Sierpinski Triangle’s sides are bisected and the triangle they form is removed. The procedure is then applied to the 3 remaining triangles, and to them recursively or until the Universe ends.

Note that the use of recursion allows the code to reflect the structure of the picture to be drawn. Let S be the initial triangle (which is invisible at first). When we enter the function S, we reset the scale by one half, because the internal triangles (that will be created next) have half the perimeter of the original one. Then we create the bottom left triangle with S(l,x,y), then the bottom right, then the top one. Then we do that to each of those triangles too – bottom left, bottom right, top – until they’re small enough, at which point we actually draw the individual triangles and fill them in with black.

I’d like to tell you that I wrote this amazingly simple program in a few minutes. It took me a couple of hours! Simplicity is hard work.

# Sierpinski Triangle
# Authour: Alan Richmond, Python3.codes
from turtle import *
size=800
min=64
pf=0.8660254 # Pythagoras factor: sqrt(3)/2
def S(l,x,y):
if l>min: # Not done yet?
l=l/2 # scale down by 2
S(l,x,y) # bottom left triangle
S(l,x+l,y) # bottom right triangle
S(l,x+l/2,y+l*pf) # top triangle
else: # Done recursing
goto(x,y); pendown() # start at (x,y)
begin_fill() # prepare to fill triangle
forward(l); left(120) # triangle base
forward(l); left(120) # triangle right
forward(l) # triangle left
end_fill()
setheading(0) # face East
penup(); goto(x,y) # finish at (x,y)
penup()
speed('fastest')
S(size,-size/2,-size*pf/2.0)
done()

### Further Information

**Wikipedia**:

The **Sierpinski triangle** (also with the original orthography *Sierpiński*), also called the **Sierpinski gasket** or the **Sierpinski Sieve**, is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles. Originally constructed as a curve, this is one of the basic examples of self-similar sets, i.e., it is a mathematically generated pattern that can be reproducible at any magnification or reduction. It is named after the Polish mathematician Wacław Sierpiński, but appeared as a decorative pattern many centuries prior to the work of Sierpiński.

### Like this:

Like Loading...

*Related*

## What do you think?