Code

Super duper mega (fun!) commit: replaced encoding=utf-8 with fileencoding=utf-8 in...
[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), 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]
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         self.OptionParser.add_option("--tab",
56                         action="store", type="string",
57                         dest="tab",
58                         help="The selected UI-tab when OK was pressed")
60     def effect(self):
61         for id, node in self.selected.iteritems():
62             if node.tag == inkex.addNS('path','svg'):
63                 d = node.get('d')
64                 p = cubicsuperpath.parsePath(d)
65                 for subpath in p:
66                     for csp in subpath:
67                         if self.options.end:
68                             delta=randomize([0,0], self.options.radiusx, self.options.radiusy, self.options.norm)
69                             csp[0][0]+=delta[0] 
70                             csp[0][1]+=delta[1] 
71                             csp[1][0]+=delta[0] 
72                             csp[1][1]+=delta[1] 
73                             csp[2][0]+=delta[0] 
74                             csp[2][1]+=delta[1] 
75                         if self.options.ctrl:
76                             csp[0]=randomize(csp[0], self.options.radiusx, self.options.radiusy, self.options.norm)
77                             csp[2]=randomize(csp[2], self.options.radiusx, self.options.radiusy, self.options.norm)
78                 node.set('d',cubicsuperpath.formatPath(p))
80 if __name__ == '__main__':
81     e = RadiusRandomize()
82     e.affect()
85 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99