SkillAgentSearch skills...

Fractalartmaker

A module for creating fractal art in Python's turtle module.

Install / Use

/learn @asweigart/Fractalartmaker
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

fractalartmaker

A module for creating fractal art in Python's turtle module.

Fractals are recursive, self-similar shapes. The fractalartmaker module (abbreviated as fam) lets you create fractals in Python's built-in turtle module. This module is based on the "Fractal Art Maker" chapter of my free book, The Recursive Guide to Recursion.

Quickstart

To install, run pip install fractalartmaker in the command line terminal. Run python -m fractalartmaker on Windows or python3 -m fractalartmaker on macOS to run the demo and view nine pieces of fractal art made by Al Sweigart.

First, you must know a little bit of Python programming and Python's turtle module. RealPython.com has a turtle module tutorial.

You can view a demo fractal by running the following code from the interactive shell (aka the REPL):

>>> import fractalartmaker as fam
>>> fam.demo_four_corners()

NOTE: It's much easier to type if you import the fractalartmaker module with the name fam. We will do so throughout this tutorial.

This draws the Four Corners fractal in a new turtle window.

The main function we'll use in the fam module is fam.draw_fractal(). We'll need to pass it a drawing function, which is a function that takes a size argument and draws a simple shape. Here's the code for my_square_drawing_function() function that draws a square:

