Code

8728ed7a37e413dc17b10c9eb930fa534af85b67
[inkscape.git] / share / extensions / rtree.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
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, pturtle, random
21 def rtree(turtle, size, min):
22         if size < min:
23                 return
24         turtle.fd(size)
25         turn = random.uniform(20, 40)
26         turtle.lt(turn)
27         rtree(turtle, size*random.uniform(0.5,0.9), min)
28         turtle.rt(turn)
29         turn = random.uniform(20, 40)
30         turtle.rt(turn)
31         rtree(turtle, size*random.uniform(0.5,0.9), min)
32         turtle.lt(turn)
33         turtle.bk(size)
35 class RTreeTurtle(inkex.Effect):
36         def __init__(self):
37                 inkex.Effect.__init__(self)
38                 self.OptionParser.add_option("-s", "--size",
39                                                 action="store", type="float", 
40                                                 dest="size", default=100.0,
41                                                 help="initial branch size")
42                 self.OptionParser.add_option("-m", "--minimum",
43                                                 action="store", type="float", 
44                                                 dest="minimum", default=4.0,
45                                                 help="minimum branch size")
46         def effect(self):
47                 new = self.document.createElement('svg:path')
48                 s = {'stroke-linejoin': 'miter', 'stroke-width': '1.0px', 
49                         'stroke-opacity': '1.0', 'fill-opacity': '1.0', 
50                         'stroke': '#000000', 'stroke-linecap': 'butt', 
51                         'fill': 'none'}
52                 new.setAttribute('style', simplestyle.formatStyle(s))
53                 t = pturtle.pTurtle()
54                 rtree(t, self.options.size, self.options.minimum)
55                 new.setAttribute('d', t.getPath())
56                 self.document.documentElement.appendChild(new)
58 e = RTreeTurtle()
59 e.affect()