1 #!/usr/bin/env python \r
2 '''\r
3 Copyright (C) 2005 Carsten Goetze c.goetze@tu-bs.de\r
4 \r
5 This program is free software; you can redistribute it and/or modify\r
6 it under the terms of the GNU General Public License as published by\r
7 the Free Software Foundation; either version 2 of the License, or\r
8 (at your option) any later version.\r
9 \r
10 This program is distributed in the hope that it will be useful,\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
13 GNU General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU General Public License\r
16 along with this program; if not, write to the Free Software\r
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
18 '''\r
19 import random, math, inkex, simplepath\r
20 \r
21 def calculateSubdivision(x1,y1,x2,y2,smoothness):\r
22 """ Calculate the vector from (x1,y1) to (x2,y2) """\r
23 x3 = x2 - x1\r
24 y3 = y2 - y1\r
25 """ Calculate the point half-way between the two points """\r
26 hx = x1 + x3/2\r
27 hy = y1 + y3/2\r
28 """ Calculate normalized vector perpendicular to the vector (x3,y3) """\r
29 length = math.sqrt(x3*x3 + y3*y3)\r
30 if length != 0:\r
31 nx = -y3/length\r
32 ny = x3/length\r
33 else:\r
34 nx = 1\r
35 ny = 0\r
36 """ Scale perpendicular vector by random factor """\r
37 r = random.uniform(-length/(1+smoothness),length/(1+smoothness))\r
38 nx = nx * r\r
39 ny = ny * r\r
40 """ add scaled perpendicular vector to the half-way point to get the final\r
41 displaced subdivision point """\r
42 x = hx + nx\r
43 y = hy + ny\r
44 return [x, y]\r
45 \r
46 class PathFractalize(inkex.Effect):\r
47 def __init__(self):\r
48 inkex.Effect.__init__(self)\r
49 self.OptionParser.add_option("-s", "--subdivs",\r
50 action="store", type="int", \r
51 dest="subdivs", default="6",\r
52 help="Number of subdivisons")\r
53 self.OptionParser.add_option("-f", "--smooth",\r
54 action="store", type="float", \r
55 dest="smooth", default="4.0",\r
56 help="Smoothness of the subdivision")\r
57 def effect(self):\r
58 for id, node in self.selected.iteritems():\r
59 if node.tag == inkex.addNS('path','svg'):\r
60 d = node.get('d')\r
61 p = simplepath.parsePath(d)\r
62 \r
63 a = []\r
64 first = 1\r
65 for cmd,params in p:\r
66 if cmd != 'Z':\r
67 if first == 1:\r
68 x1 = params[-2]\r
69 y1 = params[-1]\r
70 a.append(['M',params[-2:]])\r
71 first = 2\r
72 else :\r
73 x2 = params[-2]\r
74 y2 = params[-1]\r
75 self.fractalize(a,x1,y1,x2,y2,self.options.subdivs,self.options.smooth)\r
76 x1 = x2\r
77 y1 = y2\r
78 a.append(['L',params[-2:]])\r
79 \r
80 node.set('d', simplepath.formatPath(a))\r
81 \r
82 def fractalize(self,a,x1,y1,x2,y2,s,f):\r
83 subdivPoint = calculateSubdivision(x1,y1,x2,y2,f)\r
84 \r
85 if s > 0 :\r
86 """ recursively subdivide the segment left of the subdivision point """\r
87 self.fractalize(a,x1,y1,subdivPoint[-2],subdivPoint[-1],s-1,f)\r
88 a.append(['L',subdivPoint])\r
89 """ recursively subdivide the segment right of the subdivision point """\r
90 self.fractalize(a,subdivPoint[-2],subdivPoint[-1],x2,y2,s-1,f)\r
91 \r
92 e = PathFractalize()\r
93 e.affect()\r
94 \r