Debian Codegraph @

Lib/turtledemo/tree.py

raw
1 """turtledemo/tree.py
2
3 Displays a 'breadth-first-tree' - in contrast
4 to the classical Logo tree drawing programs,
5 which use a depth-first-algorithm.
6
7 Uses:
8 (1) a tree-generator, where the drawing is
9 quasi the side-effect, whereas the generator
10 always yields None.
11 (2) Turtle-cloning: At each branching point
12 the current pen is cloned. So in the end
13 there are 1024 turtles.
14 """
15 from turtle import Turtle, mainloop
16 from time import perf_counter as clock
17
18 def tree(plist, l, a, f):
19 """ plist is list of pens
20 l is length of branch
21 a is half of the angle between 2 branches
22 f is factor by which branch is shortened
23 from level to level."""
24 if l > 3:
25 lst = []
26 for p in plist:
27 p.forward(l)
28 q = p.clone()
29 p.left(a)
30 q.right(a)
31 lst.append(p)
32 lst.append(q)
33 for x in tree(lst, l*f, a, f):
34 yield None
35
36 def maketree():
37 p = Turtle()
38 p.setundobuffer(None)
39 p.hideturtle()
40 p.speed(0)
41 p.getscreen().tracer(30,0)
42 p.left(90)
43 p.penup()
44 p.forward(-210)
45 p.pendown()
46 t = tree([p], 200, 65, 0.6375)
47 for x in t:
48 pass
49
50 def main():
51 a=clock()
52 maketree()
53 b=clock()
54 return "done: %.2f sec." % (b-a)
55
56 if __name__ == "__main__":
57 msg = main()
58 print(msg)
59 mainloop()