3
Copyright (C) 2005 Carsten Goetze c.goetze@tu-bs.de
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
19
import random, math, inkex, simplepath
21
def calculateSubdivision(x1,y1,x2,y2,smoothness):
22
""" Calculate the vector from (x1,y1) to (x2,y2) """
25
""" Calculate the point half-way between the two points """
28
""" Calculate normalized vector perpendicular to the vector (x3,y3) """
29
length = math.sqrt(x3*x3 + y3*y3)
36
""" Scale perpendicular vector by random factor """
37
r = random.uniform(-length/(1+smoothness),length/(1+smoothness))
40
""" add scaled perpendicular vector to the half-way point to get the final
41
displaced subdivision point """
46
class PathFractalize(inkex.Effect):
48
inkex.Effect.__init__(self)
49
self.OptionParser.add_option("-s", "--subdivs",
50
action="store", type="int",
51
dest="subdivs", default="6",
52
help="Number of subdivisons")
53
self.OptionParser.add_option("-f", "--smooth",
54
action="store", type="float",
55
dest="smooth", default="4.0",
56
help="Smoothness of the subdivision")
58
for id, node in self.selected.iteritems():
59
if node.tag == inkex.addNS('path','svg'):
61
p = simplepath.parsePath(d)
70
a.append(['M',params[-2:]])
75
self.fractalize(a,x1,y1,x2,y2,self.options.subdivs,self.options.smooth)
78
a.append(['L',params[-2:]])
80
node.set('d', simplepath.formatPath(a))
82
def fractalize(self,a,x1,y1,x2,y2,s,f):
83
subdivPoint = calculateSubdivision(x1,y1,x2,y2,f)
86
""" recursively subdivide the segment left of the subdivision point """
87
self.fractalize(a,x1,y1,subdivPoint[-2],subdivPoint[-1],s-1,f)
88
a.append(['L',subdivPoint])
89
""" recursively subdivide the segment right of the subdivision point """
90
self.fractalize(a,subdivPoint[-2],subdivPoint[-1],x2,y2,s-1,f)