-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvector.py
More file actions
102 lines (79 loc) · 1.92 KB
/
vector.py
File metadata and controls
102 lines (79 loc) · 1.92 KB
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
from operator import add, mul
from math import acos
class Vector(object):
def __init__(self, x, y, z):
self.data = [x, y, z]
@property
def x(self):
return self[0]
@x.setter
def x(self, value):
self[0] = value
@property
def y(self):
return self[1]
@y.setter
def y(self, value):
self[1] = value
@property
def z(self):
return self[2]
@z.setter
def z(self, value):
self[2] = value
def __getitem__(self, *args):
return self.data.__getitem__(*args)
def __setitem__(self, *args):
return self.data.__setitem__(*args)
def __iter__(self):
return iter(self.data)
def __str__(self):
return "<{}, {}, {}>".format(self.x, self.y, self.z)
def __repr__(self):
return "Vector(x={}, y={}, z={})".format(self.x, self.y, self.z)
def __add__(self, other):
if isinstance(other, Vector):
return Vector(*map(add, self, other))
return Vector(self.x+other, self.y+other, self.z+other)
def __mul__(self, other):
return sum(map(mul, self, other))
def __sub__(self, other):
return self + -other
def __neg__(self):
return -1*self
def __radd__(self, other):
return self + other
def __rmul__(self, other):
return Vector(self.x*other, self.y*other, self.z*other)
def __rsub__(self, other):
return other + -self
def __iadd__(self, other):
if isinstance(other, Vector):
self.x += other.x
self.y += other.y
self.z += other.z
return self
self.x += other
self.y += other
self.z += other
return self
def __imul__(self, other):
if isinstance(other, (int, float, complex)):
self.x *= other
self.y *= other
self.z *= other
return self
def __isub__(self, other):
if isinstance(other, Vector):
self.x -= other.x
self.y -= other.y
self.z -= other.z
return self
self.x -= other
self.y -= other
self.z -= other
return self
def __abs__(self):
return (self*self)**0.5
def angle(self, other):
return acos(self*other/(abs(self)*abs(other)))