Code

97e9aed41f381d25e4842f93bfc3b2dfcc6df324
[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):
22         r = random.uniform(0.0,r)
23         a = random.uniform(0.0,2*math.pi)
24         x += math.cos(a)*r
25         y += math.sin(a)*r
26         return [x, y]
28 class RadiusRandomize(inkex.Effect):
29         def __init__(self):
30                 inkex.Effect.__init__(self)
31                 self.OptionParser.add_option("-r", "--radius",
32                                                 action="store", type="float", 
33                                                 dest="radius", default=10.0,
34                                                 help="Randomly move control and end points in this radius")
35                 self.OptionParser.add_option("-c", "--ctrl",
36                                                 action="store", type="inkbool", 
37                                                 dest="ctrl", default=True,
38                                                 help="Randomize control points")
39                 self.OptionParser.add_option("-e", "--end",
40                                                 action="store", type="inkbool", 
41                                                 dest="end", default=True,
42                                                 help="Randomize nodes")
43         def effect(self):
44                 for id, node in self.selected.iteritems():
45                         if node.tagName == 'path':
46                                 d = node.attributes.getNamedItem('d')
47                                 p = cubicsuperpath.parsePath(d.value)
48                                 for subpath in p:
49                                         for csp in subpath:
50                                                 if self.options.end:
51                                                         delta=randomize([0,0], self.options.radius)
52                                                         csp[0][0]+=delta[0] 
53                                                         csp[0][1]+=delta[1] 
54                                                         csp[1][0]+=delta[0] 
55                                                         csp[1][1]+=delta[1] 
56                                                         csp[2][0]+=delta[0] 
57                                                         csp[2][1]+=delta[1] 
58                                                 if self.options.ctrl:
59                                                         csp[0]=randomize(csp[0], self.options.radius)
60                                                         csp[2]=randomize(csp[2], self.options.radius)
61                                 d.value = cubicsuperpath.formatPath(p)
63 e = RadiusRandomize()
64 e.affect()