-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
173 lines (125 loc) · 3.13 KB
/
utils.py
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
from graphics import *
from time import sleep
import random
def dawn_colors():
'''
Argument(s) :: None
Side effects
None
Return value :: list
List of black/white shade colors from black to white).
'''
grays = ["gray%s" % level for level in range(1,100, 2)]
return ["black"] + grays + ["white"]
def dusk_colors():
'''
Argument(s) :: None
Side effects
None
Return value :: list
List of black/white shade colors from white to black.
'''
d = dawn_colors()[:]
d.reverse()
return d
def take(xs, n):
'''
Arguments:
xs :: list
n :: integer
Return value :: list
n >= 0 ==> returns a list with the n first element of xs.
n < 0 ==> returns a list with the n last elements of xs.
Side effects: None
'''
if n >= 0:
return xs[:n]
else:
return xs[-1:(n-1):-1]
def shuffle(xs):
'''
Argument(s)
xs :: list
Side effects
None.
Return value :: list
A copy of xs where the order of the elements have been randomly shuffled.
'''
tmp = xs[:]
random.shuffle(tmp)
return tmp
def show(w, objects):
'''
Argument(s)
w :: GraphWin
objects :: list of graphical objects
Side effects
Draws all objects in window w.
Return value :: None
'''
for object in objects:
object.draw(w)
def hide(objects):
'''
Argument(s)
objects :: list of graphical objects
Side effects
Un-draws all objects.
Return value :: None
'''
for object in objects:
object.undraw()
def random_color():
"""
Argument(s) :: None
Side effects
None
Return value :: string
A random color name.
"""
return random.choice(["black", "white", "red", "blue", "green", "pink", "orange", "RoyalBlue1", "green3"])
def random_color_fill(shape):
'''
Argument(s)
shape :: Circle or Rectangle
Return value :: None
Side effects
Set the color of shape to a random color.
'''
shape.setFill(random_color())
def frame():
'''
Argument(s) :: None
Side effects :: None
Return value :: list
List with Line objects for a blue frame around a 500 by 500 pixel window.
'''
f = [Line(Point(0, 0), Point(499, 0)),
Line(Point(0, 499), Point(499, 499)),
Line(Point(0, 0), Point(0, 499)),
Line(Point(499, 0), Point(499, 499))]
for line in f:
line.setWidth(9)
line.setFill("blue")
return f
def make_grid():
'''
Argument(s) :: None
Side effects :: None
Return value :: list
List with Line objects. The lines make up a 5 by 5 grid and a border for
a 500 by 500 pixel window.
'''
lines = frame()
steps = range(99, 499, 100)
for y in steps:
line = Line(Point(0, y), Point(499, y))
line.setFill("blue")
line.setWidth(5)
lines.append(line)
for x in steps:
line = Line(Point(x, 0), Point(x, 499))
line.setFill("blue")
line.setWidth(5)
lines.append(line)
return lines