forked from jwasham/practice-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
37 lines (26 loc) · 706 Bytes
/
vector.py
File metadata and controls
37 lines (26 loc) · 706 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
30
31
32
33
34
35
36
37
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})".format(self.x, self.y)
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 __mult__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
def main():
v1 = Vector(2, 4)
v2 = Vector(2, 1)
print(v1)
print(v2)
# assert (v1 + v2) == Vector(4, 5)
# assert abs( * 3)
if __name__ == "__main__":
main()