# -*- encoding: big5 -*- # # OR, -*- coding: big5 -*- // seems fine # # Tree (3-branches) # # Author: # Jing-Shin Chang # # Emails: # jshin@csie.ncnu.edu.tw // shin@nlp.csie.ncnu.edu.tw // shin.jsc@gmail.com # # Last Update: # 2019/11/22 (v1.0.1) # # Revision: (v1.0) # - init. # - Trees with 3 branches # #+1 from turtle import * # including mainloop #+2 from turtle import Turtle, Screen, mainloop from time import clock # TURTLEDEMO: # - Globals related to other modules must be declared in main when included by TURTLEDEMO #- t = Turtle() #- wn = Screen() #- wn.bgcolor("black") #- t.speed(0) # n_colors = 6 colors0 = ["yellow", "magenta", "yellow", "magenta", "yellow", "magenta"] colors1 = ["yellow", "magenta", "red", "magenta", "blue", "magenta"] colors2 = ["red", "yellow", "green", "cyan", "blue", "magenta"] n_colors = len(colors2) def gotoOrigin(x=0,y=0): t.penup() t.goto(x,y) t.pendown() def Tree(level, branch, x_root, y_root, fwd, angle, colors=colors2): import math as m if ( level <= 0 ): return "Done! with Tree Level(%d) Branch(%d)" % (level, branch) # configuration fwdScale = 0.8 angScale = 0.75 color = colors[(level+branch) % n_colors] # parameters for next level tree fwdNext = fwd*fwdScale angNext = angle*angScale # Left branch angleD = angle angleR = (angleD + 90) * m.pi/180.0 # adding 90deg since we are going north x_rootNext = x_root + fwd * m.cos(angleR) y_rootNext = y_root + fwd * m.sin(angleR) t.pencolor(color) gotoOrigin(x_root, y_root) t.left(angleD) t.forward(fwd) t.left(-angleD) Tree(level-1, -1, x_rootNext, y_rootNext, fwdNext, angNext, colors) # Middle branch angleD = 0 angleR = (angleD + 90) * m.pi/180.0 x_rootNext = x_root + fwd * m.cos(angleR) y_rootNext = y_root + fwd * m.sin(angleR) t.pencolor(color) gotoOrigin(x_root, y_root) # t.right(angleD) t.forward(fwd) # t.right(-angleD) Tree(level-1, 0, x_rootNext, y_rootNext, fwdNext, angNext, colors) # Right branch angleD = angle angleR = (- angleD + 90) * m.pi/180.0 x_rootNext = x_root + fwd * m.cos(angleR) y_rootNext = y_root + fwd * m.sin(angleR) t.pencolor(color) gotoOrigin(x_root, y_root) t.right(angleD) t.forward(fwd) t.right(-angleD) Tree(level-1, +1, x_rootNext, y_rootNext, fwdNext, angNext, colors) print("Done! with Tree Level(%d) Branch(%d)" % (level, branch)) return "Done! with Tree Level(%d) Branch(%d)" % (level, branch) def drawTree(level=3, branch=0, x_root=0, y_root=0, fwd=5, angle=60, colors=colors2): len_root = fwd*2 t.pencolor(colors[(level+branch) % n_colors]) # t.mode("standard") # ccw, heading East (0 deg) # t.home() # goto(0,0), heading East gotoOrigin(x_root,y_root) t.left(90) t.forward(len_root) Tree(level, branch, x_root, y_root=y_root+len_root, fwd=fwd, angle=angle, colors=colors) t.right(90) # resume angle for next drawTree() # # Try different parameter passing options # def main(): global t, wn t = Turtle() # Global must be declared in main when included in TURTLEDEMO wn = Screen() wn.bgcolor("black") # wn.bgcolor("white") t.speed(0) # doesn't look like speeding by this arg t0 = clock() t.hideturtle() drawTree(level=4, branch=0, x_root=0, y_root=-100, fwd=50, angle=80, colors=colors2) drawTree(level=3, branch=0, x_root=200, y_root=-200, fwd=50, angle=80, colors=colors2) drawTree(level=5, branch=0, x_root=-180, y_root=-200, fwd=50, angle=80, colors=colors2) t1 = clock() return "Done! in %.2f Seconds" % (t1-t0) if __name__ == "__main__": msg = main() print(msg) mainloop() # keep alive until closed by user # FIN