forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2d.py
More file actions
29 lines (21 loc) · 649 Bytes
/
Copy pathvector2d.py
File metadata and controls
29 lines (21 loc) · 649 Bytes
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
from math import hypot
class Vector:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# def __repr__(self):
# return 'Vector(%r, %r)' % (self.x, self.y)
def __repr__(self):
return f'Vector({self.x!r}, {self.y!r})'
def __abs__(self):
return hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
def __bool__(self):
return bool(self.x or self.y)