#!/usr/bin/python # -*- encoding: utf-8 -*- from PNGCanvas import PNGCanvas import math from LSystem import LSystem class Turtle(object): def __init__(self): self.x=0 self.y=0 self.leftB=0 self.rightB=0 self.topB=0 self.bottomB=0 self.stack = list() self.p=0 self.alpha=0 self.step=10 self.backcolor=[0,0,0,0] self.drawcolor=[0,0,0,0xff] def setBorders(self,left,right,top,bottom): self.leftB=left self.rightB=right self.topB=top self.bottomB=bottom def prepareCanvas(self,width,height): self.c = PNGCanvas(width,height) self.c.color = self.backcolor # self.c.filledRectangle(0,0,width-1,height-1) // б�аАб�аКаОаМаЕаНб�б� аЕб�аЛаИ аНаАаДаО б�аОаН аНаЕ аПб�аОаЗб�аАб�аНб�аЙ self.c.blendRect(1,1,width-1,height-1,width,0,self.c) self.c.color = self.drawcolor def produceSteps(self,drawFunction,t): self.alpha=0 for action in t.interSteps(t.axiom,1): if action=='+': self.alpha=self.alpha+self.p elif action=='-': self.alpha=self.alpha-self.p elif action=='[': self.stack.append((self.x,self.y,self.alpha)) elif action==']': if len(self.stack)==0: raise Error pos = self.stack.pop() self.x=pos[0] self.y=pos[1] self.alpha=pos[2] elif action=='F': newPosx=(self.x+math.cos(self.alpha)*self.step) newPosy=(self.y+math.sin(self.alpha)*self.step) drawFunction(newPosx,newPosy) self.x=newPosx self.y=newPosy def draw(self,newposx,newposy): # print 'line (',self.x,self.y,')->(',newposx,newposy,')' self.c.line(self.x,self.y,newposx,newposy) def calcSize(self,newposx,newposy): if newposx > self.rightB: # print 'newposx>right' self.rightB=newposx if newposx < self.leftB: # print 'newposx self.topB: # print 'newposy>top' self.topB=newposy if newposy < self.bottomB: # print 'newposy