1

I am trying to create the Sierpinski carpet in python using turtle. This is my code so far:

from turtle import *

# Make a screen and a pen
pen = Pen()
screen = Screen()
pen.speed(0)
pen.color('orange')
pen.width(1.5)

def s (n, l):
    for i in range (4):
        s(n-1, l)
        pen.right(45); pen.forward(l); pen.right(45)
        s(n-1, l)
        pen.left(90); forward (l); pen.left(90)
        s(n-1, l)
        pen.right(45); pen.forward(l); pen.left(45)
        s(n-1, l)

However whenever I run it I get this message:

line 17, in s
    s(n-1, l)
  [Previous line repeated 990 more times]
RecursionError: maximum recursion depth exceeded

I tried using if i in range(4): but this does not work either, where am I going wrong?

Clonemyster
  • 45
  • 1
  • 6
  • Is your question "I don't understand the term recursion depth/want to know, how to increase the maximum recursion depth" or "How can I improve my algorithm to prevent this"? For the first question, [have a look here](https://stackoverflow.com/questions/3323001/what-is-the-maximum-recursion-depth-in-python-and-how-to-increase-it#3323013) – Mr. T Dec 15 '17 at 23:28
  • how do you start it ? `s(100, 10)` ? then try first `s(3, 10)` – furas Dec 15 '17 at 23:28
  • I forgot "stop condition". It never stop executing next `s(n-1, l)` so it repeats it even for `n < 0` – furas Dec 15 '17 at 23:31
  • I guess How can I improve my algorithm to prevent this, when run its meant to create a [sierpinski carpet](http://paulbourke.net/fractals/carpet/ifs10000000.gif) – Clonemyster Dec 15 '17 at 23:35
  • what is variable `d` ? you use it in `forward(d)` but you don't assign value first. – furas Dec 15 '17 at 23:35
  • 1
    first you have to add `"stop condition"` - you can't run next `s(n-1, l)` again and again. Because you get `s(-1, l)` and late `s(-10, l)` and `s(-100, l)` , etc. – furas Dec 15 '17 at 23:37
  • oh shit sorry, didnt notice that, will change now, where do i put in stop condition? – Clonemyster Dec 15 '17 at 23:53
  • I would put `"stop condition"` at the beginning of function `s` – furas Dec 16 '17 at 00:13

1 Answers1

2

You need "stop condition" to stop recursion.

For example:

 if n == 0: # "stop condition"

    # draw or exit

 else: # recursions

    # execute some recursions

I don't know algorithm but I created carpet with this code.

I use trace(0) and update() only to draw it much faster.

#!/usr/bin/env python3

import turtle

# --- functions ---

def s(n, l):

    if n == 0: # stop conditions

        # draw filled rectangle

        turtle.color('black')
        turtle.begin_fill()
        for _ in range (4):
            turtle.forward(l)
            turtle.left(90)
        turtle.end_fill()

    else: # recursion

        # around center point create 8 smalles rectangles.
        # create two rectangles on every side 
        # so you have to repeat it four times

        for _ in range(4):
            # first rectangle
            s(n-1, l/3)    
            turtle.forward(l/3)

            # second rectangle
            s(n-1, l/3)    
            turtle.forward(l/3)

            # go to next corner
            turtle.forward(l/3)
            turtle.left(90)

        # update screen
        turtle.update()

# --- main ---    

# stop updating screen (to make it faster)
turtle.tracer(0) 

# start
s(4, 400)

# event loop
turtle.done()

enter image description here

Wikipedia: Sierpinski carpet

furas
  • 134,197
  • 12
  • 106
  • 148