Fractals
It’s November!
It is now the month of November, and the focus of this month’s theme is going to be “Visuals.” We’re going to try and have a diverse set of puzzles that focus on making something interactive and/or visually engaging. This week we are looking at Turtle Graphics and Fractals (though use anything you want to, if you have a different idea of how you’d like to make fractals).
Setup
Python comes with a cool built-in turtle graphics module called turtle
.
Python Downloads - I’d recommend choosing
Python 3
Turtle Graphics
Turtle graphics is an old method of generating graphics. It dates all the way back to the Logo programming language, first released in 1967.
You start with the “Turtle”, a fictional robot that drags a pen across paper (or in our case, the screen). It can be controlled by simple commands.
In Python:
import turtle
t = turtle.Turtle() # make a new turtle
The turtle has a position on the screen and has some orientation angle in degrees.
Here are the basic commands to control the turtle:
The basic commands:
Command | Description |
---|---|
t.forward(x) |
Move forward x pixels |
t.backward(x) |
Move backwards x pixels |
t.left(angle) |
Turn in place angle degrees to the left |
t.right(angle) |
Turn in place angle degrees to the right |
t.penup() |
Lift the pen from the “paper”. This allows moving without drawing a line |
t.pendown() |
Press the pen down onto the “paper”. Now the turtle will resume drawing lines |
There are several other commands, but I’ll let you read the docs.
Example Program:
import turtle
# Create our turtle
t = turtle.Turtle()
# Draw a cool pattern by drawing straight lines then making sharp
# angled turns
for i in range(9):
# Move 300 pixels forward
t.forward(300)
# Turn to the right 200 degrees
t.right(200)
# This keeps the turtle window open at the end of the program
turtle.mainloop()
This produces a design like this:
Puzzle
Try as many of these as you can manage:
- Draw a square
- Draw a hexagon
- Draw a triangle
- Let the user type a number of sides and generate a polygon with that many sides
- Draw a square spiral
- Draw a pattern with circles (hint: read the docs.)
- Try changing the pen color (again, read the docs!)
When you feel satisfied with the above, get creative and make your own design.
The Challenge: Fractals
There is class of fractals that are easily drawn with turtle graphics. Here’s an example, the von Koch Snowflake:
Step 0: Start with a triangle. Turtle commands:
- Move forwards some amount of pixels (start with a big number)
- Turn left 120°
- Repeat steps 1 and 2 twice more
Step 1: Replace every forward command with the following:
- Move forward 1/3 of the old distance
- Turn right 60°
- Move forward 1/3 of the old distance
- Turn left 120°
- Move forward 1/3 of the old distance
- Move Turn right 60°
- Move forward 1/3 of the old distance
Step 2: Do the same thing as Step 1, dividing the distance for each
segment by three each time
This can be repeated an infinite number of times (theoretically). Here’s the
snowflake after 5 iterations.
For the curious, L-Systems are useful in making this type of fractal.
If you liked this challenge, try making these other similar fractals:
Or try designing your own!