-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
475 lines (375 loc) · 9.92 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
"""Utilities
"""
# pylint: disable=no-member
import collections.abc
import math
import os
def floor(value, size, offset=200):
"""Floor of `value` given `size` and `offset`.
The floor function is best understood with a diagram of the number line::
-200 -100 0 100 200
<--|--x--|-----|--y--|--z--|-->
The number line shown has offset 200 denoted by the left-hand tick mark at
-200 and size 100 denoted by the tick marks at -100, 0, 100, and 200. The
floor of a value is the left-hand tick mark of the range where it lies. So
for the points show above: ``floor(x)`` is -200, ``floor(y)`` is 0, and
``floor(z)`` is 100.
>>> floor(10, 100)
0.0
>>> floor(120, 100)
100.0
>>> floor(-10, 100)
-100.0
>>> floor(-150, 100)
-200.0
>>> floor(50, 167)
-33.0
"""
return float(((value + offset) // size) * size - offset)
def path(filename):
"""Return full path to `filename` in freegames module."""
filepath = os.path.realpath(__file__)
dirpath = os.path.dirname(filepath)
fullpath = os.path.join(dirpath, filename)
return fullpath
def line(a, b, x, y):
"""Draw line from `(a, b)` to `(x, y)`."""
import turtle
turtle.up()
turtle.goto(a, b)
turtle.down()
turtle.goto(x, y)
def square(x, y, size, name):
"""Draw square at `(x, y)` with side length `size` and fill color `name`.
The square is oriented so the bottom left corner is at (x, y).
"""
import turtle
turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.color(name)
turtle.begin_fill()
for count in range(4):
turtle.forward(size)
turtle.left(90)
turtle.end_fill()
class vector(collections.abc.Sequence):
"""Two-dimensional vector.
Vectors can be modified in-place.
>>> v = vector(0, 1)
>>> v.move(1)
>>> v
vector(1, 2)
>>> v.rotate(90)
>>> v
vector(-2.0, 1.0)
"""
# pylint: disable=invalid-name
PRECISION = 6
__slots__ = ('_x', '_y', '_hash')
def __init__(self, x, y):
"""Initialize vector with coordinates: x, y.
>>> v = vector(1, 2)
>>> v.x
1
>>> v.y
2
"""
self._hash = None
self._x = round(x, self.PRECISION)
self._y = round(y, self.PRECISION)
@property
def x(self):
"""X-axis component of vector.
>>> v = vector(1, 2)
>>> v.x
1
>>> v.x = 3
>>> v.x
3
"""
return self._x
@x.setter
def x(self, value):
if self._hash is not None:
raise ValueError('cannot set x after hashing')
self._x = round(value, self.PRECISION)
@property
def y(self):
"""Y-axis component of vector.
>>> v = vector(1, 2)
>>> v.y
2
>>> v.y = 5
>>> v.y
5
"""
return self._y
@y.setter
def y(self, value):
if self._hash is not None:
raise ValueError('cannot set y after hashing')
self._y = round(value, self.PRECISION)
def __hash__(self):
"""v.__hash__() -> hash(v)
>>> v = vector(1, 2)
>>> h = hash(v)
>>> v.x = 2
Traceback (most recent call last):
...
ValueError: cannot set x after hashing
"""
if self._hash is None:
pair = (self.x, self.y)
self._hash = hash(pair)
return self._hash
def __len__(self):
"""v.__len__() -> len(v)
>>> v = vector(1, 2)
>>> len(v)
2
"""
return 2
def __getitem__(self, index):
"""v.__getitem__(v, i) -> v[i]
>>> v = vector(3, 4)
>>> v[0]
3
>>> v[1]
4
>>> v[2]
Traceback (most recent call last):
...
IndexError
"""
if index == 0:
return self.x
if index == 1:
return self.y
raise IndexError
def copy(self):
"""Return copy of vector.
>>> v = vector(1, 2)
>>> w = v.copy()
>>> v is w
False
"""
type_self = type(self)
return type_self(self.x, self.y)
def __eq__(self, other):
"""v.__eq__(w) -> v == w
>>> v = vector(1, 2)
>>> w = vector(1, 2)
>>> v == w
True
"""
if isinstance(other, vector):
return self.x == other.x and self.y == other.y
return NotImplemented
def __ne__(self, other):
"""v.__ne__(w) -> v != w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v != w
True
"""
if isinstance(other, vector):
return self.x != other.x or self.y != other.y
return NotImplemented
def __iadd__(self, other):
"""v.__iadd__(w) -> v += w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v += w
>>> v
vector(4, 6)
>>> v += 1
>>> v
vector(5, 7)
"""
if self._hash is not None:
raise ValueError('cannot add vector after hashing')
if isinstance(other, vector):
self.x += other.x
self.y += other.y
else:
self.x += other
self.y += other
return self
def __add__(self, other):
"""v.__add__(w) -> v + w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v + w
vector(4, 6)
>>> v + 1
vector(2, 3)
>>> 2.0 + v
vector(3.0, 4.0)
"""
copy = self.copy()
return copy.__iadd__(other)
__radd__ = __add__
def move(self, other):
"""Move vector by other (in-place).
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v.move(w)
>>> v
vector(4, 6)
>>> v.move(3)
>>> v
vector(7, 9)
"""
self.__iadd__(other)
def __isub__(self, other):
"""v.__isub__(w) -> v -= w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v -= w
>>> v
vector(-2, -2)
>>> v -= 1
>>> v
vector(-3, -3)
"""
if self._hash is not None:
raise ValueError('cannot subtract vector after hashing')
if isinstance(other, vector):
self.x -= other.x
self.y -= other.y
else:
self.x -= other
self.y -= other
return self
def __sub__(self, other):
"""v.__sub__(w) -> v - w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v - w
vector(-2, -2)
>>> v - 1
vector(0, 1)
"""
copy = self.copy()
return copy.__isub__(other)
def __imul__(self, other):
"""v.__imul__(w) -> v *= w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v *= w
>>> v
vector(3, 8)
>>> v *= 2
>>> v
vector(6, 16)
"""
if self._hash is not None:
raise ValueError('cannot multiply vector after hashing')
if isinstance(other, vector):
self.x *= other.x
self.y *= other.y
else:
self.x *= other
self.y *= other
return self
def __mul__(self, other):
"""v.__mul__(w) -> v * w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v * w
vector(3, 8)
>>> v * 2
vector(2, 4)
>>> 3.0 * v
vector(3.0, 6.0)
"""
copy = self.copy()
return copy.__imul__(other)
__rmul__ = __mul__
def scale(self, other):
"""Scale vector by other (in-place).
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> v.scale(w)
>>> v
vector(3, 8)
>>> v.scale(0.5)
>>> v
vector(1.5, 4.0)
"""
self.__imul__(other)
def __itruediv__(self, other):
"""v.__itruediv__(w) -> v /= w
>>> v = vector(2, 4)
>>> w = vector(4, 8)
>>> v /= w
>>> v
vector(0.5, 0.5)
>>> v /= 2
>>> v
vector(0.25, 0.25)
"""
if self._hash is not None:
raise ValueError('cannot divide vector after hashing')
if isinstance(other, vector):
self.x /= other.x
self.y /= other.y
else:
self.x /= other
self.y /= other
return self
def __truediv__(self, other):
"""v.__truediv__(w) -> v / w
>>> v = vector(1, 2)
>>> w = vector(3, 4)
>>> w / v
vector(3.0, 2.0)
>>> v / 2
vector(0.5, 1.0)
"""
copy = self.copy()
return copy.__itruediv__(other)
def __neg__(self):
"""v.__neg__() -> -v
>>> v = vector(1, 2)
>>> -v
vector(-1, -2)
"""
# pylint: disable=invalid-unary-operand-type
copy = self.copy()
copy.x = -copy.x
copy.y = -copy.y
return copy
def __abs__(self):
"""v.__abs__() -> abs(v)
>>> v = vector(3, 4)
>>> abs(v)
5.0
"""
return (self.x ** 2 + self.y ** 2) ** 0.5
def rotate(self, angle):
"""Rotate vector counter-clockwise by angle (in-place).
>>> v = vector(1, 2)
>>> v.rotate(90)
>>> v == vector(-2, 1)
True
"""
if self._hash is not None:
raise ValueError('cannot rotate vector after hashing')
radians = angle * math.pi / 180.0
cosine = math.cos(radians)
sine = math.sin(radians)
x = self.x
y = self.y
self.x = x * cosine - y * sine
self.y = y * cosine + x * sine
def __repr__(self):
"""v.__repr__() -> repr(v)
>>> v = vector(1, 2)
>>> repr(v)
'vector(1, 2)'
"""
type_self = type(self)
name = type_self.__name__
return '{}({!r}, {!r})'.format(name, self.x, self.y)