Lesson 40 of 51 · Fractals
Fractal Fern
Barnsley fern uses randomness to create a natural-looking plant.
Python
from turtle import *
import random
bgcolor("black")
speed(0)
tracer(1000)
hideturtle()
x, y = 0, 0
color("lime")
for i in range(5000):
r = random.random()
if r < 0.01:
x, y = 0, 0.16 * y
elif r < 0.86:
x, y = 0.85 * x + 0.04 * y, -0.04 * x + 0.85 * y + 1.6
elif r < 0.93:
x, y = 0.20 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6
else:
x, y = -0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44
penup()
goto(x * 50, y * 50 - 200)
pendown()
dot(2)
update()
done()Output

1
Randomness + Math
Each dot is placed by picking one of 4 transformations at random.