Code

fix 198404
[inkscape.git] / share / extensions / radiusrand.py
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 random, math, inkex, cubicsuperpath
21 def randomize((x, y), r, norm):
22     if norm:
23         r = abs(random.normalvariate(0.0,0.5*r))
24     else:
25         r = random.uniform(0.0,r)
26     a = random.uniform(0.0,2*math.pi)
27     x += math.cos(a)*r
28     y += math.sin(a)*r
29     return [x, y]
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("-r", "--radius",
36                         action="store", type="float", 
37                         dest="radius", default=10.0,
38                         help="Randomly move control and end points in this radius")
39         self.OptionParser.add_option("-c", "--ctrl",
40                         action="store", type="inkbool", 
41                         dest="ctrl", default=True,
42                         help="Randomize control points")
43         self.OptionParser.add_option("-e", "--end",
44                         action="store", type="inkbool", 
45                         dest="end", default=True,
46                         help="Randomize nodes")
47         self.OptionParser.add_option("-n", "--norm",
48                         action="store", type="inkbool", 
49                         dest="norm", default=True,
50                         help="Use normal distribution")
51     def effect(self):
52         for id, node in self.selected.iteritems():
53             if node.tag == inkex.addNS('path','svg'):
54                 d = node.get('d')
55                 p = cubicsuperpath.parsePath(d)
56                 for subpath in p:
57                     for csp in subpath:
58                         if self.options.end:
59                             delta=randomize([0,0], self.options.radius, self.options.norm)
60                             csp[0][0]+=delta[0] 
61                             csp[0][1]+=delta[1] 
62                             csp[1][0]+=delta[0] 
63                             csp[1][1]+=delta[1] 
64                             csp[2][0]+=delta[0] 
65                             csp[2][1]+=delta[1] 
66                         if self.options.ctrl:
67                             csp[0]=randomize(csp[0], self.options.radius, self.options.norm)
68                             csp[2]=randomize(csp[2], self.options.radius, self.options.norm)
69                 node.set('d',cubicsuperpath.formatPath(p))
71 e = RadiusRandomize()
72 e.affect()