-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvectors.py
250 lines (197 loc) · 7.22 KB
/
vectors.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
vectors.py
Math for Vectors
authored by Vincent
version 1.0.2
last modified Nov 28, 2020 at 3:59 PM (UTC + 7)
Copyright © 2020 Vincent. All rights reserved.
"""
import math
def main():
""" Main function"""
print(help(Vector2))
def parallelogram(lhs, rhs):
""" Find area of parallelogram """
if isinstance(lhs, type(rhs)) and type(lhs) not in [Vector2, Vector3, VectorAny]:
raise TypeError("Cannot do dot product with a non Vector")
return (lhs * rhs).magnitude()
def triangle(lhs, rhs):
""" Get area of triangle """
return parallelogram(lhs, rhs) / 2
class Vector2:
""" Vector of x and y """
def __init__(self, x, y):
""" Create self """
self.array = [x, y]
def get_x(self):
""" X Axis value """
return self.array[0]
def get_y(self):
""" Y axis value """
return self.array[1]
def __add__(self, other):
""" Add self """
new = Vector2(self.get_x(), self.get_y())
if not isinstance(other, Vector2):
raise TypeError("Cannot do add product with a non Vector2")
for i in range(len(new.array)):
new.array[i] = new.array[i] + other.array[i]
return new
def __sub__(self, other):
""" Subtract self """
new = Vector2(self.get_x(), self.get_y())
if not isinstance(other, Vector2):
raise TypeError("Cannot do min product with a non Vector2")
for i in range(len(new.array)):
new.array[i] = new.array[i] - other.array[i]
return new
def dot(self, other):
""" Dot product """
if not isinstance(other, Vector2):
raise TypeError("Cannot do dot product with a non Vector2")
res = 0.0
for i in range(len(self.array)):
res += self.array[i] * other.array[i]
return res
def __str__(self):
""" String self """
return "Vector2 ({0}, {1})".format(self.array[0], self.array[1])
def magnitude(self):
""" Magnitude """
res = 0.0
for i in self.array:
res += (i ** 2)
return math.sqrt(res)
def is_orthogonal(self, other) -> bool:
""" Check if two vector is orthogonal to each other """
return self.dot(other) == 0
@staticmethod
def zero():
""" Zero of Vector2 """
return Vector2(0, 0)
class Vector3:
""" Vector of 3 axis """
def __init__(self, x, y, z):
""" Constructor """
self.axises = [x, y, z]
@staticmethod
def zero():
""" Zero Vector3 """
return Vector3(0, 0, 0)
def get_x(self):
""" X Axis value """
return self.axises[0]
def get_y(self):
""" Y Axis value """
return self.axises[1]
def get_z(self):
""" Z Axis value """
return self.axises[2]
def __str__(self):
""" String self """
return "Vector3 ({0}, {1}, {2})".format(self.get_x(), self.get_y(), self.get_z())
def __add__(self, other):
""" Add two vector3 """
new = Vector3(self.get_x(), self.get_y(), self.get_z())
if not isinstance(other, Vector3):
raise TypeError("Cannot do add product with a non Vector3")
# Add all element of the vectors of self and the other
for i in range(len(new.axises)):
new.axises[i] = new.axises[i] + other.axises[i]
return new
def __sub__(self, other):
""" Subtract two vector3 """
new = Vector3(self.get_x(), self.get_y(), self.get_z())
if not isinstance(other, Vector3):
raise TypeError("Cannot do min product with a non Vector3")
# Subtract all element of the vectors of self and the other
for i in range(len(new.axises)):
new.axises[i] = new.axises[i] - other.axises[i]
return new
def __mul__(self, other):
""" Cross product """
if not isinstance(other, Vector3):
raise TypeError("Cannot do cross product with a non Vector3")
# Get a new Vector 3 as result
new_value = Vector3.zero()
# Loop through for each axises
for i in range(len(self.axises)):
# Get all the axises beside the currently checked on
lhs = [self.axises[j] for j in range(len(self.axises)) if j != i]
rhs = [other.axises[k] for k in range(len(other.axises)) if k != i]
# Find the determinant and put in the result for the current index
deter = (lhs[0] * rhs[1]) - (lhs[1] * rhs[0])
new_value.axises[i] = deter
return new_value
def dot(self, other):
""" Dot product of vector3 """
if not isinstance(other, Vector3):
raise TypeError("Cannot do dot product with a non Vector3")
# Add all the multi of each axises from both
res = 0.0
for i in range(len(self.axises)):
res += self.axises[i] * other.axises[i]
return res
def magnitude(self):
""" Magnitude """
res = 0.0
for num in self.axises:
res += num ** 2
return res
def is_orthogonal(self, other) -> bool:
""" Check if two vector is orthogonal to each other """
return self.dot(other) == 0
class VectorAny:
""" Vector of size any """
def __init__(self, *axises):
""" Construct a Vector """
res = []
for axis in axises:
res.append(axis)
self.axises = res
def __str__(self):
""" String self """
res = "("
for axis in self.axises:
res += " " + str(axis) + ","
return res + ")"
def __add__(self, other):
""" Add two VectorAny """
new = VectorAny(1)
new.axises = self.axises
if not isinstance(other, VectorAny) or len(other.axises) != len(self.axises):
raise TypeError("Cannot add to a non VectorAny or VectorAny with different size")
# Add all axises
for i in range(len(new.axises)):
new.axises[i] = new.axises[i] + other.axises[i]
return new
def __sub__(self, other):
""" Substract two VectorAny """
new = VectorAny(1)
new.axises = self.axises
if not isinstance(other, VectorAny) or len(other.axises) != len(self.axises):
raise TypeError("Cannot add to a non VectorAny or VectorAny with different size")
# Subtract all axises
for i in range(len(new.axises)):
new.axises[i] = new.axises[i] - other.axises[i]
return new
def dot(self, other):
""" Dot product of VectorAny """
if not isinstance(other, VectorAny) or len(other.axises) != len(self.axises):
raise TypeError("Cannot add to a non VectorAny or VectorAny with different size")
# Add all multiplication of each axises from both
res = 0.0
for i in range(len(self.axises)):
res += self.axises[i] * other.axises[i]
return res
def magnitude(self):
""" Magnitude """
res = 0.0
for num in self.axises:
res += num ** 2
return res
def is_orthogonal(self, other) -> bool:
""" Check if two vector is orthogonal to each other """
return self.dot(other) == 0
if __name__ == '__main__':
main()