home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June (Extra) / CHIP 2006-06.3.iso / program / opensource / Inkscape-0.43-2.win32.exe / share / extensions / lindenmayer.py < prev    next >
Encoding:
Python Source  |  2005-05-24  |  3.0 KB  |  89 lines

  1. #!/usr/bin/env python 
  2. '''
  3. Copyright (C) 2005 Aaron Spike, aaron@ekips.org
  4.  
  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.
  9.  
  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.
  14.  
  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
  20.  
  21. class LSystem(inkex.Effect):
  22.     def __init__(self):
  23.         inkex.Effect.__init__(self)
  24.         self.OptionParser.add_option("-o", "--order",
  25.                         action="store", type="int", 
  26.                         dest="order", default=3,
  27.                         help="number of iteration")
  28.         self.OptionParser.add_option("-a", "--angle",
  29.                         action="store", type="float", 
  30.                         dest="angle", default=16.0,
  31.                         help="angle for turn commands")
  32.         self.OptionParser.add_option("-s", "--step",
  33.                         action="store", type="float", 
  34.                         dest="step", default=25.0,
  35.                         help="step size")
  36.         self.OptionParser.add_option("-x", "--axiom",
  37.                         action="store", type="string", 
  38.                         dest="axiom", default="++F",
  39.                         help="initial state of system")
  40.         self.OptionParser.add_option("-r", "--rules",
  41.                         action="store", type="string", 
  42.                         dest="rules", default="F=FF-[-F+F+F]+[+F-F-F]",
  43.                         help="replacement rules")
  44.         self.stack = []
  45.         self.turtle = pturtle.pTurtle()
  46.     def iterate(self):
  47.         self.rules = dict([i.split("=") for i in self.options.rules.upper().split(";") if i.count("=")==1])
  48.         self.__recurse(self.options.axiom.upper(),0)
  49.         return self.turtle.getPath()
  50.     def __recurse(self,rule,level):
  51.         for c in rule:
  52.             if level < self.options.order:
  53.                 try:
  54.                     self.__recurse(self.rules[c],level+1)
  55.                 except KeyError:
  56.                     pass
  57.             
  58.             if c == 'F':
  59.                 self.turtle.pd()
  60.                 self.turtle.fd(self.options.step)
  61.             elif c == 'G':
  62.                 self.turtle.pu()
  63.                 self.turtle.fd(self.options.step)
  64.             elif c == '+':
  65.                 self.turtle.lt(self.options.angle)
  66.             elif c == '-':
  67.                 self.turtle.rt(self.options.angle)
  68.             elif c == '|':
  69.                 self.turtle.lt(180)
  70.             elif c == '[':
  71.                 self.stack.append([self.turtle.getpos(), self.turtle.getheading()])
  72.             elif c == ']':
  73.                 self.turtle.pu()
  74.                 pos,heading = self.stack.pop()
  75.                 self.turtle.setpos(pos)
  76.                 self.turtle.setheading(heading)
  77.     def effect(self):
  78.         new = self.document.createElement('svg:path')
  79.         s = {'stroke-linejoin': 'miter', 'stroke-width': '1.0px', 
  80.             'stroke-opacity': '1.0', 'fill-opacity': '1.0', 
  81.             'stroke': '#000000', 'stroke-linecap': 'butt', 
  82.             'fill': 'none'}
  83.         new.setAttribute('style', simplestyle.formatStyle(s))
  84.         new.setAttribute('d', self.iterate())
  85.         self.document.documentElement.appendChild(new)
  86.  
  87. e = LSystem()
  88. e.affect()
  89.