-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.py
More file actions
219 lines (183 loc) · 7.83 KB
/
Vector.py
File metadata and controls
219 lines (183 loc) · 7.83 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#Vector.py
import math
class Vector3:
def __init__(self, *args):
self.current = -1
if isinstance(args[0], tuple) or isinstance(args[0], list) or isinstance(args[0], Vector3):
self.x = args[0][0]
self.y = args[0][1]
self.z = args[0][2]
else:
self.x = args[0]
self.y = args[1]
self.z = args[2]
self.magnitude = math.sqrt(self.x**2 + self.y**2 + self.z**2)
def __str__(self):
return f"Vector3({self.x}, {self.y}, {self.z})"
def __repr__(self):
return f"Vector3({self.x}, {self.y}, {self.z})"
def __getitem__(self, key):
if key==0:
return self.x
elif key==1:
return self.y
elif key==2:
return self.z
else:
return None
def __setitem__(self, key, value):
if key==0:
self.x = value
self.magnitude = math.sqrt(self.x**2 + self.y**2 + self.z**2)
elif key==1:
self.y = value
self.magnitude = math.sqrt(self.x**2 + self.y**2 + self.z**2)
elif key==2:
self.z = value
self.magnitude = math.sqrt(self.x**2 + self.y**2 + self.z**2)
def __eq__(self, other):
if not isinstance(other, Vector3):
return False
if self.x == other.x and self.y == other.y and self.z == other.z:
return True
def __add__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
elif (isinstance(other, tuple) or isinstance(other, list)) and len(other) == 3:
return Vector3(self.x + other[0], self.y + other[1], self.z + other[2])
elif isinstance(other, float) or isinstance(other, int):
return Vector3(self.x + other, self.y + other, self.z + other)
else:
raise TypeError("Unsupported operand type. Can only add Vector3 objects.")
def __radd__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
elif (isinstance(other, tuple) or isinstance(other, list)) and len(other) == 3:
return Vector3(self.x + other[0], self.y + other[1], self.z + other[2])
elif isinstance(other, float) or isinstance(other, int):
return Vector3(self.x + other, self.y + other, self.z + other)
else:
raise TypeError("Unsupported operand type. Can only add Vector3 objects.")
def __iadd__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
elif (isinstance(other, tuple) or isinstance(other, list)) and len(other) == 3:
return Vector3(self.x + other[0], self.y + other[1], self.z + other[2])
elif isinstance(other, float) or isinstance(other, int):
return Vector3(self.x + other, self.y + other, self.z + other)
else:
raise TypeError("Unsupported operand type. Can only add Vector3 objects.")
def __sub__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
elif (isinstance(other, tuple) or isinstance(other, list)) and len(other) == 3:
return Vector3(self.x - other[0], self.y - other[1], self.z - other[2])
elif isinstance(other, float) or isinstance(other, int):
return Vector3(self.x - other, self.y - other, self.z - other)
else:
raise TypeError("Unsupported operand type. Can only subtract Vector3 objects.")
def __mul__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x * other.x, self.y * other.y, self.z * other.z)
elif isinstance(other, float) or isinstance(other, int):
return Vector3(self.x * other, self.y * other, self.z * other)
else:
raise TypeError("Unsupported operand type. Can only multiply Vector3 objects.")
def __rmul__(self, other):
if isinstance(other, Vector3):
return Vector3(self.x * other.x, self.y * other.y, self.z * other.z)
elif isinstance(other, float) or isinstance(other, int):
return Vector3(self.x * other, self.y * other, self.z * other)
else:
raise TypeError("Unsupported operand type. Can only multiply Vector3 objects.")
def __truediv__(self, other):
if isinstance(other, Vector3):
if other.x != 0 and other.y != 0:
return Vector3(self.x / other.x, self.y / other.y, self.z / other.z)
else:
raise ValueError("Division by zero not allowed.")
elif isinstance(other, float) or isinstance(other, int):
return Vector3(self.x / other, self.y / other, self.z / other)
else:
raise TypeError("Unsupported operand type. Can only divide Vector3 objects.")
def __neg__(self):
return Vector3(-self.x, -self.y, -self.z)
def __iter__(self):
self.current = -1
return self
def __next__(self):
if self.current >= 2:
raise StopIteration
self.current += 1
if self.current == 0:
return self.x
elif self.current == 1:
return self.y
elif self.current == 2:
return self.z
def normalizeSelf(self):
self.x /= self.magnitude
self.y /= self.magnitude
self.z /= self.magnitude
self.magnitude = 1
def setMag(self, mag):
self.x *= mag
self.y *= mag
self.z *= mag
self.magnitude = mag
def getMag(self):
return self.magnitude
class Vector2:
def __init__(self, x, y):
self.x = x
self.y = y
self.magnitude = math.sqrt(self.x**2 + self.y**2)
def __str__(self):
return f"Vector2(x={self.x}, y={self.y})"
def __getitem__(self, key):
if key==0:
return self.x
elif key==1:
return self.y
else:
return None
def __setitem__(self, key, value):
if key==0:
self.x = value
self.magnitude = math.sqrt(self.x**2 + self.y**2)
elif key==1:
self.y = value
self.magnitude = math.sqrt(self.x**2 + self.y**2)
def normalizeSelf(self):
self.x /= magnitude
self.y /= magnitude
self.magnitude = 1
def setMag(self, mag):
self.x *= mag
self.y *= mag
self.magnitude = mag
def __add__(self, other):
if isinstance(other, Vector2):
return Vector2(self.x + other.x, self.y + other.y)
else:
raise TypeError("Unsupported operand type. Can only add Vector2 objects.")
def __sub__(self, other):
if isinstance(other, Vector2):
return Vector2(self.x - other.x, self.y - other.y)
else:
raise TypeError("Unsupported operand type. Can only subtract Vector2 objects.")
def __mul__(self, other):
if isinstance(other, Vector2):
return Vector2(self.x * other.x, self.y * other.y)
else:
raise TypeError("Unsupported operand type. Can only multiply Vector2 objects.")
def __div__(self, other):
if isinstance(other, Vector2):
if other.x != 0 and other.y != 0:
return Vector2(self.x / other.x, self.y / other.y)
else:
raise ValueError("Division by zero not allowed.")
else:
raise TypeError("Unsupported operand type. Can only divide Vector2 objects.")
def getMag(self):
return self.magnitude