[cairo-commit] python-cairo/examples lsystem.py,NONE,1.1

Carl Worth commit at pdx.freedesktop.org
Thu Nov 20 09:31:53 PST 2003


Committed by: cworth

Update of /cvs/cairo/python-cairo/examples
In directory pdx:/tmp/cvs-serv8133

Added Files:
	lsystem.py 
Log Message:
Added lsystem.py

--- NEW FILE: lsystem.py ---
#!/usr/bin/env python

import gtk
import cairo
import cairo.gtk

# Copyright 2003 Jesse Andrews (jdandr2 at uky.edu) under GPL


class lindenmayer:
    def __init__( self ):
        self.str = ''
        self.prod = {'[':'[','f':'f',']':']','+':'+','-':'-'}
        self.SIZE = 10
        self.THETA = 90

    def addProd( self, let, pro ):
        self.prod[let]=pro

    def iterate( self, qty=1 ):
        for i in xrange(qty):
            self.str = ''.join([ self.prod[l] for l in self.str])
        print 'Done iterating'

    def expose( self, drawingarea, event ):
        drawable = drawingarea.window
        width = drawingarea.allocation.width
        height = drawingarea.allocation.height

        drawable.clear()

        ctx = cairo.Context()
        cairo.gtk.set_target_drawable(ctx, drawable)
        ctx.set_rgb_color(0, 0, 0)

        ctx.set_line_width(self.SIZE / 4)
        ctx.set_tolerance(0.1)
        ctx.set_line_join(cairo.LINE_JOIN_BEVEL)

        ctx.new_path()
        ctx.move_to(100,100)

        for c in self.str:
            if c == 'f': line(ctx, self.SIZE )
            if c == '+': rotate( ctx, +self.THETA )
            if c == '-': rotate( ctx, -self.THETA )
            if c == '[': ctx.save()
            if c == ']': ctx.restore()

        ctx.stroke()

def line(ctx, len):
    ctx.rel_line_to( 0, len )

def rotate(ctx, deg):
    ctx.rotate( 2*3.141592653589793*deg/360.0  )

def main():
    win = gtk.Window()
    win.connect('destroy', gtk.mainquit)
    win.set_title('Cairo Lindenmayer System')
    win.set_default_size(600, 600)

    drawingarea = gtk.DrawingArea()
    cls = lindenmayer()

    ################# SETUP LSYSTEM HERE ################

    ### Generic stuff ###

    cls.str = 'f'   # the starting string

    cls.SIZE = 5    # length of a line

    ##############################################
    ##############################################
    #### Uncomment the one you want to use... ####
    #### only one at a time right now!        ####
    ##############################################
    ##############################################

    ###### Kock Square Curve #######
    cls.addProd('f','f-f+f+f-f')
    cls.THETA = 90


    ###### Kock Snowflake ######

#    cls.addProd('f','f-f++f-f')
#    cls.THETA = 60


    ######## Peano Curve ########
#    cls.addProd('x', 'xfyfx+f+yfxfy-f-xfyfx')
#    cls.addProd('y', 'yfxfy-f-xfyfx+f+yfxfy')
#    cls.addProd('f', 'f')
#    cls.THETA = 90
#    cls.str = 'y'


    ###### the plant ######
    ## doesn't seem to work ... .save & .restore messed up ##

#    cls.addProd( 'f','f[+f]f[-f]f' )
#    cls.THETA = 25

    ####### the tree #########
    ## doesn't seem to work ... .save & .restore messed up ##

#    cls.addProd( 'f', 'ff+[+f-f-f]-[-f+f+f]' )
#    cls.THETA = 22


    ### times to iterate string rewriting ###
    #this grows QUICKLY, so start only inc by 1 each run!
    cls.iterate(4)

    ################ DONE SETUP ###############

    drawingarea.connect('expose_event', cls.expose)

    win.add(drawingarea)
    win.show_all()

    gtk.main()

if __name__ == '__main__':
    main()






More information about the cairo-commit mailing list