# Using Python to Make Art with Math

### Ryan Palo Feb 2 ć»10 min read

*Originally published on Simple Programmer.*

Math can be intimidating.

Depending on the teacher and how it is taught, it can be an infuriating combination of inscrutable and boring.

But, thereās a beauty to mathāa symmetry to the intelligence and logic behind numbers. I love math, and I want other people to love it too.

One neat way to make math more approachable and show its beauty visually is to combine it with something called āgenerative art.ā Generative art is where you create a few, usually simple rules which are often math- or geometry-based, and then you tell a computer to process these rules.

Since computers are great for processing instructions quickly, and on a much greater scale than a human could, the designs that are created are often more complex and interesting than you might expect from such simple rules.

This example shows a bunch of floating particles that move with a mesmerizingly natural motion. They float around, link up with other particles, and change directions all on their own. Itās a variation on a āflocking algorithm,ā and the amazing thing is that most of this natural motion comes from simply having each particle follow the rules of ādonāt run into anybody,ā āstay with the flock,ā and āgo in roughly the same direction as those near you.ā

Python is a great option for creating these generative art projects; it is used by data scientists, mathematicians, and engineers (among many others) as an open source option for processing numerical calculations and generating visualizations.

It is also extremely easy to read and write clear code, which makes it an ideal language for outlining the simple rules needed to create this generative art.

One of the simplest mathematical constructs you can create with these rules is an integer sequence, which is an ordered list of integers (whole numbers, including: positive, negative, and zero). Usually, the relationship between these integers is spelled out in some sort of logical way with a set of rules in order to help someone figure out what the next number in the pattern is.

In this article, we are going to take the mathematics of integer sequences, supercharge them with the power of programming in Python, and use them to create some interesting and beautiful generative art.

It is a fun exercise, and the calculations and code samples are simple enough that they should be engaging for programmers, mathematicians, and artists alike.

This is not a beginnerās Python article, though, so I will assume you have some familiarity with Pythonās syntaxāor, at least, a willingness to pick it up as you go alongāas well as how to run Python programs. If youāre not sure how to run them, and youād like to write code in an application with a big giant āPlayā button, the Mu text editor is great for beginners.

The particular sequence I want to talk about this time is the **RecamĆ”n sequence**. The rules are deceptively simple, but when the numbers are given visual or auditory shape, the results can be interesting and even a little spooky.

## The RecamĆ”n Sequence

Here are the rules:

- Start at zero.
- Every step you take will be 1 bigger than the last step you took.
- If itās possible to step backward (negatively), do so. Otherwise step forward.
- Backward steps are only allowed if the resulting location is positive (greater than zero) and if weāve never been to that number before.

Letās do the first few as examples.

We start at zero.

- 0

The next step size will be 1. Stepping backward would put us at -1, which is not allowed, so weāll step forward.

- 0 -> 1

The next step size is 2. Stepping backward would put us at -1. Thatās still not allowed, so forward we must go.

- 0 -> 1 -> 3

The next step size is 3. Stepping backward puts us at 0. Since weāve already been to 0 (our first starting point), this is not a valid move. I promise the sequence gets interesting soon, but for now, we step forward.

- 0 -> 1 -> 3 -> 6

The next step size is 4. Stepping backward lands us at 2. Since 2 is positive, and we havenāt seen it yet, we can take our first legitimate backward step!

- 0 -> 1 -> 3 -> 6 -> 2

Hopefully youāre beginning to see how the rules work. Now, this is kind of interesting to think about, but Iām not sure I would call a list of five numbers beautiful. Thatās where weāll lean a little bit harder on art and code. Luckily Python provides both of these to us in a fun and adorable module in its Standard Library: **Turtle**.

## Introducing Turtle Graphics

Turtle Graphics was originally a key feature of the Logo programming language.

Itās a relatively simple Domain-Specific Language (DSL) where there is an avatarātraditionally shaped like a little turtleāon the screen, and you give it instructions on where to go: forward, left, or right. As it moves, it draws a line wherever it goes with its tail, although you can tell it to pick its tail up or put it down as necessary. It can even jump positions and change colors!

