def __init__(self, start, rules): """Initializes a new L-System""" LSystem.__init__(self, start, rules) # Initialize standard rendering rules + parameters self.fd_length = 5 self.angle = 90 self.__render_rules = TurtleDrawState.default_render_rules()
def render_turtle(self, turtle): """Renders the L-System using the given turtle""" # Create state state = TurtleDrawState(turtle, self.fd_length, self.angle) # Render LSystem.render(self, self.render_rules, state)
def get_lsystem(self): lsys = self._lsystem if not lsys: lsys = LSystem() for c in self.chromosomes: lsys.add_production(c.lhs, c.rhs) self._lsystem = lsys #print "DNA: LSystem:" ### #lsys.dump() ### return lsys
def __init__(self, **kwargs): super(LSystemImage, self).__init__(**kwargs) self.register_event_type('on_update') self.bind(pos=self.reload_image) self.bind(size=self.reload_image) self.iterations = 1 self.background_color = (0, 0, 0) self.line_width = 1 self.all_lines = [] self.ls = LSystem() Clock.schedule_interval(self.update, 1 / 10.0)
def do_set(self, arg): 'Set LSystem (algae, drangoncurve, twindragon, binarytree, koch)' switcher = { 'algae': LSystem('A', { 'A': 'AB', 'B': 'A' }), 'dragoncurve': LSystem('FX', { 'X': 'X+YF+', 'Y': '-FX-Y' }), 'twindragon': LSystem('FX+FX+', { 'X': 'X+YF', 'Y': 'FX-Y' }), 'binarytree': LSystem('0', { '1': '11', '0': '1[+0]-0' }), 'koch': LSystem('F', {'F': 'F+F-F-F+F'}), 'sierpinski': LSystem('F-G-G', { 'F': 'F-G+F+G-F', 'G': 'GG' }), 'arrowhead': LSystem('F', { 'F': 'G-F-G', 'G': 'F+G+F' }), 'hilbert': LSystem('A', { 'A': '-BF+AFA+FB-', 'B': '+AF-BFB-FA+' }), 'moore': LSystem('LFL+F+LFL', { 'L': '-RF+LFL+FR-', 'R': '+LF-RFR-FL+' }), 'fern': LSystem('X', { 'X': 'F+[[X]-X]-F[-FX]+X', 'F': 'FF' }) } self.lsystem = switcher.get(arg, None)
def __init__(self,**kwargs): super(LSystemImage,self).__init__(**kwargs) self.register_event_type('on_update') self.bind(pos=self.reload_image) self.bind(size=self.reload_image) self.iterations = 1 self.background_color = (0,0,0) self.line_width = 1 self.all_lines = [] self.ls=LSystem() Clock.schedule_interval(self.update,1/10.0)
def main(): window = gtk.Window() oLSystem = LSystem() oLSystem.axiom = "F" oLSystem.rules["F"] = "F+F-F-F+F" oLSystem.operations = { "F" : BasicOperation("drawForward", 20), "+" : BasicOperation("turnACCW", (math.pi)/2), "-" : BasicOperation("turnCCW", (math.pi)/2) } oLSystem.setLevel(5) oRenderer = CairoRenderer(oLSystem) window.add(oRenderer) window.connect("destroy", gtk.main_quit) window.show_all() gtk.main()
turtle.penup() turtle.setpos(x, y) turtle.setheading(0) turtle.pendown() turtle.fd(width) turtle.rt(90) turtle.fd(height) turtle.rt(90) turtle.fd(width) turtle.rt(90) turtle.fd(height) turtle.penup() turtle.setpos(x_current, y_current) turtle.setheading(0) turtle.pendown() if __name__ == '__main__': turtle.speed(100) turtle.pencolor('blue') bounds = (30, -30, 70, 70) draw_rect(bounds, turtle) turtle.pencolor('green') lsystem = LSystem(getattr(config, sys.argv[1])) lsystem.run(TurtleBoundsLoggerExecutor(turtle, bounds)) turtle.mainloop()
self.alpha = alpha self.beta = beta self.gamma = gamma self.dl = dl self.dw = dw self.budSize = bs self.min = m self.ls = ls self.a = 0 self.b = 0 self.c = 1 self.csg = csg data0 = Data(32.0, 20.0, 90.0, 1.0, 1.0, 1.1, 2.0, 3, topiary) if __name__ == "__main__": seed = 1965 ls = LSystem(seed, data0) ls.setAxiom([w(1), I(25, 0.5), A(1, 5, 0.5), Q()]) ls.setDerivationDepth(31) ls.declare(P0()) ls.declare(P1()) ls.declare(P2()) ls.declare(P3()) ls.declare(P4()) ls.declare(P5()) ls.declare(P6()) Renderer(ls, data0.csg, 'drawCSG', scale=5.0).render()
], [p(-data.a2), l(), B(a.length * data.r2, a.width * data.wr)] class Data: def __init__(self, r1, r2, a1, a2, wr): self.r1 = r1 self.r2 = r2 self.a1 = a1 self.a2 = a2 self.wr = wr fig_a = Data(0.9, 0.7, 5, 65, 0.707) fig_b = Data(0.9, 0.7, 10, 60, 0.707) fig_c = Data(0.9, 0.8, 20, 50, 0.707) fig_d = Data(0.9, 0.8, 35, 35, 0.707) if __name__ == "__main__": # Run stand alone if len(sys.argv) < 2: print "Usage: python", sys.argv[0], "<Data>" quit() data = eval(sys.argv[1]) ls = LSystem(0, data) ls.declare(P1()) ls.declare(P2()) ls.setAxiom([A(80, 10)]) ls.setDerivationDepth(10) Renderer(ls, None, scale=2.0).render()
class LSystemImage(Im): def __init__(self,**kwargs): super(LSystemImage,self).__init__(**kwargs) self.register_event_type('on_update') self.bind(pos=self.reload_image) self.bind(size=self.reload_image) self.iterations = 1 self.background_color = (0,0,0) self.line_width = 1 self.all_lines = [] self.ls=LSystem() Clock.schedule_interval(self.update,1/10.0) def on_update(self,*args,**kwargs): pass def set_lsystem(self,lsystem): self.ls.set_lsystem(lsystem) self.reset() #self.set_iterations(1) def set_iterations(self,iterations): if iterations<0: iterations = 0 self.iterations=iterations self.reset() def get_iterations(self): return self.iterations def set_background_color(self,color): self.background_color = color self.reload_image(None) def set_width(self,width): self.line_width = width self.reload_image(None) def reset(self): self.ls.reset() self.chunks = self.ls.draw(self.iterations,1000) self.all_lines = [] self.update(force = True) def update(self,*args,**kwargs): try: lines = self.chunks.next() except: lines = [] if lines: self.all_lines+=lines self.reload_image(None) self.dispatch('on_update',len(self.all_lines)) if kwargs.get('force',False): self.reload_image(None) def reload_image(self,instance,*args): im = lines2image(self.all_lines, map(int,self.size), line_width =self.line_width, background_color=self.background_color) stio = StringIO() im.save(stio,format="PNG") stio.seek(0) with self.canvas: self.texture = ImageLoaderPygame(stio).texture
class LSystemImage(Im): def __init__(self, **kwargs): super(LSystemImage, self).__init__(**kwargs) self.register_event_type('on_update') self.bind(pos=self.reload_image) self.bind(size=self.reload_image) self.iterations = 1 self.background_color = (0, 0, 0) self.line_width = 1 self.all_lines = [] self.ls = LSystem() Clock.schedule_interval(self.update, 1 / 10.0) def on_update(self, *args, **kwargs): pass def set_lsystem(self, lsystem): self.ls.set_lsystem(lsystem) self.reset() #self.set_iterations(1) def set_iterations(self, iterations): if iterations < 0: iterations = 0 self.iterations = iterations self.reset() def get_iterations(self): return self.iterations def set_background_color(self, color): self.background_color = color self.reload_image(None) def set_width(self, width): self.line_width = width self.reload_image(None) def reset(self): self.ls.reset() self.chunks = self.ls.draw(self.iterations, 1000) self.all_lines = [] self.update(force=True) def update(self, *args, **kwargs): try: lines = self.chunks.next() except: lines = [] if lines: self.all_lines += lines self.reload_image(None) self.dispatch('on_update', len(self.all_lines)) if kwargs.get('force', False): self.reload_image(None) def reload_image(self, instance, *args): im = lines2image(self.all_lines, map(int, self.size), line_width=self.line_width, background_color=self.background_color) stio = StringIO() im.save(stio, format="PNG") stio.seek(0) with self.canvas: self.texture = ImageLoaderPygame(stio).texture
return FSC(None,[I],None) def produce(self,actuals,data): i = actuals.unpack(ASC.SP) return I(i.t+data.dt,i.dl,i.dw) class P2(Production): def context(self): return FSC(None,[B],None) def produce(self,actuals,data): b = actuals.unpack(ASC.SP) return B(b.t+data.dt) class Data: def __init__(self,gp,dl,dw,dt): self.gp = gp self.dl = dl self.dw = dw self.dt = dt data0 = Data(10,5,0.1,1) ls = LSystem(int(sys.argv[1]),data0) ls.declare(P0()) ls.declare(P1()) ls.declare(P2()) ls.setAxiom([A(1,data0.dl,data0.dw)]) ls.setDerivationDepth(100) Renderer(ls,None,scale=2.75).render()
self.r1 = r1 self.r2 = r2 self.a1 = a1 self.a2 = a2 self.f1 = f1 self.f2 = f2 self.w0 = w0 self.q = q self.e = e self.m = m fig_a = Data(0.75, 0.77, 35, -35, 0, 0, 30, 0.5, 0.4, 0.0) fig_b = Data(0.65, 0.71, 27, -68, 0, 0, 20, 0.53, 0.50, 1.7) fig_f = Data(0.92, 0.37, 0, 60, 180, 0, 2, 0.5, 0.0, 0.5) fig_g = Data(0.8, 0.8, 30, -30, 137, 137, 30, 0.5, 0.5, 0.0) fig_h = Data(0.95, 0.75, 5, -30, -90, 90, 40, 0.6, 0.45, 25.0) fig_i = Data(0.55, 0.95, -5, 30, 137, 137, 5, 0.4, 0.0, 5.0) if __name__ == "__main__": # Run stand alone if len(sys.argv) < 2: print "Usage: python", sys.argv[0], "<Data>" quit() data = eval(sys.argv[1]) ls = LSystem(0, data) ls.declare(P0()) ls.setAxiom([A(100, data.w0)]) ls.setDerivationDepth(10) Renderer(ls, None, scale=2.0).render()
import turtle from lsystem import LSystem plant = dict(axiom='X', productions={'X': 'F-[[X]+X]+F[+FX]-X', 'F': 'FF'}) plantL = LSystem(**plant) q = [] def restore(): pos, angl = q.pop() turtle.up() turtle.setposition(pos) turtle.seth(angl) turtle.down() methods = { 'F': lambda: turtle.fd(3), '-': lambda: turtle.left(25), '+': lambda: turtle.right(25), '[': lambda: q.append((turtle.pos(), turtle.heading())), ']': restore, } turtle.screensize(800, 1200) turtle.ht() turtle.pencolor('green') turtle.delay(0) turtle.seth(75) for c in plantL[6]:
self.b = 0 self.c = 1 self.csg = csg data0 = Data(32.0, 20.0, 90.0, 1.0, 1.0, 1.1, 2.0, 3, topiary) if __name__ == "__main__": if len(sys.argv) < 5: print "Usage: python %s <axiom> <data> <depth> <seed>" % sys.argv[0] print "Example: \"[w(0.5),F(25,0.5),A(1,5,0.5),Q()]\" data0 5 92333" quit() # Axiom on form: "[A(),Q()]" data = eval(sys.argv[2]) maxDepth = int(sys.argv[3]) axiom = eval(sys.argv[1]) seed = int(sys.argv[4]) ls = LSystem(seed, data) ls.setAxiom(axiom) ls.setDerivationDepth(maxDepth) ls.declare(P0()) ls.declare(P1()) ls.declare(P2()) ls.declare(P3()) ls.declare(P4()) ls.declare(P5()) ls.declare(P6()) Renderer(ls, data.csg, 'drawCSG', scale=5.0).render()