def my_square_drawing_function(size):
    # Move to the top-right corner before drawing:
    turtle.penup()
    turtle.forward(size // 2)
    turtle.left(90)
    turtle.forward(size // 2)
    turtle.left(180)
    turtle.pendown()

    # Draw a square with sides of length `size`:
    for i in range(4):  # Draw four lines.
        turtle.forward(size)
        turtle.right(90)

On its own, this function will just draw a square of a given size. (Note that this function isn't recursive; it just draws one square.) But the fam.draw_fractal() function can use functions like this to create fractals.

To make a fractal, call fam.draw_fractal() and pass it this drawing function, a starting size (let's go with 100), and a list of recursion specification dictionaries. By recursion specification dictionary, I mean a list of dictionaries with (optional) keys 'size', 'x', 'y', and 'angle'. I'll explain what these keys do later, but try calling this:

fam.draw_fractal(my_square_drawing_function, 100, [{'size': 0.96, 'y': 0.5, 'angle': 11}])
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/my_square1.webp" style="max-width: 50%">

This draws a fractal similar to the fam.demo_horn() fractal. The 100 is the initial size for the first square to draw. The list of recursion specification dictionaries [{'size': 0.96, 'y': 0.5, 'angle': 11}] has one dictionary in it. This means for each shape drawn with the drawing function, we will draw one more recursive shape. The new square is drawn at 96% the original size (because the 'size' key is 0.96), located 50% of the size above the square (because the 'y' key is 0.5), after rotating it counterclockwise by 11 degrees (because the 'angle' is set to 11).

By default, fam.draw_fractal() only goes 8 recursive levels deep. You can change this by passing a max_depth argument.

fam.draw_fractal(my_square_drawing_function, 100, [{'size': 0.96, 'y': 0.5, 'angle': 11}], max_depth=50)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/my_square2.webp" style="max-width: 50%">

Let's make each square draw two squares by putting a second recursion specification dictionary in the list. Because each square will draw two squares (and both of those two squares will draw two squares each, and so on and so on), be sure to set max_depth to something small like it's default 8. Otherwise, the exponentially large amount of drawing will slow your program down to a crawl.

Let's make the second recursion specification dictionary the same as the first, but it's 'angle' key is -11 instead of 11. This will make it veer off clockwise by 11 degrees instead of the normal counterclockwise:

fam.draw_fractal(my_square_drawing_function, 100, [{'size': 0.96, 'y': 0.5, 'angle': 11}, {'size': 0.96, 'y': 0.5, 'angle': -11}], max_depth=8)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/my_square3.webp" style="max-width: 50%">

As you can see, it doesn't take much to fill up the window with too many shapes. The key to making aesthetically pleasing fractals is to take a light touch and spend a lot of time experimenting. For example, we could pass a list of three recursion specification dictionaries to draw squares in three of the corners of the parent square:

fam.draw_fractal(my_square_drawing_function, 350,
    [{'size': 0.5, 'x': -0.5, 'y': 0.5},
     {'size': 0.5, 'x': 0.5, 'y': 0.5},
     {'size': 0.5, 'x': -0.5, 'y': -0.5}], max_depth=4)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/my_square4.webp" style="max-width: 50%">

If we increase the max_depth to 10, we can see a new pattern emerge:

fam.draw_fractal(my_square_drawing_function, 350,
    [{'size': 0.5, 'x': -0.5, 'y': 0.5},
     {'size': 0.5, 'x': 0.5, 'y': 0.5},
     {'size': 0.5, 'x': -0.5, 'y': -0.5}], max_depth=10)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/my_square5.webp" style="max-width: 50%">

The fractalartmaker module comes with a fam.square and fam.triangle drawing functions that you can play with. They will accept a colors keyword argument such as colors=(('black', 'white'), ('black', 'gray')). Also take a look at the code for the demo functions inside the __init__.py file for more ideas. Check out the rest of this documentation for advanced tips. Good luck!

NOTE: Calling fam.draw_fractal() automatically calls turtle.reset() to clear the window and move the turtle cursor back to 0, 0. If you don't want this behavior, pass reset=False to fam.draw_fractal()

NOTE: To free you from having to import the turtle module yourself, you can call most turtle functions from the fam module: fam.reset(), fam.update(), and so on.

Gallery of Demo Fractals

def demo_four_corners(size=350, max_depth=5, **kwargs):
    # Four Corners:
    if 'colors' not in kwargs:
        kwargs['colors'] = (('black', 'white'), ('black', 'gray'))
    draw_fractal(square, size,
        [{'size': 0.5, 'x': -0.5, 'y': 0.5},
         {'size': 0.5, 'x': 0.5, 'y': 0.5},
         {'size': 0.5, 'x': -0.5, 'y': -0.5},
         {'size': 0.5, 'x': 0.5, 'y': -0.5}], max_depth=max_depth, **kwargs)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/four-corners.webp" style="max-width: 50%">
def demo_spiral_squares(size=600, max_depth=50, **kwargs):
    # Spiral Squares:
    if 'colors' not in kwargs:
        kwargs['colors'] = (('black', 'white'), ('black', 'gray'))
    draw_fractal(square, size, [{'size': 0.95,
        'angle': 7}], max_depth=max_depth, **kwargs)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/spiral-squares.webp" style="max-width: 50%">
def demo_double_spiral_squares(size=600, **kwargs):
    # Double Spiral Squares:
    if 'colors' not in kwargs:
        kwargs['colors'] = (('black', 'white'), ('black', 'gray'))
    draw_fractal(square, 600,
        [{'size': 0.8, 'y': 0.1, 'angle': -10},
         {'size': 0.8, 'y': -0.1, 'angle': 10}], **kwargs)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/double-spiral-squares.webp" style="max-width: 50%">
def demo_triangle_spiral(size=20, max_depth=80, **kwargs):
    # Triangle Spiral:
    draw_fractal(triangle, size,
        [{'size': 1.05, 'angle': 7}], max_depth=max_depth, **kwargs)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/triangle-spiral.webp" style="max-width: 50%">
def demo_glider(size=600, **kwargs):
    # Conway's Game of Life Glider:
    if 'colors' not in kwargs:
        kwargs['colors'] = (('black', 'white'), ('black', 'gray'))
    third = 1 / 3
    draw_fractal(square, 600,
        [{'size': third, 'y': third},
         {'size': third, 'x': third},
         {'size': third, 'x': third, 'y': -third},
         {'size': third, 'y': -third},
         {'size': third, 'x': -third, 'y': -third}], **kwargs)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/glider.webp" style="max-width: 50%">
def demo_sierpinski_triangle(size=600, **kwargs):
    # Sierpinski Triangle:
    toMid = math.sqrt(3) / 6
    draw_fractal(triangle, 600,
        [{'size': 0.5, 'y': toMid, 'angle': 0},
         {'size': 0.5, 'y': toMid, 'angle': 120},
         {'size': 0.5, 'y': toMid, 'angle': 240}], **kwargs)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/sierpinski-triangle.webp" style="max-width: 50%">
def demo_wave(size=280, **kwargs):
    # Wave:
    draw_fractal(triangle, size,
        [{'size': 0.5, 'x': -0.5, 'y': 0.5},
         {'size': 0.3, 'x': 0.5, 'y': 0.5},
         {'size': 0.5, 'y': -0.7, 'angle': 15}], **kwargs)
<img src="https://raw.githubusercontent.com/asweigart/fractalartmaker/main/wave.webp" style="max-width: 50%">
def demo_horn(size=100, max_depth=100, **kwargs):
    # Horn:
    if 'colors' not in kwargs:
        kwargs['colors'] = (('black', 'white'), ('black', 'gray'))
    draw_fractal(square, size,
        [{'size': 0.96, 'y': 0.5, 'angle': 11}], max_depth=max_depth, **kwargs)

<img src="https://raw.githubusercontent.com/asweigart/fractal

View on GitHub
GitHub Stars14
CategoryDevelopment
Updated23d ago
Forks1

Languages

Python

Security Score

75/100

Audited on Mar 7, 2026

No findings