Advertisement
UniQuet0p1

Untitled

Oct 29th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. """Meta-trees and meta-dragons."""
  2.  
  3. from turtle import Turtle
  4. from sys import setrecursionlimit
  5. setrecursionlimit(10000)
  6.  
  7.  
  8. def tree(length):
  9. """
  10. Write a recursive turtle program to draw a binary tree.
  11.  
  12. Start with a trunk 200px tall.
  13. Each new branch should be 3/5 as big as its trunk.
  14. Minimum branch size is 5px.
  15. Move turtle with: t.forward(), t.left(), t.right(), tree()
  16.  
  17. :param length: height of the trunk or leaf
  18. """
  19. pass
  20.  
  21.  
  22. def apply_dragon_rules(string):
  23. """
  24. Write a recursive function that replaces characters in string.
  25.  
  26. Like so:
  27. "a" -> "aRbFR"
  28. "b" -> "LFaLb"
  29. apply_dragon_rules("a") -> "aRbFR"
  30. apply_dragon_rules("aa") -> "aRbFRaRbFR"
  31. apply_dragon_rules("FRaFRb") -> "FRaRbFRFRLFaLb"
  32.  
  33. :param string: sentence with "a" and "b" characters that need to be replaced
  34. :return: new sentence with "a" and "b" characters replaced
  35. """
  36. pass
  37.  
  38.  
  39. def curve(string, depth):
  40. """
  41. Recursively generate the next depth of rules.
  42.  
  43. Calls apply_dragon_rules() function `depth` times.
  44. curve("Fa", 2) -> "FaRbFRRLFaLbFR"
  45.  
  46. :param string: current instruction string
  47. :param depth: how many times the rules are applied
  48. :return: instructionset at iteration 'depth'
  49. """
  50. pass
  51.  
  52.  
  53. def format_curve(string):
  54. """
  55. Use recursions to remove a and b symbols from the instruction string.
  56.  
  57. format_curve("Fa") -> "F"
  58. format_curve("FaRbFR") -> "FRFR"
  59.  
  60. :param string: instruction string
  61. :return: clean instructions with only "F", "R", and "L" characters
  62. """
  63. pass
  64.  
  65.  
  66. def draw_dragon(string, length):
  67. """Draw the dragon by reading the string recursively.
  68.  
  69. Use t.right(), t.left(), t.forward() and draw_dragon() to move turtle.
  70. L - means turn 90 degrees to left and go forward
  71. R - means turn 90 degrees to right and go forward
  72. F - means don't turn just go forward
  73.  
  74. :param string: instructions left to process
  75. :param length: how many pixels to move forward, left or right
  76. """
  77. pass
  78.  
  79.  
  80. def get_line_length(dragon_width, depth):
  81. """Return one Turtle step length if the width and depth are known."""
  82. return dragon_width / (2 ** (1 / 2)) ** depth
  83.  
  84.  
  85. def save(t: Turtle):
  86. """Save the turtle graphic to file which can be opened with a image editor like GIMP."""
  87. t.ht() # hide him
  88. t.getscreen().getcanvas().postscript(file='tree.ps')
  89.  
  90.  
  91. if __name__ == '__main__':
  92. t = Turtle()
  93. t.getscreen().bgcolor("#1c262b")
  94. t.color("#96004f")
  95. t.speed(0)
  96. t.pensize(2)
  97. t.left(90)
  98. tree(200)
  99.  
  100. '''
  101. s = curve("Fa", 8)
  102. s = format_curve(s)
  103. l = get_line_length(100, 8)
  104. draw_dragon(s, l)
  105. '''
  106. save(t)
  107. t.getscreen().exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement