""" CSCI 150 Spring 2020 Lab 9 Name: Section: Creativity: """ import turtle as t def gcd(a, b): pass # sample calls for testing # gcd(36, 81) # gcd(13, 6) def stairs(length, levels): pass # sample calls for testing # stairs_demo(300, 1) # stairs_demo() def sierpinski(length, iterations): pass # sample calls for testing # sierpinski_demo(300, 1) # sierpinski_demo() def recursive_H(length, level): pass # sample calls for testing # recursive_H_demo(200, 1) # recursive_H_demo() # When all is complete, create screenshot using # drawing_demo() # Testing code is provided below that calls the recursive functions above def stairs_demo(length=256, levels=5): """ Moves the turtle to the top left of the screen and draws a staircase of squares. Calls turtle.done() at the end of drawing Args: length: side length of top square in pixels levels: number of boxes to draw Returns: None """ # pick up the pen and move the turtle so it starts at the left edge of the canvas t.penup() t.goto(-t.window_width()/2 + 20, t.window_height()/2 - 20) t.pendown() # draw the stairs by calling recursive function stairs(length, levels) # finished t.done() def sierpinski_demo(length=300, iterations=5): """ Starts the turtle at the left edge of the drawing window and then calls sierpinski, with tracer off and ends with turtle.done() Args: length: base length of Sierpinski triangle in pixels iterations: complexity level of Sierpinski triangle Returns: None """ if iterations > 3: # turn off animation (too slow for high iterations) t.tracer(False) # pick up the pen and move the turtle so it starts at the left edge of the canvas t.penup() t.goto(-t.window_width()/3 + 20,0) t.pendown() sierpinski(length, iterations) # finish t.update() # need t.update() if using t.tracer(False) t.done() def recursive_H_demo(length=150, level=3): """ Calls recursive_H, with tracer off and ends with turtle.done() Args: length: height of innermost H in pixels level: complexity level of recursive H Returns: None """ if level > 2: # turn off animation (too slow for high iterations) t.tracer(False) recursive_H(length, level) t.update() # need t.update() if using t.tracer(False) t.done() def drawing_demo(): """ Creates drawings of `stairs`, `sierpinski`, and `recursive_H` Args: None Returns: None """ t.tracer(False) # draw stairs in upper left of drawing window t.penup() t.goto(-t.window_width()/2 + 20, t.window_height()/2 - 20) t.pendown() stairs(150, 6) # draw sierpinski in upper right of drawing window t.penup() t.goto(0, t.window_height()/2 - 300) # t.goto(0,40) t.pendown() sierpinski(300, 5) # draw recursive H in lower half of drawing window t.penup() t.goto(0, -t.window_height()/4) t.pendown() recursive_H(150, 4) # finish t.update() t.done()