Python and Turtle Difficulty Level 7,loop,math,recursion Sierpinski Pentagon with Python Turtle

Sierpinski Pentagon with Python Turtle

Use recursion to draw the following Sierpinski Pentagon.

Sierpinski Pentagon with Recursion Depth 1
Sierpinski Pentagon with Recursion Depth 2
Sierpinski Pentagon with Recursion Depth 5

This project is closed related to Pentaflake Fractal. The difference is the in Pentaflake we also draw an upside down Pentaflake recursively.

What’s next?
Pentaflake Fractal
Colored Sierpinski Pentagon

3 thoughts on “Sierpinski Pentagon with Python Turtle”

  1. Why could I never get these to stick together. they always have a gap.

    import turtle
    import random
    import math
    turtle.speed(0)
    turtle.tracer(0,0)
    turtle.hideturtle()
    turtle.up()
    def draw_pentagon(x,y,size,angle):
    turtle.up()
    turtle.goto(x,y)
    turtle.left(90)
    turtle.fd(math.cos(36)*size)
    turtle.left(90)
    turtle.fd(math.sin(36)*size)
    turtle.seth(angle)
    turtle.down()
    for i in range(5):
    turtle.fd(size)
    turtle.left(72)

    def draw_pentagon_recursion(x,y,size,angle,n):
    if n==0:
    return
    for angle in range(0,360,72):
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(angle)
    draw_pentagon(x,y,size,angle)
    cx=turtle.xcor()
    cy=turtle.ycor()
    draw_pentagon_recursion(x,y,size,angle,n-1)
    angle=0
    for i in range(5):
    draw_pentagon_recursion(0,0,100,angle,5)
    angle+=252
    turtle.update()

    1. Your draw_pentagon() function has a couple of errors:

      math.cos(36) needs to be changed to math.cos(math.radians(36))

      Also the side length of the pentagon is no longer ‘size’, it should be 2*math.sin(math.radians(36))*size (some geometry work needed)

      So this is the correct draw_pentagon() function (that should align perfectly):
      def draw_pentagon(x,y,size,angle):
      turtle.up()
      turtle.goto(x,y)
      turtle.dot()
      turtle.seth(angle)
      turtle.left(90)
      turtle.fd(math.cos(math.radians(36))*size)
      turtle.dot(‘blue’)
      turtle.left(90)
      turtle.fd(math.sin(math.radians(36))*size)
      turtle.dot(‘red’)
      turtle.seth(angle)
      turtle.down()
      for i in range(5):
      turtle.fd(2*math.sin(math.radians(36))*size)
      turtle.left(72)

      You also have some problems in the draw_pentagon_recursion(). Try to fix it!

  2. this is what I get. Can someone help?

    import turtle
    import colorsys
    import math
    turtle.speed(0)
    turtle.hideturtle()
    turtle.tracer(0,0)
    def draw_circle(x,y,size,color):
    turtle.up()
    turtle.seth(0)
    turtle.goto(x+math.sin(36)*size/2,y+math.sin(36)*size/2)
    turtle.down()
    turtle.fillcolor(color)
    turtle.begin_fill()
    for i in range(5):
    turtle.fd(size)
    turtle.left(72)
    turtle.end_fill()

    def draw_circle_snowflake(x,y,size,color,angle,n):
    if n==0:
    return
    draw_circle(x,y,size,color)
    h=0
    for angle in range(0,360,72):
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(angle+18)
    turtle.fd(2*size)
    cx=turtle.xcor()
    cy=turtle.ycor()
    color=colorsys.hsv_to_rgb(h,1,1)
    draw_circle_snowflake(cx,cy,size/2.3,color,5*angle,n-1)

    for angle in range(360):
    draw_circle_snowflake(0,0,100,’orange’,angle,4)
    angle+=72
    turtle.update()

Comments are closed.

Related Post