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