1#!/usr/local/bin/python3.8
2# coding=utf-8
3#
4# Copyright (C) 2005 Aaron Spike, aaron@ekips.org
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19#
20"""Whirl path extension (modify path)"""
21
22import math
23import inkex
24
25class Whirl(inkex.EffectExtension):
26    """Modify a path by twisting the nodes around a point"""
27    def add_arguments(self, pars):
28        pars.add_argument("-t", "--whirl", type=float,\
29            default=1.0, help="amount of whirl")
30        pars.add_argument("-r", "--rotation", type=inkex.Boolean,\
31            default=True, help="direction of rotation")
32
33    def effect(self):
34        view_center = self.svg.namedview.center
35        rotation = 1 if self.options.rotation else -1
36        whirl = self.options.whirl / 1000
37        for node in self.svg.selection.filter(inkex.PathElement):
38            self.whirl_node(view_center, rotation, whirl, node)
39
40    @staticmethod
41    def whirl_node(center, direction, ammount, node):
42        """Apply a whirl to a path given the center, direction and amount"""
43        path = node.path.to_superpath()
44        for sub in path:
45            for csp in sub:
46                for point in csp:
47                    point[0] -= center[0]
48                    point[1] -= center[1]
49                    dist = math.sqrt((point[0] ** 2) + (point[1] ** 2))
50                    if dist != 0:
51                        art = direction * dist * ammount
52                        theta = math.atan2(point[1], point[0]) + art
53                        point[0] = (dist * math.cos(theta))
54                        point[1] = (dist * math.sin(theta))
55                    point[0] += center[0]
56                    point[1] += center[1]
57        node.path = path
58
59
60if __name__ == '__main__':
61    Whirl().run()
62