~ubuntu-branches/debian/experimental/inkscape/experimental

« back to all changes in this revision

Viewing changes to share/extensions/fractalize.py

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-09-09 23:29:02 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080909232902-c50iujhk1w79u8e7
Tags: 0.46-2.1
* Non-maintainer upload.
* Add upstream patch fixing a crash in the open dialog
  in the zh_CN.utf8 locale. Closes: #487623.
  Thanks to Luca Bruno for the patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python 
 
2
'''
 
3
Copyright (C) 2005 Carsten Goetze c.goetze@tu-bs.de
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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 random, math, inkex, simplepath
 
20
 
 
21
def calculateSubdivision(x1,y1,x2,y2,smoothness):
 
22
    """ Calculate the vector from (x1,y1) to (x2,y2) """
 
23
    x3 = x2 - x1
 
24
    y3 = y2 - y1
 
25
    """ Calculate the point half-way between the two points """
 
26
    hx = x1 + x3/2
 
27
    hy = y1 + y3/2
 
28
    """ Calculate normalized vector perpendicular to the vector (x3,y3) """
 
29
    length = math.sqrt(x3*x3 + y3*y3)
 
30
    if length != 0:
 
31
      nx = -y3/length
 
32
      ny = x3/length
 
33
    else:
 
34
      nx = 1
 
35
      ny = 0
 
36
    """ Scale perpendicular vector by random factor """
 
37
    r = random.uniform(-length/(1+smoothness),length/(1+smoothness))
 
38
    nx = nx * r
 
39
    ny = ny * r
 
40
    """ add scaled perpendicular vector to the half-way point to get the final
 
41
        displaced subdivision point """
 
42
    x = hx + nx
 
43
    y = hy + ny
 
44
    return [x, y]
 
45
 
 
46
class PathFractalize(inkex.Effect):
 
47
    def __init__(self):
 
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")
 
57
    def effect(self):
 
58
        for id, node in self.selected.iteritems():
 
59
            if node.tag == inkex.addNS('path','svg'):
 
60
                d = node.get('d')
 
61
                p = simplepath.parsePath(d)
 
62
                
 
63
                a = []
 
64
                first = 1
 
65
                for cmd,params in p:
 
66
                    if cmd != 'Z':
 
67
                        if first == 1:
 
68
                            x1 = params[-2]
 
69
                            y1 = params[-1]
 
70
                            a.append(['M',params[-2:]])
 
71
                            first = 2
 
72
                        else :
 
73
                            x2 = params[-2]
 
74
                            y2 = params[-1]
 
75
                            self.fractalize(a,x1,y1,x2,y2,self.options.subdivs,self.options.smooth)
 
76
                            x1 = x2
 
77
                            y1 = y2
 
78
                            a.append(['L',params[-2:]])
 
79
 
 
80
                node.set('d', simplepath.formatPath(a))
 
81
 
 
82
    def fractalize(self,a,x1,y1,x2,y2,s,f):
 
83
        subdivPoint = calculateSubdivision(x1,y1,x2,y2,f)
 
84
        
 
85
        if s > 0 :
 
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)
 
91
             
 
92
e = PathFractalize()
 
93
e.affect()
 
94