Python also includes a āturtleā library packaged along with it in its standard library. Letās take a look at a sample program to see how it works. Create a file called āexample1.pyā with the following contents.

```
import turtle
window = turtle.Screen()
joe = turtle.Turtle()
joe.forward(50)
joe.left(90)
joe.forward(100)
turtle.done()
```

Here are the important bits:

- On line 1, we import the āturtleā module. This will have many of the functions that weāre going to use, and we couldnāt use them unless we imported the module first. Weāll use these functions by writing āturtle.function_name,ā as youāll see.
- On line 3, we create a Window. This is the window that will pop up when you run your code. It holds all of the stuff weāre going to be drawing.
- On line 4, we actually create our Turtle. We are going to call him ājoeā and store him in a variable so that we can give him commands later.
- Lines 6-8 are us giving ājoeā commands. You see the ādotā syntax that should be getting pretty familiar.
- Line 10 just keeps the window from closing when ājoeā is done with his drawing.

If you run your code, you should see something like the following:

*Side note: If you want the real, old-fashioned Logo experience, you can add ājoe.shape(āturtleā)ā to your code, right under the line where you define ājoe.ā Isnāt he cute?*

Okay, now that youāve seen what āturtleā is all about, letās get back to the sequence we were working on.

## Coding the Sequence

Like anything good, weāre definitely not going to get a perfect result on the first go. Weāll need to do some iteration. Iāll take you through three passes at this art project, and each one will get a little more complicated and a little more visually interesting. After that, I have some potential ideas for further iteration that you can try if this gets your creative spark going. Letās get to it!

### First Try

The code that we write will be very similar to the English we would use to describe the steps for the sequence. Remember the two rules: Go backward when possible, otherwise go forward, and increase the step size by one after each step.

Create a new file named ārecaman1.py.ā Weāll start with those basic rules and then figure out how to make it actually work. Iām naming our new turtle Euler, after some guy.

```
import turtle
window = turtle.Screen()
euler = turtle.Turtle() # A good mathy name for our turtle
euler.shape("turtle")
current = 0 # Here's how we know where we are
seen = set() # Here's where we'll keep track of where we've been
# Step increases by 1 each time
for step_size in range(1, 100):
backwards = current - step_size
# Step backwards if its positive and we've never been there before
if backwards > 0 and backwards not in seen:
euler.backward(step_size)
current = backwards
seen.add(current)
# Otherwise, go forwards
else:
euler.forward(step_size)
current += step_size
seen.add(current)
turtle.done()
```

However, when we run it, it doesnāt look very good. In fact, it looks like maybe somebody gave Euler too much coffee.

### Second Try

So, that was pretty reasonable. The code seems to read just like we might explain it to someone, which is good.

Iām afraid that the linear motion is just a little boring, though. This is when we want to put our artistsā caps on (do artists wear caps?) and figure out a little more creative way to get Euler from point A to point B.

Luckily turtles donāt have to move in straight lines! They can also move in arcs and circles. Letās have him bounce from spot to spot on the number line! To make a turtle draw a circle or partial arc, weāll use the ācircleā command, which causes the turtle to follow a circle where the imaginary center is āradiusā units to the turtleās left.

That means weāll have to orient our turtle before drawing, depending on whether heās going forward or backward, using the āsetheadingā command.

Remember that you can find all the turtle commands in the official documentation, just in case youāre curious.

```
import turtle
window = turtle.Screen()
euler = turtle.Turtle() # A good mathy name for our turtle
euler.shape("turtle")
current = 0 # Here's how we know where we are
seen = set() # Here's where we'll keep track of where we've been
# Step increases by 1 each time
for step_size in range(1, 100):
backwards = current - step_size
# Step backwards if its positive and we've never been there before
if backwards > 0 and backwards not in seen:
euler.setheading(90) # 90 degrees is pointing straight up
euler.circle(step_size/2, 180) # 180 degrees means "draw a semicircle"
current = backwards
seen.add(current)
# Otherwise, go forwards
else:
euler.setheading(270) # 270 degrees is straight down
euler.circle(step_size/2, 180)
current += step_size
seen.add(current)
turtle.done()
```

