ยปCore Development>Code coverage>Lib/turtledemo/forest.py

Python code coverage for Lib/turtledemo/forest.py

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