1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2007 Joel Holdsworth joel@airwebreathe.org.uk
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 inkex, simplestyle, math
21 class Spirograph(inkex.Effect):
22 def __init__(self):
23 inkex.Effect.__init__(self)
24 self.OptionParser.add_option("-R", "--primaryr",
25 action="store", type="float",
26 dest="primaryr", default=60.0,
27 help="The radius of the outer gear")
28 self.OptionParser.add_option("-r", "--secondaryr",
29 action="store", type="float",
30 dest="secondaryr", default=100.0,
31 help="The radius of the inner gear")
32 self.OptionParser.add_option("-d", "--penr",
33 action="store", type="float",
34 dest="penr", default=50.0,
35 help="The distance of the pen from the inner gear")
36 self.OptionParser.add_option("-p", "--gearplacement",
37 action="store", type="string",
38 dest="gearplacement", default=50.0,
39 help="Selects whether the gear is inside or outside the ring")
40 self.OptionParser.add_option("-a", "--rotation",
41 action="store", type="float",
42 dest="rotation", default=0.0,
43 help="The number of degrees to rotate the image by")
44 self.OptionParser.add_option("-q", "--quality",
45 action="store", type="int",
46 dest="quality", default=16,
47 help="The quality of the calculated output")
48 def effect(self):
50 if self.options.secondaryr == 0:
51 return
52 if self.options.quality == 0:
53 return
55 if(self.options.gearplacement.strip(' ').lower().startswith('outside')):
56 a = self.options.primaryr + self.options.secondaryr
57 flip = -1
58 else:
59 a = self.options.primaryr - self.options.secondaryr
60 flip = 1
62 ratio = a / self.options.secondaryr
63 if ratio == 0:
64 return
65 scale = 2 * math.pi / (ratio * self.options.quality)
67 rotation = - math.pi * self.options.rotation / 180;
69 new = inkex.etree.Element(inkex.addNS('path','svg'))
70 s = { 'stroke': '#000000', 'fill': 'none' }
71 new.set('style', simplestyle.formatStyle(s))
73 pathString = ''
74 maxPointCount = 1000
76 for i in range(maxPointCount):
78 theta = i * scale
80 x = a * math.cos(theta + rotation) + \
81 self.options.penr * math.cos(ratio * theta + rotation) * flip + \
82 self.view_center[0]
83 y = a * math.sin(theta + rotation) - \
84 self.options.penr * math.sin(ratio * theta + rotation) + \
85 self.view_center[1]
87 dx = (-a * math.sin(theta + rotation) - \
88 ratio * self.options.penr * math.sin(ratio * theta + rotation) * flip) * scale / 3
89 dy = (a * math.cos(theta + rotation) - \
90 ratio * self.options.penr * math.cos(ratio * theta + rotation)) * scale / 3
92 if i <= 0:
93 pathString += 'M ' + str(x) + ',' + str(y) + ' C ' + str(x + dx) + ',' + str(y + dy) + ' '
94 else:
95 pathString += str(x - dx) + ',' + str(y - dy) + ' ' + str(x) + ',' + str(y)
97 if math.fmod(i / ratio, self.options.quality) == 0 and i % self.options.quality == 0:
98 pathString += 'Z'
99 break
100 else:
101 if i == maxPointCount - 1:
102 pass # we reached the allowed maximum of points, stop here
103 else:
104 pathString += ' C ' + str(x + dx) + ',' + str(y + dy) + ' '
107 new.set('d', pathString)
108 self.current_layer.append(new)
110 e = Spirograph()
111 e.affect()