Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. """
  2. Generates an "h-tree" using recursion and turtle graphics.
  3.  
  4. Example: The program runs with one command line argument which is the
  5. depth of recursion (greater than or equal to 0). For example:
  6.  
  7. $ python3 HTree
  8. Usage: python3 HTree #
  9.  
  10. $ python3 HTree -1
  11. The depth must be greater than or equal to 0
  12.  
  13. $ python3 HTree 0 # draws nothing
  14.  
  15. $ python3 HTree 1 # draws a h-tree of depth 1 (see writeup)
  16.  
  17. $ python3 HTree 2 # draws a h-tree of depth 2 (see writeup)
  18.  
  19. author: sps (Sean Strout @ RIT CS)
  20. """
  21.  
  22. import turtle # for turtle commands
  23. import sys # for access to command line arguments
  24.  
  25. # The length of the largest segment (a constant)
  26. MAX_SEGMENT_LENGTH = 1024
  27.  
  28.  
  29. def init(length, depth):
  30. """
  31. Initialize the graphics.
  32.  
  33. length (number): The length of the main snowflake branch.
  34. depth (int): The depth of recursion.
  35. """
  36.  
  37. turtle.setworldcoordinates(-length*2, -length*2, length*2, length*2)
  38. turtle.title("H-Tree, depth: " + str(depth))
  39.  
  40.  
  41. def drawHTree(length, depth):
  42. """
  43. Recursively draw the H-Tree.
  44.  
  45. length (number): the length of the current h-tree segments
  46. depth (int): the current depth of recursion
  47.  
  48. pre-conditions:
  49. depth is greater than or equal to 0
  50. turtle is at center of current h-tree, down, facing east
  51.  
  52. post-conditions:
  53. turtle is at center of current h-tree, down, facing east
  54. """
  55.  
  56. if depth > 0:
  57. # start in center of H, move to upper right
  58. turtle.forward(length / 2)
  59. turtle.left(90)
  60. turtle.forward(length / 2)
  61. turtle.right(90)
  62.  
  63. # recurse
  64. drawHTree(length / 2, depth - 1)
  65.  
  66. # move to lower right of H
  67. turtle.right(90)
  68. turtle.forward(length)
  69. turtle.left(90)
  70.  
  71. # recurse
  72. drawHTree(length / 2, depth - 1)
  73.  
  74. # move to upper left of H
  75. turtle.left(90)
  76. turtle.forward(length / 2)
  77. turtle.left(90)
  78. turtle.forward(length)
  79. turtle.right(90)
  80. turtle.forward(length / 2)
  81. turtle.right(90)
  82.  
  83. # recurse
  84. drawHTree(length / 2, depth - 1)
  85.  
  86. # move to lower left of H
  87. turtle.right(90)
  88. turtle.forward(length)
  89. turtle.left(90)
  90.  
  91. # recurse
  92. drawHTree(length / 2, depth - 1)
  93.  
  94. # return to center of H
  95. turtle.left(90)
  96. turtle.forward(length / 2)
  97. turtle.right(90)
  98. turtle.forward(length / 2)
  99.  
  100.  
  101. def main():
  102. """
  103. The main method reads the command line argument for the depth
  104. and draws the h-tree.
  105. """
  106.  
  107. # In python, when run on the command line, the name of the module
  108. # is sys.argv[0] and the first command line argument is sys.argv[1], e.g.:
  109. #
  110. # $ python3 HTree 2
  111. #
  112. # sys.argv[0] -> 'HTree'
  113. # sys.argv[1] -> '2'
  114. if len(sys.argv) < 2:
  115. print('Usage: python3 HTree #')
  116. return
  117.  
  118. # check the depth is >= 0
  119. depth = int(sys.argv[1])
  120. if depth < 0:
  121. print('The depth must be greater than or equal to 0')
  122. return
  123.  
  124. # initialize turtle
  125. init(MAX_SEGMENT_LENGTH, depth)
  126.  
  127. # draw the h-tree
  128. drawHTree(MAX_SEGMENT_LENGTH, depth)
  129.  
  130. # wait for input to close
  131. input("Hit enter to close...")
  132.  
  133. if __name__ == '__main__':
  134. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement