Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 15 - Recursion - Programming Exercises - Page 527: 15.30

Answer

code

Work Step by Step

# 15.30 (Turtle: H-tree fractal) Rewrite the H-tree fractal in Exercise 15.29 using Turtle, # as shown in Figure 15.16. Your program should prompt the user to enter the # order and display the corresponding fractal for the order. import turtle # Draw H shapes with the specified order def displayHShape(order, center, length): if order >= 0: p1 = [center[0] - length / 2, center[1] + length / 2] p2 = [center[0] - length / 2, center[1] - length / 2] p3 = [center[0] + length / 2, center[1] + length / 2] p4 = [center[0] + length / 2, center[1] - length / 2] # Draw an H shape drawLine(center[0] - length / 2, center[1], center[0] + length / 2, center[1]) drawLine(p1[0], p1[1], p2[0], p2[1]) drawLine(p3[0], p3[1], p4[0], p4[1]) # Recursively display three H shape of a smaller order displayHShape(order - 1, p1, length / 2) displayHShape(order - 1, p2, length / 2) displayHShape(order - 1, p3, length / 2) displayHShape(order - 1, p4, length / 2) def drawLine(x1, y1, x2, y2): turtle.penup() turtle.goto(x1, y1) turtle.pendown() turtle.goto(x2, y2) def main(): order = eval(input("Enter an order: ")) length = eval(input("Enter the length of a side: ")) displayHShape(order, [0, 0], length) turtle.done() main()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.