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