Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96171
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
import turtle
15
16
from polygon import circle, arc
17
18
# LEVEL 0 PRIMITIVES
19
# fd, bk, lt, rt, pu, pd
20
21
def fd(t, length):
22
t.fd(length)
23
24
def bk(t, length):
25
t.bk(length)
26
27
def lt(t, angle=90):
28
t.lt(angle)
29
30
def rt(t, angle=90):
31
t.rt(angle)
32
33
def pd(t):
34
t.pd()
35
36
def pu(t):
37
t.pu()
38
39
40
# LEVEL 1 PRIMITIVES are simple combinations of Level 0 primitives.
41
# They have no pre- or post-conditions.
42
43
def fdlt(t, n, angle=90):
44
"""forward and left"""
45
fd(t, n)
46
lt(t, angle)
47
48
def fdbk(t, n):
49
"""forward and back, ending at the original position"""
50
fd(t, n)
51
bk(t, n)
52
53
def skip(t, n):
54
"""lift the pen and move"""
55
pu(t)
56
fd(t, n)
57
pd(t)
58
59
def stump(t, n, angle=90):
60
"""Makes a vertical line and leave the turtle at the top, facing right"""
61
lt(t)
62
fd(t, n)
63
rt(t, angle)
64
65
def hollow(t, n):
66
"""move the turtle vertically and leave it at the top, facing right"""
67
lt(t)
68
skip(t, n)
69
rt(t)
70
71
72
# LEVEL 2 PRIMITIVES use primitives from Levels 0 and 1
73
# to draw posts (vertical elements) and beams (horizontal elements)
74
# Level 2 primitives ALWAYS return the turtle to the original
75
# location and direction.
76
77
def post(t, n):
78
"""Makes a vertical line and return to the original position"""
79
lt(t)
80
fdbk(t, n)
81
rt(t)
82
83
def beam(t, n, height):
84
"""Makes a horizontal line at the given height and return."""
85
hollow(t, n*height)
86
fdbk(t, n)
87
hollow(t, -n*height)
88
89
def hangman(t, n, height):
90
"""Makes a vertical line to the given height and a horizontal line
91
at the given height and then return.
92
This is efficient to implement, and turns out to be useful, but
93
it's not so semantically clean."""
94
stump(t, n * height)
95
fdbk(t, n)
96
lt(t)
97
bk(t, n*height)
98
rt(t)
99
100
def diagonal(t, x, y):
101
"""Makes a diagonal line to the given x, y offsets and return"""
102
from math import atan2, sqrt, pi
103
angle = atan2(y, x) * 180 / pi
104
dist = sqrt(x**2 + y**2)
105
lt(t, angle)
106
fdbk(t, dist)
107
rt(t, angle)
108
109
def vshape(t, n, height):
110
diagonal(t, -n/2, height*n)
111
diagonal(t, n/2, height*n)
112
113
def bump(t, n, height):
114
"""Makes a bump with radius n at height*n
115
"""
116
stump(t, n*height)
117
arc(t, n/2.0, 180)
118
lt(t)
119
fdlt(t, n*height+n)
120
121
122
"""
123
The letter-drawing functions all have the precondition
124
that the turtle is in the lower-left corner of the letter,
125
and postcondition that the turtle is in the lower-right
126
corner, facing in the direction it started in.
127
128
They all take a turtle as the first argument and a size (n)
129
as the second. Most letters are (n) units wide and (2n) units
130
high.
131
132
"""
133
134
def draw_a(t, n):
135
diagonal(t, n/2, 2*n)
136
beam(t, n, 1)
137
skip(t, n)
138
diagonal(t, -n/2, 2*n)
139
140
def draw_b(t, n):
141
bump(t, n, 1)
142
bump(t, n, 0)
143
skip(t, n/2)
144
145
def draw_c(t, n):
146
hangman(t, n, 2)
147
fd(t, n)
148
149
def draw_d(t, n):
150
bump(t, 2*n, 0)
151
skip(t, n)
152
153
def draw_ef(t, n):
154
hangman(t, n, 2)
155
hangman(t, n, 1)
156
157
def draw_e(t, n):
158
draw_ef(t, n)
159
fd(t, n)
160
161
def draw_f(t, n):
162
draw_ef(t, n)
163
skip(t, n)
164
165
def draw_g(t, n):
166
hangman(t, n, 2)
167
fd(t, n/2)
168
beam(t, n/2, 2)
169
fd(t, n/2)
170
post(t, n)
171
172
def draw_h(t, n):
173
post(t, 2*n)
174
hangman(t, n, 1)
175
skip(t, n)
176
post(t, 2*n)
177
178
def draw_i(t, n):
179
beam(t, n, 2)
180
fd(t, n/2)
181
post(t, 2*n)
182
fd(t, n/2)
183
184
def draw_j(t, n):
185
beam(t, n, 2)
186
arc(t, n/2, 90)
187
fd(t, 3*n/2)
188
skip(t, -2*n)
189
rt(t)
190
skip(t, n/2)
191
192
def draw_k(t, n):
193
post(t, 2*n)
194
stump(t, n, 180)
195
vshape(t, 2*n, 0.5)
196
fdlt(t, n)
197
skip(t, n)
198
199
def draw_l(t, n):
200
post(t, 2*n)
201
fd(t, n)
202
203
def draw_n(t, n):
204
post(t, 2*n)
205
skip(t, n)
206
diagonal(t, -n, 2*n)
207
post(t, 2*n)
208
209
def draw_m(t, n):
210
post(t, 2*n)
211
draw_v(t, n)
212
post(t, 2*n)
213
214
def draw_o(t, n):
215
skip(t, n)
216
circle(t, n)
217
skip(t, n)
218
219
def draw_p(t, n):
220
bump(t, n, 1)
221
skip(t, n/2)
222
223
def draw_q(t, n):
224
draw_o(t, n)
225
diagonal(t, -n/2, n)
226
227
def draw_r(t, n):
228
draw_p(t, n)
229
diagonal(t, -n/2, n)
230
231
def draw_s(t, n):
232
fd(t, n/2)
233
arc(t, n/2, 180)
234
arc(t, n/2, -180)
235
fdlt(t, n/2, -90)
236
skip(t, 2*n)
237
lt(t)
238
239
def draw_t(t, n):
240
beam(t, n, 2)
241
skip(t, n/2)
242
post(t, 2*n)
243
skip(t, n/2)
244
245
def draw_u(t, n):
246
post(t, 2*n)
247
fd(t, n)
248
post(t, 2*n)
249
250
def draw_v(t, n):
251
skip(t, n/2)
252
vshape(t, n, 2)
253
skip(t, n/2)
254
255
def draw_w(t, n):
256
draw_v(t, n)
257
draw_v(t, n)
258
259
def draw_x(t, n):
260
diagonal(t, n, 2*n)
261
skip(t, n)
262
diagonal(t, -n, 2*n)
263
264
def draw_v(t, n):
265
skip(t, n/2)
266
diagonal(t, -n/2, 2*n)
267
diagonal(t, n/2, 2*n)
268
skip(t, n/2)
269
270
def draw_y(t, n):
271
skip(t, n/2)
272
stump(t, n)
273
vshape(t, n, 1)
274
rt(t)
275
fdlt(t, n)
276
skip(t, n/2)
277
278
def draw_z(t, n):
279
beam(t, n, 2)
280
diagonal(t, n, 2*n)
281
fd(t, n)
282
283
def draw_(t, n):
284
# draw a space
285
skip(t, n)
286
287
if __name__ == '__main__':
288
289
# create and position the turtle
290
size = 20
291
bob = turtle.Turtle()
292
293
for f in [draw_h, draw_e, draw_l, draw_l, draw_o]:
294
f(bob, size)
295
skip(bob, size)
296
297
turtle.mainloop()
298
299