-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.vector.py
More file actions
35 lines (23 loc) · 717 Bytes
/
2.vector.py
File metadata and controls
35 lines (23 loc) · 717 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
from math import hypot
class Vector:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Vector({0},{1})'.format(self.x, self.y)
def __abs__(self):
return hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
# return bool(self.x or self.y)
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
def __rmul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
def main():
vector = Vector(2, 6)
print(2 * vector)
if __name__ == '__main__':
main()