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