annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/tree.py @ 68:5028fdace37b

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