usr bin python encoding utf-8 from PNGCanvas import PNGCanvas import m

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/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<left'
self.leftB=newposx
if newposy > self.topB:
# print 'newposy>top'
self.topB=newposy
if newposy < self.bottomB:
# print 'newposy<bottom'
self.bottomB=newposy
# print 'left=',self.leftB,'right=',self.rightB
# print 'top=',self.topB,'bottom=',self.bottomB
# print 'pos=',newposx,newposy
# print '---'
def start(self,pngFileName,rulesFileName,deep):
rules = open(rulesFileName,'r')
t = LSystem(['F','-','+','b','[',']'],deep)
t.setAxiom(rules.readline())
self.p = math.pi/float(rules.readline())
while 1:
str = rules.readline()
print str
if not str: break
rule=str.split('=')
t.registerRule(rule[0],rule[1])
rules.close()
self.calcSize(self.x,self.y)
self.produceSteps(self.calcSize,t)
width=self.rightB-self.leftB
# print 'top=',self.topB, 'bottom=',self.bottomB
# print 'left=',self.leftB,'right=',self.rightB
height=self.topB-self.bottomB
self.x=abs(self.leftB)
self.y=abs(self.bottomB)
# print width,height,'pos',self.x,self.y
self.prepareCanvas(int(width+1),int(height+1))
self.produceSteps(self.draw,t)
file = open(pngFileName,'wb')
file.write(self.c.dump())
file.close()
if __name__=='__main__':
png = raw_input('outputfile:')
data = raw_input('datafile:')
deep = int(raw_input('deep:'))
t = Turtle()
t.start(png,data,deep)
# vim:set tabstop=4 softtabstop=4 shiftwidth=4 expandtab: