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