# -*- encoding: big5 -*- # # ColorSpiral.py # # - jshin@csie.ncnu.edu.tw // shin@nlp.csie.ncnu.edu.tw // shin.jsc@gmail.com # # # Sierpinski_triangle # # # 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) # # https://zh.wikipedia.org/wiki/%E8%AC%9D%E7%88%BE%E8%B3%93%E6%96%AF%E5%9F%BA%E4%B8%89%E8%A7%92%E5%BD%A2 # """ 這條曲線以L系統來記述為: 變數: A , B 常數: + , - 公理: A 規則: A → B-A-B B → A+B+A A,B : 向前 - : 左轉60° + : 右轉60° """ """ IF NOT PART OF TURTLEDEMO ... import turtle t = turtle.Turtle() wn = turtle.Screen() """ #+1 from turtle import * # including mainloop #+2 from turtle import Turtle, Screen, mainloop from time import clock #- t = Turtle() # Globals ewlated to other modules must be declared in main when included by TURTLEDEMO #- wn = Screen() #- wn.bgcolor("black") #- t.speed(0) sides = 6 colors0 = ["yellow", "magenta", "yellow", "magenta", "yellow", "magenta"] colors1 = ["yellow", "magenta", "red", "magenta", "blue", "magenta"] def colorSpiral(nMoves = 60, degRotation = 1, wider = True, constFwd = 0, fwdMore = True, colors = colors1): # gotoOrigin() for x in range(nMoves): t.pencolor(colors[x % sides]) # the number of colors must be >= sides if ( constFwd != 0 ): t.forward(constFwd) # constant move step elif (not fwdMore): t.forward(x) # step size increase as x else: t.forward(x * 3/sides + x) # [0, 0, 1, 1, 2, 2, 3, 3, ...] + x t.left(360/sides + degRotation) # 360/sides: make a complete rotation every #side left turns # +degRotation = 0: without rotation # +degRotation = 1: with a little rotation if ( wider ): t.width(x*sides/200) # incresingly wider def gotoOrigin(x=0,y=0): t.penup() t.goto(x,y) t.pendown() def Sierpinski_triangle(level=10, fwd=50): # A(level,fwd) # A=> 'B'-'A'-'B': bottom-left B first then top A, then bottom-right B B(level,fwd) # B=> 'A'+'B'+'A': bottom-left A first then top B, then bottom-right A def A(level,fwd,colors = colors1): if ( level <= 0 ): return "Done! with Level A(%d)" % level t.pencolor(colors[level % sides]) B(level-1,fwd) t.forward(fwd) t.left(60) print("f-",end='') A(level-1,fwd) t.forward(fwd) t.left(60) print("f-",end='') B(level-1,fwd) print("\nDone! with Level A(%d)" % level) return "Done! with Level A(%d)" % level def B(level,fwd,colors = colors0): if ( level <= 0 ): return "Done! with Level B(%d)" % level t.pencolor(colors[level % sides]) A(level-1,fwd) t.forward(fwd) t.right(60) print("f+",end='') B(level-1,fwd) t.forward(fwd) t.right(60) print("f+",end='') A(level-1,fwd) print("\nDone! with Level B(%d)" % level) return "Done! with Level B(%d)" % level # # 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") t.speed(0) # doesn't look like speeding by this arg t0 = clock() gotoOrigin(-200,-100) t.hideturtle() # colorSpiral() # default # colorSpiral(constFwd=60) # colorSpiral(colors=colors0) # colorSpiral(nMoves=80) # colorSpiral(degRotation=0) # colorSpiral(wider=False) # colorSpiral(fwdMore=False) # colorSpiral(degRotation=0, wider=False) # colorSpiral(120, 10, False) Sierpinski_triangle(level=6, fwd=5) t1 = clock() return "Done! in %.2f Seconds" % (t1-t0) if __name__ == "__main__": msg = main() print(msg) mainloop() # keep alive until closed by user # FIN