## ## ## 電腦繪圖 ## ## Project 4 - The Dragon Curve ## ## 第九組98131034 張晉瑋 ##  98131004 黃振綱  ## 99131085 徐誌偉 ## ## 指導老師 江清水 ## ## 2013.12.18 ## from PIL import Image from PIL import ImageDraw import random def T(f,r): if f==1: return r return T(f-1,'R,')+r+T(f-1,'L,') def direction(L): List=['N'] for count in (L[0:]): if List[-1]=='N': if count=='R': List=List+['E'] if count=='L': List=List+['W'] elif List[-1]=='E': if count=='R': List=List+['S'] if count=='L': List=List+['N'] elif List[-1]=='W': if count=='R': List=List+['N'] if count=='L': List=List+['S'] elif List[-1]=='S': if count=='R': List=List+['W'] if count=='L': List=List+['E'] return List def position(List,size): x=size/2 y=size/2 s=size/2,size/2 for count in (List[0:]): if count=="N": y=y-10 ns=x,y s=s+ns elif count=="E": x=x+10 ns=x,y s=s+ns elif count=="W": x=x-10 ns=x,y s=s+ns elif count=="S": y=y+10 ns=x,y s=s+ns return s def drawLine(position,size,color): img=Image.new( "RGB" ,(size,size), "white") draw = ImageDraw.Draw(img) draw.line(position, fill=color) img.show() def main(): fold=eval(input("Please input fold(1~11):")) if fold==1: size=100 elif 12>fold>1: size=(fold-1)*100 elif 16>fold>11: size=fold*(fold-2)*20 else: size=8000 color=eval(input("Please input color(R,G,B,or random (ex.255,0,0)):")) if color==random: color=int(random.uniform(255,0)),int(random.uniform(255,0)),int(random.uniform(255,0)) return drawLine(position(direction(T(fold,'R,')),size),size,color) main()