frompyprocessingimport*frommathimportpi, sinfromlsystemsimportcsgrammarfromlsystems.arcballimportArcBall myball=None# needs exposure at module level THETA=(6.5*pi)/36 # 32.5 degrees in radians production=None# needs exposure at module level distance=90 repeat=1 count=0 scale_factor=[0.55, 0.65, 0.72, 0.75, 0.8, 0.85] AXIOM='F' IGNORE='[]+-^&3' RULES={ 'F':'F[-EF[3&A]]E[+F[3^A]]', # context free rule 'F<E':'F[&F[3+A]][^F[3-A]]' # context sensitive rule }defsetup():""" processing setup """ size(800, 600)globalproduction, myball myball=ArcBall(width/2.0, height/2.0,(width-20)*0.5)myball.selectAxis(1)production=csgrammar.repeat(6, AXIOM, RULES, IGNORE)fill(0, 200, 0)noStroke()defdraw():""" Animate a 3D context free plant in processing/pyglet draw loop """ background(20, 20, 180)lights()translate(width/2.0, height*0.8)lightSpecular(204, 204, 204)specular(255, 255, 255)shininess(1.0)update()forvalinproduction:evaluate(val)def__noop():passdef__yawRight():globalrepeat rotateX(-THETA*repeat)repeat=1def__yawLeft():globalrepeat rotateX(THETA*repeat)repeat=1def__rollRight():globalrepeat rotateZ(-THETA*repeat)repeat=1def__rollLeft():globalrepeat rotateZ(THETA*repeat)repeat=1def__thriceRepeat():globalrepeat repeat=3def__pushStack():pushMatrix()def__popStack():popMatrix()def__drawRod():globaldistance, count sides=16 radius1=distance/6 radius2=distance/(6*scale_factor[count])angle=0 angleIncrement=TWO_PI/sides beginShape(QUAD_STRIP)foriinrange(sides+1):normal(cos(angle), 0, sin(angle))vertex(radius2*cos(angle), 0, radius2*sin(angle))vertex(radius1*cos(angle),-distance, radius1*sin(angle))angle+=angleIncrement endShape()# draw the spherical top cap translate(0,-distance, 0)sphereDetail(16)sphere(radius1)if(count>0):scale(scale_factor[count]);if(count<len(scale_factor)-1):count+=1 #### # A dictionary of lsystem operations #### lsysOp={ '+':__yawRight, '-':__yawLeft, '^':__rollLeft, '&':__rollRight, '3':__thriceRepeat, 'F':__drawRod, '[':__pushStack, ']':__popStack, 'A':__noop, 'E':__noop }defupdate():""" wrap arcball update and rotation as a local function """ theta, x, y, z=myball.update()rotate(theta, x, y, z)defmousePressed():myball.mousePressed(mouse.x, mouse.y)defmouseDragged():myball.mouseDragged(mouse.x, mouse.y)defevaluate(key):""" Is a wrapper controlling access to the dict of functions """iflsysOp.has_key(key):lsysOp[key]()else:ifnotRULES.has_key(key):# useful debugging check you could("Unknown rule {0}".format(key))# comment out these lines run()

Image produced using pyprocessing takesnapshot utility |

## No comments:

## Post a Comment