home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / share / extensions / radiusrand.py < prev    next >
Text File  |  2011-07-08  |  4KB  |  81 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 random, math, inkex, cubicsuperpath
  20.  
  21. def randomize((x, y), rx, ry, norm):
  22.     if norm:
  23.         r = abs(random.normalvariate(0.0,0.5*max(rx, ry)))
  24.     else:
  25.         r = random.uniform(0.0,max(rx, ry))
  26.     a = random.uniform(0.0,2*math.pi)
  27.     x += math.cos(a)*rx
  28.     y += math.sin(a)*ry
  29.     return [x, y]
  30.  
  31. class RadiusRandomize(inkex.Effect):
  32.     def __init__(self):
  33.         inkex.Effect.__init__(self)
  34.         self.OptionParser.add_option("--title")
  35.         self.OptionParser.add_option("-x", "--radiusx",
  36.                         action="store", type="float", 
  37.                         dest="radiusx", default=10.0,
  38.                         help="Randomly move nodes and handles within this radius, X")
  39.         self.OptionParser.add_option("-y", "--radiusy",
  40.                         action="store", type="float", 
  41.                         dest="radiusy", default=10.0,
  42.                         help="Randomly move nodes and handles within this radius, Y")
  43.         self.OptionParser.add_option("-c", "--ctrl",
  44.                         action="store", type="inkbool", 
  45.                         dest="ctrl", default=True,
  46.                         help="Randomize control points")
  47.         self.OptionParser.add_option("-e", "--end",
  48.                         action="store", type="inkbool", 
  49.                         dest="end", default=True,
  50.                         help="Randomize nodes")
  51.         self.OptionParser.add_option("-n", "--norm",
  52.                         action="store", type="inkbool", 
  53.                         dest="norm", default=True,
  54.                         help="Use normal distribution")
  55.     def effect(self):
  56.         for id, node in self.selected.iteritems():
  57.             if node.tag == inkex.addNS('path','svg'):
  58.                 d = node.get('d')
  59.                 p = cubicsuperpath.parsePath(d)
  60.                 for subpath in p:
  61.                     for csp in subpath:
  62.                         if self.options.end:
  63.                             delta=randomize([0,0], self.options.radiusx, self.options.radiusy, self.options.norm)
  64.                             csp[0][0]+=delta[0] 
  65.                             csp[0][1]+=delta[1] 
  66.                             csp[1][0]+=delta[0] 
  67.                             csp[1][1]+=delta[1] 
  68.                             csp[2][0]+=delta[0] 
  69.                             csp[2][1]+=delta[1] 
  70.                         if self.options.ctrl:
  71.                             csp[0]=randomize(csp[0], self.options.radiusx, self.options.radiusy, self.options.norm)
  72.                             csp[2]=randomize(csp[2], self.options.radiusx, self.options.radiusy, self.options.norm)
  73.                 node.set('d',cubicsuperpath.formatPath(p))
  74.  
  75. if __name__ == '__main__':
  76.     e = RadiusRandomize()
  77.     e.affect()
  78.  
  79.  
  80. # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
  81.