1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| ''' Created on Dec 2, 2017
@author: QiZhao '''
from turtle import Turtle def tree(plist, l, a, f): """ plist is list of pens l is length of branch a is half of the angle between 2 branches f is factor by which branch is shortened from level to level.""" if l > 5: lst = [] for p in plist: p.forward(l) q = p.clone() p.left(a) q.right(a) lst.append(p) lst.append(q) tree(lst, l*f, a, f) def tree2(p,l,a,f): if l>5: p.forward(l) q=p.clone() p.left(a) q.right(a) tree2(p, l*f, a, f) tree2(q, l*f, a, f)
def maketree(x,y): p = Turtle() p.color("blue") p.pensize(5) p.setundobuffer(None) p.hideturtle() p.getscreen().tracer(1,0) p.left(90) p.penup() p.goto(x,y) p.pendown() tree([p], 100, 65, 0.6375) p.penup() p.setheading(90) p.goto(x,y) p.down() p.color("green") tree2(p, 100, 65, 0.6375) def main(): maketree(-200, -200) maketree(0, 0) maketree(200,-200) main()
|