File: spirograph.py

package info (click to toggle)
inkscape 0.47.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 122,072 kB
  • ctags: 46,938
  • sloc: cpp: 349,791; ansic: 31,306; python: 14,868; xml: 6,613; sh: 5,041; makefile: 1,773; perl: 1,486; asm: 675; ruby: 148
file content (116 lines) | stat: -rw-r--r-- 4,753 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/env python
'''
Copyright (C) 2007 Joel Holdsworth joel@airwebreathe.org.uk

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''
import inkex, simplestyle, math

class Spirograph(inkex.Effect):
    def __init__(self):
        inkex.Effect.__init__(self)
        self.OptionParser.add_option("-R", "--primaryr",
                        action="store", type="float",
                        dest="primaryr", default=60.0,
                        help="The radius of the outer gear")
        self.OptionParser.add_option("-r", "--secondaryr",
                        action="store", type="float",
                        dest="secondaryr", default=100.0,
                        help="The radius of the inner gear")
        self.OptionParser.add_option("-d", "--penr",
                        action="store", type="float",
                        dest="penr", default=50.0,
                        help="The distance of the pen from the inner gear")
        self.OptionParser.add_option("-p", "--gearplacement",
                        action="store", type="string",
                        dest="gearplacement", default=50.0,
                        help="Selects whether the gear is inside or outside the ring")
        self.OptionParser.add_option("-a", "--rotation",
                        action="store", type="float",
                        dest="rotation", default=0.0,
                        help="The number of degrees to rotate the image by")
        self.OptionParser.add_option("-q", "--quality",
                        action="store", type="int",
                        dest="quality", default=16,
                        help="The quality of the calculated output")

    def effect(self):

        if self.options.secondaryr == 0:
            return
        if self.options.quality == 0:
            return

        if(self.options.gearplacement.strip(' ').lower().startswith('outside')):
            a = self.options.primaryr + self.options.secondaryr
            flip = -1
        else:
            a = self.options.primaryr - self.options.secondaryr
            flip = 1

        ratio = a / self.options.secondaryr
        if ratio == 0:
            return
        scale = 2 * math.pi / (ratio * self.options.quality)

        rotation = - math.pi * self.options.rotation / 180;

        new = inkex.etree.Element(inkex.addNS('path','svg'))
        s = { 'stroke': '#000000', 'fill': 'none' }
        new.set('style', simplestyle.formatStyle(s))

        pathString = ''
        maxPointCount = 1000

        for i in range(maxPointCount):

            theta = i * scale

            x = a * math.cos(theta + rotation) + \
                self.options.penr * math.cos(ratio * theta + rotation) * flip + \
                self.view_center[0]
            y = a * math.sin(theta + rotation) - \
                self.options.penr * math.sin(ratio * theta + rotation) + \
                self.view_center[1]

            dx = (-a * math.sin(theta + rotation) - \
                ratio * self.options.penr * math.sin(ratio * theta + rotation) * flip) * scale / 3
            dy = (a * math.cos(theta + rotation) - \
                ratio * self.options.penr * math.cos(ratio * theta + rotation)) * scale / 3

            if i <= 0:
                pathString += 'M ' + str(x) + ',' + str(y) + ' C ' + str(x + dx) + ',' + str(y + dy) + ' '
            else:
                pathString += str(x - dx) + ',' + str(y - dy) + ' ' + str(x) + ',' + str(y)

                if math.fmod(i / ratio, self.options.quality) == 0 and i % self.options.quality == 0:
                    pathString += 'Z'
                    break
                else:
                    if i == maxPointCount - 1:
                        pass # we reached the allowed maximum of points, stop here
                    else:
                        pathString += ' C ' + str(x + dx) + ',' + str(y + dy) + ' '


        new.set('d', pathString)
        self.current_layer.append(new)

if __name__ == '__main__':
    e = Spirograph()
    e.affect()


# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99