forest.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. """ turtlegraphics-example-suite:
  3. tdemo_forest.py
  4. Displays a 'forest' of 3 breadth-first-trees
  5. similar to the one in tree.
  6. For further remarks see tree.py
  7. This example is a 'breadth-first'-rewrite of
  8. a Logo program written by Erich Neuwirth. See
  9. http://homepage.univie.ac.at/erich.neuwirth/
  10. """
  11. from turtle import Turtle, colormode, tracer, mainloop
  12. from random import randrange
  13. from time import perf_counter as clock
  14. def symRandom(n):
  15. return randrange(-n,n+1)
  16. def randomize( branchlist, angledist, sizedist ):
  17. return [ (angle+symRandom(angledist),
  18. sizefactor*1.01**symRandom(sizedist))
  19. for angle, sizefactor in branchlist ]
  20. def randomfd( t, distance, parts, angledist ):
  21. for i in range(parts):
  22. t.left(symRandom(angledist))
  23. t.forward( (1.0 * distance)/parts )
  24. def tree(tlist, size, level, widthfactor, branchlists, angledist=10, sizedist=5):
  25. # benutzt Liste von turtles und Liste von Zweiglisten,
  26. # fuer jede turtle eine!
  27. if level > 0:
  28. lst = []
  29. brs = []
  30. for t, branchlist in list(zip(tlist,branchlists)):
  31. t.pensize( size * widthfactor )
  32. t.pencolor( 255 - (180 - 11 * level + symRandom(15)),
  33. 180 - 11 * level + symRandom(15),
  34. 0 )
  35. t.pendown()
  36. randomfd(t, size, level, angledist )
  37. yield 1
  38. for angle, sizefactor in branchlist:
  39. t.left(angle)
  40. lst.append(t.clone())
  41. brs.append(randomize(branchlist, angledist, sizedist))
  42. t.right(angle)
  43. for x in tree(lst, size*sizefactor, level-1, widthfactor, brs,
  44. angledist, sizedist):
  45. yield None
  46. def start(t,x,y):
  47. colormode(255)
  48. t.reset()
  49. t.speed(0)
  50. t.hideturtle()
  51. t.left(90)
  52. t.penup()
  53. t.setpos(x,y)
  54. t.pendown()
  55. def doit1(level, pen):
  56. pen.hideturtle()
  57. start(pen, 20, -208)
  58. t = tree( [pen], 80, level, 0.1, [[ (45,0.69), (0,0.65), (-45,0.71) ]] )
  59. return t
  60. def doit2(level, pen):
  61. pen.hideturtle()
  62. start(pen, -135, -130)
  63. t = tree( [pen], 120, level, 0.1, [[ (45,0.69), (-45,0.71) ]] )
  64. return t
  65. def doit3(level, pen):
  66. pen.hideturtle()
  67. start(pen, 190, -90)
  68. t = tree( [pen], 100, level, 0.1, [[ (45,0.7), (0,0.72), (-45,0.65) ]] )
  69. return t
  70. # Hier 3 Baumgeneratoren:
  71. def main():
  72. p = Turtle()
  73. p.ht()
  74. tracer(75,0)
  75. u = doit1(6, Turtle(undobuffersize=1))
  76. s = doit2(7, Turtle(undobuffersize=1))
  77. t = doit3(5, Turtle(undobuffersize=1))
  78. a = clock()
  79. while True:
  80. done = 0
  81. for b in u,s,t:
  82. try:
  83. b.__next__()
  84. except:
  85. done += 1
  86. if done == 3:
  87. break
  88. tracer(1,10)
  89. b = clock()
  90. return "runtime: %.2f sec." % (b-a)
  91. if __name__ == '__main__':
  92. main()
  93. mainloop()