File: lsystem.py

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (222 lines) | stat: -rw-r--r-- 7,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python

"""
A fractal display program using the Lindenmayer rule system to describe the fractal.
"""
__version__ = "$Revision: 1.6 $"
__date__ = "$Date: 2005/12/13 11:13:23 $"

from PythonCard import clipboard, dialog, graphic, model
from PythonCard.turtle import AbstractTurtle, BitmapTurtle
import wx
import os
import time

class LSystem(model.Background):

    def on_initialize(self, event):
        self.filename = None
        self.on_iterations_select(None)
        self.on_angle_select(None)
    
    def on_iterations_select(self, event):
        self.components.iterationDisplay.text = \
            str(self.components.iterations.value)
    
    def on_angle_select(self, event):
        self.components.angleDisplay.text = \
            str(self.components.angle.value)
    
    def on_angleDisplay_textUpdate(self, event):
        self.components.angle.value = \
            int(self.components.angleDisplay.text)
        
    def on_Render_command(self, event):
        self.statusBar.text = "Drawing, please wait..."
        starttime = time.time()

        fractalString = self.expand(
                self.components.scriptField.text,
                self.components.iterations.value)
        borderWidth = 5.0
        angle = self.components.angle.value
        self.components.bufOff.autoRefresh = False
        bounds = drawAbstractFractal(
            fractalString,
            1,
            angle,
            (0,0))
        width, height = self.components.bufOff.size
        scale = min(
            (width - 2 * borderWidth) / (bounds[2]-bounds[0]),
            (height - 2 * borderWidth) / (bounds[3]-bounds[1]))
        startPos = (bounds[0] * -scale + borderWidth,
                    bounds[1] * -scale + borderWidth) 
        self.components.bufOff.autoRefresh = True
        self.drawFractal(
            fractalString,
            'blue',
            scale,
            angle,
            startPos)

        stoptime = time.time()
        self.statusBar.text = "Draw time: %.2f seconds" % (stoptime - starttime)

    def drawFractal(self, aFractalString, color, legLength, angle, startPos):
        self.components.bufOff.clear()
        t = BitmapTurtle(self.components.bufOff)
        t.color(color)
        t.lt(90)
        t.moveTo(startPos[0], startPos[1])
        for char in aFractalString:
            if char=='F':
                t.fd(legLength)
            elif char=='B':
                t.bk(legLength)
            elif char=='+':
                t.rt(angle)
            elif char=='-':
                t.lt(angle)
            elif char=='[':
                t.push()
            elif char==']':
                t.pop()
            
    def expand(self, fractalRules, iterations):
        lines = fractalRules.upper().split('\n')
        rules = {}
        for rule in lines:
            name, value = rule.split('=')
            assert(name=='AXIOM' or len(name)==1)
            rules[name] = value
        ret = rules['AXIOM']
        for i in range(iterations):
            l = list(ret)
            for pos, char in enumerate(l):
                repl = rules.get(char, None)
                if repl:
                    l[pos] = repl
            ret = ''.join(l)
        return ret
        
    def initSizers(self):
        sizer1 = wx.BoxSizer(wx.VERTICAL)
        comp = self.components
        flags = wx.LEFT | wx.RIGHT | wx.BOTTOM | wx.ALIGN_BOTTOM
        # Mac wxButton needs 7 pixels on bottom and right
        macPadding = 7
        sizer1.Add(comp.btnColor, 0, flags, macPadding)
        sizer1.Add(comp.bufOff, 1, wx.EXPAND)
        
        sizer1.Fit(self)
        sizer1.SetSizeHints(self)
        self.panel.SetSizer(sizer1)
        self.panel.SetAutoLayout(1)
        self.panel.Layout()

    def on_btnColor_mouseClick(self, event):
        result = dialog.colorDialog(self)
        if result.accepted:
            self.components.bufOff.foregroundColor = result.color
            event.target.backgroundColor = result.color

    def openFile(self):
        result = dialog.openFileDialog(None, "Import which file?")
        if result.accepted:
            path = result.paths[0]
            os.chdir(os.path.dirname(path))
            self.filename = path
            bmp = graphic.Bitmap(self.filename)
            self.components.bufOff.drawBitmap(bmp, (0, 0))

    def on_menuFileOpen_select(self, event):
        self.openFile()

    def on_menuFileSaveAs_select(self, event):
        if self.filename is None:
            path = ''
            filename = ''
        else:
            path, filename = os.path.split(self.filename)
        result = dialog.saveFileDialog(None, "Save As", path, filename)
        if result.accepted:
            path = result.paths[0]
            fileType = graphic.bitmapType(path)
            print fileType, path
            bmp = self.components.bufOff.getBitmap()
            try:
                bmp.SaveFile(path, fileType)
                return True
            except IOError:
                return False
        else:
            return False

    def on_menuEditCopy_select(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable') and widget.canCopy():
            widget.copy()
        else:
            clipboard.setClipboard(self.components.bufOff.getBitmap())

    def on_menuEditPaste_select(self, event):
        widget = self.findFocus()
        if hasattr(widget, 'editable') and widget.canPaste():
            widget.paste()
        else:
            bmp = clipboard.getClipboard()
            if isinstance(bmp, wx.Bitmap):
                self.components.bufOff.drawBitmap(bmp)

    def on_editClear_command(self, event):
        self.components.bufOff.clear()

def drawAbstractFractal(aFractalString, legLength, angle, startPos):
    def recalcBounds(bounds, turtle):
        x, y = turtle.getXY()
        bounds[0] = min(bounds[0], x)
        bounds[1] = min(bounds[1], y)
        bounds[2] = max(bounds[2], x)
        bounds[3] = max(bounds[3], y)
        
    t = AbstractTurtle((1,1))
    t.lt(90)
    t.moveTo(startPos[0], startPos[1])
    bounds = list(startPos + startPos)
    for char in aFractalString:
        if char=='F':
            t.fd(legLength)
            recalcBounds(bounds, t)
        elif char=='B':
            t.bk(legLength)
            recalcBounds(bounds, t)
        elif char=='+':
            t.rt(angle)
        elif char=='-':
            t.lt(angle)
        elif char=='[':
            t.push()
        elif char==']':
            t.pop()
    return bounds

from PythonCard.tests import pyunit

class LSystemTests(pyunit.TestCase):
    def setUp(self):
        pass
    def tearDown(self):
        pass
    def testBoundsCheckBackwardMoves(self):
        bounds = drawAbstractFractal('BB+B', 1, 90, (10.0,20.0))
        # remember that down is positive in this coordinate system
        self.assertEqual([9.0, 20.0, 10.0, 22.0], bounds)
    def testBoundsCheckForwardMoves(self):
        bounds = drawAbstractFractal('FF-F', 1, 90, (10.0,20.0))
        # remember that down is positive in this coordinate system
        self.assertEqual([9.0, 18.0, 10.0, 20.0], bounds)

if __name__ == '__main__':
    app = model.Application(LSystem)
    app.MainLoop()