# -*- 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.1) # # Revision: (v1.1) # - fix: pencolor: set before drawing instead of at beginning of A()/B() # - add: colors2 - RGB+YCM # # References: # 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"] colors2 = ["red", "yellow", "green", "cyan", "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 = colors2): if ( level <= 0 ): return "Done! with Level A(%d)" % level # t.pencolor(colors[level % sides]) #x will be overwritten by A/B(level-1) B(level-1,fwd) t.pencolor(colors[level % sides]) t.forward(fwd) t.left(60) print("f-",end='') A(level-1,fwd) t.pencolor(colors[level % sides]) 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 = colors2): if ( level <= 0 ): return "Done! with Level B(%d)" % level # t.pencolor(colors[level % sides]) #x will be overwritten by A/B(level-1) A(level-1,fwd) t.pencolor(colors[level % sides]) t.forward(fwd) t.right(60) print("f+",end='') B(level-1,fwd) t.pencolor(colors[level % sides]) 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") # wn.bgcolor("white") 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) # # Even-numbered levels [2,4,6,8] are increasingly larger with the same top A structure. # Odd-numbered levels have similar relationship (but in opposite orientation). # # Best demo level: 6 # if (False): Sierpinski_Triangle(level=2, fwd=5) Sierpinski_Triangle(level=4, fwd=5) Sierpinski_Triangle(level=6, fwd=5) Sierpinski_Triangle(level=8, fwd=5) else: 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