blob: 82dd759231a7072fce2a473e25d10841f4885222 [file] [log] [blame]
Guido van Rossumb241b671998-12-04 16:42:46 +00001# LogoMation-like turtle graphics
2
3from math import * # Also for export
4import Tkinter
Martin v. Löwis4b6ea792000-10-01 17:52:01 +00005class Error(Exception):
6 pass
Guido van Rossumb241b671998-12-04 16:42:46 +00007
8class RawPen:
9
10 def __init__(self, canvas):
11 self._canvas = canvas
12 self._items = []
13 self._tracing = 1
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000014 self._arrow = 0
Guido van Rossumb241b671998-12-04 16:42:46 +000015 self.degrees()
16 self.reset()
17
18 def degrees(self, fullcircle=360.0):
19 self._fullcircle = fullcircle
20 self._invradian = pi / (fullcircle * 0.5)
21
22 def radians(self):
23 self.degrees(2.0*pi)
24
25 def reset(self):
26 canvas = self._canvas
27 width = canvas.winfo_width()
28 height = canvas.winfo_height()
29 if width <= 1:
30 width = canvas['width']
31 if height <= 1:
32 height = canvas['height']
33 self._origin = float(width)/2.0, float(height)/2.0
34 self._position = self._origin
35 self._angle = 0.0
36 self._drawing = 1
37 self._width = 1
38 self._color = "black"
39 self._filling = 0
40 self._path = []
41 self._tofill = []
42 self.clear()
43 canvas._root().tkraise()
44
45 def clear(self):
46 self.fill(0)
47 canvas = self._canvas
48 items = self._items
49 self._items = []
50 for item in items:
51 canvas.delete(item)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000052 self._delete_turtle()
53 self._draw_turtle()
54
Guido van Rossumb241b671998-12-04 16:42:46 +000055
56 def tracer(self, flag):
57 self._tracing = flag
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000058 if not self._tracing:
59 self._delete_turtle()
60 self._draw_turtle()
Guido van Rossumb241b671998-12-04 16:42:46 +000061
62 def forward(self, distance):
63 x0, y0 = start = self._position
64 x1 = x0 + distance * cos(self._angle*self._invradian)
65 y1 = y0 - distance * sin(self._angle*self._invradian)
66 self._goto(x1, y1)
67
68 def backward(self, distance):
69 self.forward(-distance)
70
71 def left(self, angle):
72 self._angle = (self._angle + angle) % self._fullcircle
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000073 self._draw_turtle()
Guido van Rossumb241b671998-12-04 16:42:46 +000074
75 def right(self, angle):
76 self.left(-angle)
77
78 def up(self):
79 self._drawing = 0
80
81 def down(self):
82 self._drawing = 1
83
84 def width(self, width):
85 self._width = float(width)
86
87 def color(self, *args):
88 if not args:
89 raise Error, "no color arguments"
90 if len(args) == 1:
91 color = args[0]
92 if type(color) == type(""):
93 # Test the color first
94 try:
95 id = self._canvas.create_line(0, 0, 0, 0, fill=color)
Martin v. Löwis4b6ea792000-10-01 17:52:01 +000096 except Tkinter.TclError:
Guido van Rossumb241b671998-12-04 16:42:46 +000097 raise Error, "bad color string: %s" % `color`
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000098 self._set_color(color)
Guido van Rossumb241b671998-12-04 16:42:46 +000099 return
100 try:
101 r, g, b = color
102 except:
103 raise Error, "bad color sequence: %s" % `color`
104 else:
105 try:
106 r, g, b = args
107 except:
108 raise Error, "bad color arguments: %s" % `args`
109 assert 0 <= r <= 1
110 assert 0 <= g <= 1
111 assert 0 <= b <= 1
112 x = 255.0
113 y = 0.5
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000114 self._set_color("#%02x%02x%02x" % (int(r*x+y), int(g*x+y), int(b*x+y)))
115
116 def _set_color(self,color):
117 self._color = color
118 self._draw_turtle()
119
Guido van Rossumb241b671998-12-04 16:42:46 +0000120
121 def write(self, arg, move=0):
122 x, y = start = self._position
123 x = x-1 # correction -- calibrated for Windows
Fred Draked038ca82000-10-23 18:31:14 +0000124 item = self._canvas.create_text(x, y,
Guido van Rossumb241b671998-12-04 16:42:46 +0000125 text=str(arg), anchor="sw",
126 fill=self._color)
127 self._items.append(item)
128 if move:
129 x0, y0, x1, y1 = self._canvas.bbox(item)
130 self._goto(x1, y1)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000131 self._draw_turtle()
Guido van Rossumb241b671998-12-04 16:42:46 +0000132
133 def fill(self, flag):
134 if self._filling:
135 path = tuple(self._path)
136 smooth = self._filling < 0
137 if len(path) > 2:
138 item = self._canvas._create('polygon', path,
139 {'fill': self._color,
140 'smooth': smooth})
141 self._items.append(item)
142 self._canvas.lower(item)
143 if self._tofill:
144 for item in self._tofill:
145 self._canvas.itemconfigure(item, fill=self._color)
146 self._items.append(item)
147 self._path = []
148 self._tofill = []
149 self._filling = flag
150 if flag:
151 self._path.append(self._position)
152
153 def circle(self, radius, extent=None):
154 if extent is None:
155 extent = self._fullcircle
156 x0, y0 = self._position
157 xc = x0 - radius * sin(self._angle * self._invradian)
158 yc = y0 - radius * cos(self._angle * self._invradian)
159 if radius >= 0.0:
160 start = self._angle - 90.0
161 else:
162 start = self._angle + 90.0
163 extent = -extent
164 if self._filling:
165 if abs(extent) >= self._fullcircle:
166 item = self._canvas.create_oval(xc-radius, yc-radius,
167 xc+radius, yc+radius,
168 width=self._width,
169 outline="")
170 self._tofill.append(item)
171 item = self._canvas.create_arc(xc-radius, yc-radius,
172 xc+radius, yc+radius,
173 style="chord",
174 start=start,
175 extent=extent,
176 width=self._width,
177 outline="")
178 self._tofill.append(item)
179 if self._drawing:
180 if abs(extent) >= self._fullcircle:
181 item = self._canvas.create_oval(xc-radius, yc-radius,
182 xc+radius, yc+radius,
183 width=self._width,
184 outline=self._color)
185 self._items.append(item)
186 item = self._canvas.create_arc(xc-radius, yc-radius,
187 xc+radius, yc+radius,
188 style="arc",
189 start=start,
190 extent=extent,
191 width=self._width,
192 outline=self._color)
193 self._items.append(item)
194 angle = start + extent
195 x1 = xc + abs(radius) * cos(angle * self._invradian)
196 y1 = yc - abs(radius) * sin(angle * self._invradian)
197 self._angle = (self._angle + extent) % self._fullcircle
198 self._position = x1, y1
199 if self._filling:
200 self._path.append(self._position)
201
202 def goto(self, *args):
203 if len(args) == 1:
204 try:
205 x, y = args[0]
206 except:
207 raise Error, "bad point argument: %s" % `args[0]`
208 else:
209 try:
210 x, y = args
211 except:
212 raise Error, "bad coordinates: %s" % `args[0]`
213 x0, y0 = self._origin
214 self._goto(x0+x, y0-y)
215
216 def _goto(self, x1, y1):
217 x0, y0 = start = self._position
218 self._position = map(float, (x1, y1))
219 if self._filling:
220 self._path.append(self._position)
221 if self._drawing:
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000222 if self._tracing:
Guido van Rossumb241b671998-12-04 16:42:46 +0000223 dx = float(x1 - x0)
224 dy = float(y1 - y0)
225 distance = hypot(dx, dy)
226 nhops = int(distance)
227 item = self._canvas.create_line(x0, y0, x0, y0,
228 width=self._width,
Guido van Rossumb241b671998-12-04 16:42:46 +0000229 capstyle="round",
230 fill=self._color)
231 try:
232 for i in range(1, 1+nhops):
233 x, y = x0 + dx*i/nhops, y0 + dy*i/nhops
234 self._canvas.coords(item, x0, y0, x, y)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000235 self._draw_turtle((x,y))
Guido van Rossumb241b671998-12-04 16:42:46 +0000236 self._canvas.update()
237 self._canvas.after(10)
Guido van Rossuma659efe2001-01-01 19:11:07 +0000238 # in case nhops==0
239 self._canvas.coords(item, x0, y0, x1, y1)
Guido van Rossumb241b671998-12-04 16:42:46 +0000240 self._canvas.itemconfigure(item, arrow="none")
Martin v. Löwis4b6ea792000-10-01 17:52:01 +0000241 except Tkinter.TclError:
Guido van Rossumb241b671998-12-04 16:42:46 +0000242 # Probably the window was closed!
243 return
244 else:
245 item = self._canvas.create_line(x0, y0, x1, y1,
246 width=self._width,
247 capstyle="round",
248 fill=self._color)
249 self._items.append(item)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000250 self._draw_turtle()
251
252 def _draw_turtle(self,position=[]):
253 if not self._tracing:
254 return
255 if position == []:
256 position = self._position
257 x,y = position
258 distance = 8
259 dx = distance * cos(self._angle*self._invradian)
260 dy = distance * sin(self._angle*self._invradian)
261 self._delete_turtle()
Martin v. Löwis4157ffb2002-03-28 15:45:57 +0000262 self._arrow = self._canvas.create_line(x-dx,y+dy,x,y,
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000263 width=self._width,
264 arrow="last",
265 capstyle="round",
266 fill=self._color)
267 self._canvas.update()
268
269 def _delete_turtle(self):
270 if self._arrow != 0:
271 self._canvas.delete(self._arrow)
272 self._arrow = 0
273
Guido van Rossumb241b671998-12-04 16:42:46 +0000274
275
276_root = None
277_canvas = None
278_pen = None
279
280class Pen(RawPen):
281
282 def __init__(self):
283 global _root, _canvas
284 if _root is None:
Martin v. Löwis4b6ea792000-10-01 17:52:01 +0000285 _root = Tkinter.Tk()
Guido van Rossumb241b671998-12-04 16:42:46 +0000286 _root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
287 if _canvas is None:
288 # XXX Should have scroll bars
Martin v. Löwis4b6ea792000-10-01 17:52:01 +0000289 _canvas = Tkinter.Canvas(_root, background="white")
Guido van Rossumb241b671998-12-04 16:42:46 +0000290 _canvas.pack(expand=1, fill="both")
291 RawPen.__init__(self, _canvas)
292
293 def _destroy(self):
294 global _root, _canvas, _pen
295 root = self._canvas._root()
296 if root is _root:
297 _pen = None
298 _root = None
299 _canvas = None
300 root.destroy()
Fred Draked038ca82000-10-23 18:31:14 +0000301
Guido van Rossumb241b671998-12-04 16:42:46 +0000302
303def _getpen():
304 global _pen
305 pen = _pen
306 if not pen:
307 _pen = pen = Pen()
308 return pen
309
310def degrees(): _getpen().degrees()
311def radians(): _getpen().radians()
312def reset(): _getpen().reset()
313def clear(): _getpen().clear()
314def tracer(flag): _getpen().tracer(flag)
315def forward(distance): _getpen().forward(distance)
316def backward(distance): _getpen().backward(distance)
317def left(angle): _getpen().left(angle)
318def right(angle): _getpen().right(angle)
319def up(): _getpen().up()
320def down(): _getpen().down()
321def width(width): _getpen().width(width)
322def color(*args): apply(_getpen().color, args)
323def write(arg, move=0): _getpen().write(arg, move)
324def fill(flag): _getpen().fill(flag)
325def circle(radius, extent=None): _getpen().circle(radius, extent)
326def goto(*args): apply(_getpen().goto, args)
327
328def demo():
329 reset()
330 tracer(1)
331 up()
332 backward(100)
333 down()
334 # draw 3 squares; the last filled
335 width(3)
336 for i in range(3):
337 if i == 2:
338 fill(1)
339 for j in range(4):
340 forward(20)
341 left(90)
342 if i == 2:
343 color("maroon")
344 fill(0)
345 up()
346 forward(30)
347 down()
348 width(1)
349 color("black")
350 # move out of the way
351 tracer(0)
352 up()
353 right(90)
354 forward(100)
355 right(90)
356 forward(100)
357 right(180)
358 down()
359 # some text
360 write("startstart", 1)
361 write("start", 1)
362 color("red")
363 # staircase
364 for i in range(5):
365 forward(20)
366 left(90)
367 forward(20)
368 right(90)
369 # filled staircase
370 fill(1)
371 for i in range(5):
372 forward(20)
373 left(90)
374 forward(20)
375 right(90)
376 fill(0)
377 # more text
378 write("end")
379 if __name__ == '__main__':
380 _root.mainloop()
381
382if __name__ == '__main__':
383 demo()