Thatās neat, but for the first little while, it seems like he just wiggles around in one place and the lines are very close together. Also, heās not going to be using the whole left half of the screen!

Letās do one more iteration together, where we make it even nicer to look at.

### Third Try

The goals for this iteration are to make the picture bigger, and to give him more room to work.

```
import turtle
window = turtle.Screen()
window.setup(width=800, height=600, startx=10, starty=0.5)
euler = turtle.Turtle() # A good mathy name for our turtle
euler.shape("turtle")
scale = 5 # This isn't a turtle module setting. This is just for us.
# Move the little buddy over to the left side to give him more room to work
euler.penup()
euler.setpos(-390, 0)
euler.pendown()
current = 0 # Here's how we know where we are
seen = set() # Here's where we'll keep track of where we've been
# Step increases by 1 each time
for step_size in range(1, 100):
backwards = current - step_size
# Step backwards if its positive and we've never been there before
if backwards > 0 and backwards not in seen:
euler.setheading(90) # 90 degrees is pointing straight up
# 180 degrees means "draw a semicircle"
euler.circle(scale * step_size/2, 180)
current = backwards
seen.add(current)
# Otherwise, go forwards
else:
euler.setheading(270) # 270 degrees is straight down
euler.circle(scale * step_size/2, 180)
current += step_size
seen.add(current)
turtle.done()
```

As you can see, weāve added a scaling factor which you can tune to whatever you think works best. I arrived at this value by trying a couple and picking my favorite. We also shifted him over so he starts at the left side of the screen. Since he can never go negative, we know he will only go right from wherever he starts.

### Further Tweaking

By now, I think you get the gist, and youāre hopefully starting to see the magic of integer sequences: out of a few simple rules (and with the help of a tireless reptile assistant), you can make some truly interesting and captivating results.

Youāve got all the tools you need to do something even cooler. Here are some ideas to get your creative juices flowing:

- What if you change the color of the lines? What if you change it continuously?
- What if you skew the drawing to an angle to take up more of the screen?
- What if you have multiple turtles drawing sequences in tandem?
- What if you vary the predictable sequence with a little bit of randomness?
- What if you incorporate user input (mouse clicks, key presses, etc.) to affect how the turtles behave?
- What if you make a turtle that draws but is driven by something really slow, like the cycle of the moon?
- Is there a way you could incorporate a turtle with an IoT project or a web application?

## Other Incarnations of the Sequence

If you found the RecamĆ”n sequence *particularly* fascinating, youāre not alone. There are a ton of different incarnations out there people have created, combining the lines with color, sound, and more. Here are a few of my favorite.

This slightly spooky version with sound:

This Numberphile video about the sequence is really interesting:

The Coding Train also did a couple videos on coding this up if you want a cool video walkthrough with an amazing teacher and donāt mind writing JavaScript:

In fact, P5.js (and its Java-based predecessor, Processing) are both great alternatives for making art and animations with code; they come with dedicated editors to help you do that, and they allow support for sound and other add-ins!

## Get Your Turtles in Gear

Hopefully this tutorial was enough to spark your interest in using code to generate art, and hopefully (if you were before), you are no longer too intimidated to look to mathematics as a source for your artistic ideas.

Geometry, never-ending constants, golden ratios, Fibonacci spirals, fractals, and number theory are all goldmines of awesome visual projects just waiting to be programmatically generated, and now you have all the tools you need to get your own set of turtles and start generating!

(open source and trusted by devs everywhere ā¤ļø)

This is quite intriguing! I hadn't heard of that sequence before. I'm excited to see what comes out of the PyCon arts festival this year, because I'm hoping it's cool stuff like this š

Great Post!

Thanks! Glad you liked it!