# Solution to Exam 2 Question B import cTurtle def drawSquareFractal(t, length, detail): if detail == 0: # Draw square. t.down() t.forward(length) t.left(90) t.forward(length) t.left(90) t.forward(length) t.left(90) t.forward(length) t.left(90) else: # Draw bottom-left subfractal. drawSquareFractal(t, length / 3.0, detail - 1) # Draw bottom-right subfractal. t.up() t.right(90) t.forward(length * 2.0 / 3.0) t.left(90) drawSquareFractal(t, length / 3.0, detail - 1) # Draw top-right subfractal. t.up() t.forward(length * 2.0 / 3.0) drawSquareFractal(t, length / 3.0, detail - 1) # Draw top-left subfractal. t.up() t.left(90) t.forward(length * 2.0 / 3.0) t.right(90) drawSquareFractal(t, length / 3.0, detail - 1) # Draw center subfractal. t.up() t.right(90) t.forward(length / 3.0) t.right(90) t.forward(length / 3.0) t.left(180) drawSquareFractal(t, length / 3.0, detail - 1) # Return to starting place. t.up() t.left(90) t.forward(length / 3.0) t.left(90) t.forward(length / 3.0) t.left(180) if __name__ == "__main__": t = cTurtle.Turtle() t.hideturtle() drawSquareFractal(t, 100.0, 2) raw_input("Press any key to exit.")