5912201d2b4562a75dcb74667c4b607feaada4f0
1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
19 import inkex, simplestyle, pturtle, random
21 def stripme(s):
22 return s.strip()
24 class LSystem(inkex.Effect):
25 def __init__(self):
26 inkex.Effect.__init__(self)
27 self.OptionParser.add_option("-o", "--order",
28 action="store", type="int",
29 dest="order", default=3,
30 help="number of iteration")
31 self.OptionParser.add_option("-l", "--langle",
32 action="store", type="float",
33 dest="langle", default=16.0,
34 help="angle for turning left")
35 self.OptionParser.add_option("-r", "--rangle",
36 action="store", type="float",
37 dest="rangle", default=16.0,
38 help="angle for turning right")
39 self.OptionParser.add_option("-s", "--step",
40 action="store", type="float",
41 dest="step", default=25.0,
42 help="step size")
43 self.OptionParser.add_option("-p", "--randomizestep",
44 action="store", type="float",
45 dest="randomizestep", default=0.0,
46 help="randomize step")
47 self.OptionParser.add_option("-z", "--randomizeangle",
48 action="store", type="float",
49 dest="randomizeangle", default=0.0,
50 help="randomize angle")
51 self.OptionParser.add_option("-x", "--axiom",
52 action="store", type="string",
53 dest="axiom", default="++F",
54 help="initial state of system")
55 self.OptionParser.add_option("-u", "--rules",
56 action="store", type="string",
57 dest="rules", default="F=FF-[-F+F+F]+[+F-F-F]",
58 help="replacement rules")
59 self.stack = []
60 self.turtle = pturtle.pTurtle()
61 def iterate(self):
62 self.rules = dict([map(stripme, i.split("=")) for i in self.options.rules.upper().split(";") if i.count("=")==1])
63 string = self.__recurse(self.options.axiom.upper(),0)
64 self.__compose_path(string)
65 return self.turtle.getPath()
66 def __compose_path(self, string):
67 self.turtle.pu()
68 self.turtle.setpos(self.view_center)
69 self.turtle.pd()
70 for c in string:
71 if c in 'ABCDEF':
72 self.turtle.pd()
73 self.turtle.fd(self.options.step * (random.normalvariate(1.0, 0.01 * self.options.randomizestep)))
74 elif c in 'GHIJKL':
75 self.turtle.pu()
76 self.turtle.fd(self.options.step * (random.normalvariate(1.0, 0.01 * self.options.randomizestep)))
77 elif c == '+':
78 self.turtle.lt(self.options.langle * (random.normalvariate(1.0, 0.01 * self.options.randomizeangle)))
79 elif c == '-':
80 self.turtle.rt(self.options.rangle * (random.normalvariate(1.0, 0.01 * self.options.randomizeangle)))
81 elif c == '|':
82 self.turtle.lt(180)
83 elif c == '[':
84 self.stack.append([self.turtle.getpos(), self.turtle.getheading()])
85 elif c == ']':
86 self.turtle.pu()
87 pos,heading = self.stack.pop()
88 self.turtle.setpos(pos)
89 self.turtle.setheading(heading)
91 def __recurse(self,rule,level):
92 level_string = ''
93 for c in rule:
94 if level < self.options.order:
95 try:
96 level_string = level_string + self.__recurse(self.rules[c],level+1)
97 except KeyError:
98 level_string = level_string + c
99 else:
100 level_string = level_string + c
101 return level_string
103 def effect(self):
104 s = {'stroke-linejoin': 'miter', 'stroke-width': '1.0px',
105 'stroke-opacity': '1.0', 'fill-opacity': '1.0',
106 'stroke': '#000000', 'stroke-linecap': 'butt',
107 'fill': 'none'}
108 attribs = {'style':simplestyle.formatStyle(s),'d':self.iterate()}
109 inkex.etree.SubElement(self.current_layer,inkex.addNS('path','svg'),attribs)
111 e = LSystem()
112 e.affect()