Albion College

CS 171

Introduction to Computer Science I & Lab

Fall 2021

Lab 13

Goals and Objectives

The main goal this lab is for you to get a more practice with recursion in the context of fractals.

Task

You will complete methods that create some simple fractal images.

Background

Fractals can lead to visually interesting artwork. I've used this process to create several artworks:

Other artists such as M.C. Escher and Robert Fathauer have used fractals in their artwork.

In the lab I am asking you to create images like the ones below as well as explore some options in existing code.

Sierpinski carpet tree fern

Introductions

Introduce yourself to your lab partner(s). Talk about your plans for winter break.

Download the lab

Download the source file Lab13.py. It contains the stubs of several methods. Save it directly to your folder with your other lab files and open it in Python.

Pressing a key will change the graphic displayed as follows:

c
circles
q
squares
k
Koch curve
s
Sierpinski carpet
t
tree
f
fern
a
YOUR artistic fractal

Explorations

See the comments before functions that encourage you to explore these fractals and gain a better understanding of fractals and recursion.

Code

# YOUR NAMES HERE! import turtle # translate def translate(pic, rx, ry): pic.penup() pic.forward(rx) pic.left(90) pic.forward(ry) pic.right(90) pic.pendown() return # draw a circle centered at current position def circle(pic, L): #pic.begin_fill() # translate to the lower left corner translate(pic,0,-L) pic.circle(L, steps=int(L)) #pic.end_fill() # translate back to the origin translate(pic,0,L) return # draw a square centered at current position def square(pic, L): #pic.begin_fill() # translate to the lower left corner translate(pic,-L,-L) pic.forward(2*L) pic.left(90) pic.forward(2*L) pic.left(90) pic.forward(2*L) pic.left(90) pic.forward(2*L) pic.left(90) #pic.end_fill() # translate back to the origin translate(pic,L,L) return # draw recursive squares def squares(pic, L): square(pic, L) if L < 25: return L2 = 0.4*L translate(pic, 0, L) squares(pic, L2) translate(pic, 0, -L)# move back translate(pic, L, 0) squares(pic, L2) translate(pic, -L, 0)# move back translate(pic, -L, 0) squares(pic, L2) translate(pic, L, 0)# move back translate(pic, 0, -L) squares(pic, L2) translate(pic, 0, L) # move back return # draw recursive squares def circles(pic, L): circle(pic, L) if L < 25: return L2 = 0.5*L translate(pic, 0, L) circles(pic, L2) translate(pic, 0, -L)# move back translate(pic, L, 0) circles(pic, L2) translate(pic, -L, 0)# move back translate(pic, -L, 0) circles(pic, L2) translate(pic, L, 0)# move back translate(pic, 0, -L) circles(pic, L2) translate(pic, 0, L) # move back return # draw Koch curve def kochDriver(pic,L): translate(pic, -L, 0) koch(pic, 2*L) translate(pic, -L, 0) return def koch(pic, L): if L < 10: pic.forward(L) return pic.pendown() pic.color('green') # drawing color - Tk color koch(pic, L/3) pic.left(60) koch(pic, L/3) pic.right(120) koch(pic, L/3) pic.left(60) koch(pic, L/3) return # draw Sierpinski carpet def sierpinski(pic, L): return # draw tree def tree(pic, L): return # draw fern def fern(pic, L): return # draw your own creative artistic fractal def artistic(pic, L): return # not much, if anything, to change here def main(): # Create a turtle object pic = turtle.Turtle() # set title of the drawing window pic.getscreen().title("CS 171 - Lab 13") # set size of the drawing window pic.getscreen().setup(width=800, height=800) # default starting display reset(pic) # Loop forever waiting for window to close turtle.mainloop() def reset(pic): #turtle.clearscreen() #turtle.showturtle() #pic.hideturtle() # hide the turtle # set a fast speed turtle.delay(0) turtle.speed(0) # Set colors - Can be changed later as needed # https://www.google.com/search?q=color+picker pic.getscreen().bgcolor("#94d4e0") # Image background color, hex RGB value # https://www.tcl.tk/man/tcl8.4/TkCmd/colors.html pic.color('green', "red") # drawing and fill colors pic.color('green') # drawing color - Tk color # create callback function bindings turtle.onkey((lambda:reset(pic)), "r") turtle.onkey((lambda:squares(pic, 200)), "q") turtle.onkey((lambda:circles(pic, 200)), "c") turtle.onkey((lambda:sierpinski(pic, 100)), "s") turtle.onkey((lambda:kochDriver(pic, 300)), "k") turtle.onkey((lambda:tree(pic, 100)), "t") turtle.onkey((lambda:fern(pic, 100)), "f") turtle.onkey((lambda:artistic(pic, 100)), "a") turtle.listen() main()

When you are finished, email your lab files to your lab partner(s) and instructor.