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
115
116
117
118
119
120
121
122
123
|
#!/usr/bin/env python
import cairo
import gtk
# Copyright 2003 Jesse Andrews (jdandr2@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
x, y, width, height = drawingarea.allocation
ctx = drawable.cairo_create()
ctx.set_source_rgb(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 lin_setup():
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 ###############
return cls
def main():
win = gtk.Window()
win.connect('destroy', lambda x: gtk.main_quit())
win.set_title('cairo Lindenmayer System')
win.set_default_size(600, 600)
cls = lin_setup()
drawingarea = gtk.DrawingArea()
win.add(drawingarea)
drawingarea.connect('expose_event', cls.expose)
win.show_all()
gtk.main()
if __name__ == '__main__':
main()
|