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