-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.py
More file actions
39 lines (31 loc) · 843 Bytes
/
Vector.py
File metadata and controls
39 lines (31 loc) · 843 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
38
39
from math import hypot
class Vector:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
# 把一个对象以字符串形式表现出来以便辨认
def __repr__(self):
return '__repr__ Vector(%s, %s)'% (self.x,self.y)
def __abs__(self):
# Return the Euclidean distance, sqrt(x*x + y*y).
return hypot(self.x, self.y)
def __bool__(self):
# return bool(abs(self))
# 更高效的做法
return bool(self.x or self.y)
# 算术运算符 +
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)
v1=Vector(1,2)
v2=Vector(3,4)
print(v1+v2)
print(v1*5)
print(abs(v2))
print(v1)
v3=Vector()
print(bool(v3))