Code

redesign the dialog into 3 tabs, rewrite most texts, increase preview size
[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("-r", "--radius",
35                         action="store", type="float", 
36                         dest="radius", default=10.0,
37                         help="Randomly move control and end points in this radius")
38         self.OptionParser.add_option("-c", "--ctrl",
39                         action="store", type="inkbool", 
40                         dest="ctrl", default=True,
41                         help="Randomize control points")
42         self.OptionParser.add_option("-e", "--end",
43                         action="store", type="inkbool", 
44                         dest="end", default=True,
45                         help="Randomize nodes")
46         self.OptionParser.add_option("-n", "--norm",
47                         action="store", type="inkbool", 
48                         dest="norm", default=True,
49                         help="Use normal distribution")
50     def effect(self):
51         for id, node in self.selected.iteritems():
52             if node.tagName == 'path':
53                 d = node.attributes.getNamedItem('d')
54                 p = cubicsuperpath.parsePath(d.value)
55                 for subpath in p:
56                     for csp in subpath:
57                         if self.options.end:
58                             delta=randomize([0,0], self.options.radius, self.options.norm)
59                             csp[0][0]+=delta[0] 
60                             csp[0][1]+=delta[1] 
61                             csp[1][0]+=delta[0] 
62                             csp[1][1]+=delta[1] 
63                             csp[2][0]+=delta[0] 
64                             csp[2][1]+=delta[1] 
65                         if self.options.ctrl:
66                             csp[0]=randomize(csp[0], self.options.radius, self.options.norm)
67                             csp[2]=randomize(csp[2], self.options.radius, self.options.norm)
68                 d.value = cubicsuperpath.formatPath(p)
70 e = RadiusRandomize()
71 e.affect()