How to draw a sierpinski carpet in python using turtle

Clonemyster

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?

furas

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make turtle draw quicker?

Python Turtle: Draw concentric circles using circle() method

Sierpinski triangle recursion using turtle graphics

How to make items draw at the same time in python using turtle?

How to draw a semicircle in Python turtle only

How to draw a tiled triangle with python turtle

Using python turtle to draw a polygon with n number of sides

How to draw a circle using turtle in python?

How to draw Circle using Turtle in Python 3

How can I optimize this Sierpinski carpet i made using recursion?

Draw board with color blocks and numbers in Python turtle

How to draw some graph without using data and function? (turtle analog)

How to make fireworks in Python using turtle

Sierpinski Carpet with recursion in C using SVG

Draw Co-ordinates using Turtle

Need help making Sierpinski triangle with turtle

How to draw sine in Python turtle?

Beginner: Using a void function to calculate carpet cost in Python?

How do you draw an ellipse/oval in turtle graphics (python)?

Spirograph Using Turtle in Python

How do I draw recursive Sierpiński arrowhead curve using python turtle graphics on Visual Studio Code

Using loops in Python Turtle

Using python turtle GUI

How to draw a heptagon using Python Turtle with the point at the top?

How to draw N -times heptagon shapes not on top of each others, using Python Turtle?

How to draw "square" dots instead of "round" dots with python 3 turtle?

Python Turtle Will Not Draw Circles

How to draw circles around a pentagon using turtle?

How to draw a polygon given a number of points with Python Turtle?