from visual import * from turtle import * import time scene.autocenter=1 axiom='FF' rules={} rules['F']='FLFPBFPTF' def F(): print 'branch' def R(): print 'turn right' def L(): print 'turn left' def T(): print 'bend towards' def B(): print 'bend backward' def P(): print 'pop' aa=[0] bb=[0] cc=[0] dd=[0] ff=[0] def produce(axiom, rules): output="" for i in axiom: output=output+rules.get(i,i) return output def iterate(n,axiom,rules): print axiom while n>0: axiom=produce(axiom,rules) return iterate(n-1,axiom,rules) axiomarray=[c for c in axiom] print axiomarray construct(axiomarray) def construct(axiomarray): for i in range(len(axiomarray)): print i if axiomarray[i]=="F": time.sleep(1) F() branch=cylinder(pos=(aa[i],bb[i],cc[i]), axis=(dd[i],3,ff[i]), radius=(.25), color=(0,1,0)) if dd[i]==0 and ff[i]==0: aa.append(aa[i]) bb.append(bb[i]+(3)) cc.append(cc[i]) dd.append(0) ff.append(0) if dd[i]<0 and ff[i]>0: aa.append(aa[i]+cos(22.5)) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]-cos(22.5)) dd.append(0) ff.append(0) if dd[i]>0 and ff[i]>0: aa.append(aa[i]-cos(22.5)) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]-cos(22.5)) dd.append(0) ff.append(0) if dd[i]<0 and ff[i]<0: aa.append(aa[i]+cos(22.5)) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]+cos(22.5)) dd.append(0) ff.append(0) if dd[i]>0 and ff[i]<0: aa.append(aa[i]-cos(22.5)) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]+cos(22.5)) dd.append(0) ff.append(0) if dd[i]>0 and ff[i]==0: aa.append(aa[i]+cos(44.5)) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]) dd.append(0) ff.append(0) if dd[i]<0 and ff[i]==0: aa.append(aa[i]-cos(44.5)) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]) dd.append(0) ff.append(0) if ff[i]>0 and dd[i]==0: aa.append(aa[i]) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]+cos(44.5)) dd.append(0) ff.append(0) if ff[i]<0 and dd[i]==0: aa.append(aa[i]) bb.append(bb[i]+(3*sin(45))) cc.append(cc[i]-cos(44.5)) dd.append(0) ff.append(0) else: if axiomarray[i]=="L": aa.append(aa[i]) bb.append(bb[i]) cc.append(cc[i]) dd.append(-1) ff.append(ff[i]) if axiomarray[i]=="R": aa.append(aa[i]) bb.append(bb[i]) cc.append(cc[i]) dd.append(1) ff.append(ff[i]) if axiomarray[i]=="T": aa.append(aa[i]) bb.append(bb[i]) cc.append(cc[i]) dd.append(dd[i]) ff.append(1) if axiomarray[i]=="B": aa.append(aa[i]) bb.append(bb[i]) cc.append(cc[i]) dd.append(dd[i]) ff.append(-1) if axiomarray[i]=="P": aa.append(aa[i-2]) bb.append(bb[i-2]) cc.append(cc[i-2]) dd.append(dd[i-2]) ff.append(ff[i-2]) iterate(2,axiom,rules)