blob: 98a20da7f15c11d90a4243b8319131ceed2e8f4c [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002""" turtle-example-suite:
3
4 tdemo_tree.py
5
6Displays a 'breadth-first-tree' - in contrast
7to the classical Logo tree drawing programs,
8which use a depth-first-algorithm.
9
10Uses:
11(1) a tree-generator, where the drawing is
12quasi the side-effect, whereas the generator
13always yields None.
Terry Jan Reedyc5a72e62014-06-24 22:21:41 -040014(2) Turtle-cloning: At each branching point
15the current pen is cloned. So in the end
16there are 1024 turtles.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000017"""
Martin v. Löwis60ebb8b2008-09-21 07:32:10 +000018from turtle import Turtle, mainloop
Victor Stinner884d13a2017-10-17 14:46:45 -070019from time import perf_counter as clock
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000020
21def 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
39def 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
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000052
53def main():
54 a=clock()
55 maketree()
56 b=clock()
57 return "done: %.2f sec." % (b-a)
58
59if __name__ == "__main__":
60 msg = main()
61 print(msg)
62 mainloop()