Log in to save your progress.
<< Previous
Home
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Reference

Click on a function to read more.
Turtle
Function
Short description
back()
Moves the turtle backward
begin_fill()
Starts filling with color
circle()
Draws a circle with optional radius
color()
Determines the color the turtle draws with
end_fill()
Ends filling with color
forward()
Moves the turtle forward
goto()
Moves the turtle to a given coordinate
hideturtle()
Hides the turtle
left()
Turns the turtle left a number of degrees
pendown()
Puts the pen down on the drawing area.
penup()
Lifts the pen from the drawing area
right()
Turns the turtle right a number of degrees
shape()
Changes the drawing shape
Screen().setup()
Creates a drawing area with a given size
speed()
Sets the turtle's drawing speed

Recursion and fractals (advanced)

Here you will learn

Recursion in programming means functions that call themselves. With this technique, we can draw exciting geometric patterns, so-called fractals.

An important detail when functions call themselves is that there is a stop condition. Otherwise, the function would call itself indefinitely.

A different star

Earlier in the course, we drew a five-pointed star. Now that we have learned both repetitions and functions, we can simplify the code.

Example

A five-pointed star in a function.

def star(side):
    for i in range(5):
        forward(side)
        right(144)

star(250)
      

Now we will turn the function star into a recursive function that calls itself. In each point, a new smaller star will be drawn, and in the smaller stars, a small star will in turn be in each point. And so on to a depth we specify.

To be able to write a stop condition, we will use if n == 0: return which means "if n is equal to 0, exit the function". In each recursive step, n will decrease by one. Therefore, the function will stop calling itself when n is equal to 0. You can read more about the if statement here.

Study the example below carefully. Try to understand exactly what is happening. Recursion is not easy to understand at first!

Example

A recursive star. Try different depths!

def star(side, n):
    if n == 0:
        return
    for i in range(5):
        forward(side)
        star(side/3, n-1)
        right(144)

star(150, 3)
      

In the example above, the line star(side/3, n-1) is the important part. It means that a new star is drawn with a side one-third as large and with a depth that is one lower. With star(150, 3), the side is initially 150 and the depth is 3. After a side is drawn, star(50, 2) will be called, that is, draw a star with side 50 and depth 2. In the next step, the depth is 1 and then a simple star is drawn. At depth 0, nothing is drawn anymore because of the stop condition.

Login to save your progress and your code.

Modify the code

The code should draw a star with seven points. Such a star has seven sides (instead of five) and the angle in a point is 720/7 degrees (instead of 144 degrees).

Koch's snowflake

Koch's snowflake is one of the first described fractals and was created by the Swedish mathematician Helge von Koch in 1904. Now we will see how we can draw it with the help of recursion. The algorithm below describes how the snowflake is drawn.

  1. Start with a straight line.
  2. Divide the line into three equal parts.
  3. Replace the middle part with two parts that form a peak upwards.
  4. Repeat for all lines.

Illustration

Illustration of Koch's algorithm for one repetition.

# Click on test to see the illustration.

  

To draw the fractal, we can start by drawing the basic pattern in a function. Then we can replace each line with a recursive call.

Koch's snowflake - one repetition

def snowflake(side):
    forward(side)
    left(60) #the angle is 60 degrees in an equilateral triangle
    forward(side)
    right(120)
    forward(side)
    left(60)
    forward(side)
  

Now we will replace forward(side) with snowflake(side/3,n-1). Instead of drawing a line, we make a new pattern where the line would have been. The new pattern is only one-third as large. The stop condition when n = 1 is simply to draw the line. This is the base case as you can see in step 1 of the algorithm.

Koch's snowflake

Finished example of Koch's snowflake. Click on test and try different values for n.

def snowflake(side,n):
    if n == 1: #stop condition
        forward(side)
        return

    snowflake(side/3,n-1) #instead of forward(100)
    left(60)
    snowflake(side/3,n-1) #instead of forward(100)
    right(120)
    snowflake(side/3,n-1) #instead of forward(100)
    left(60) 
    snowflake(side/3,n-1) #instead of forward(100)
  

Login to save your progress and your code.

Modify the code

Try to use the function snowflake to draw a whole snowflake. For example, by drawing the shape of a square where each side uses snowflake.

Status
You have finished all tasks in this section!
🎉