-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.py
57 lines (39 loc) · 1.47 KB
/
vector.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
from math import sqrt
class Vector2:
def __init__(self, x: float, y: float):
self.x = float(x)
self.y = float(y)
self.magnitude = sqrt(self.x**2 + self.y**2)
def __add__(self, other):
return Vector2(self.x+other.x, self.y+other.y)
def __sub__(self, other):
return Vector2(self.x-other.x, self.y-other.y)
def __mul__(self, scale):
return Vector2(self.x*scale, self.y*scale)
def cross_product(self, other):
Cz = self.x * other.y - self.y * other.x
return Vector3(0, 0, Cz)
def dot_product(self, other):
return self.x * other.x + self.y * other.y
def __str__(self):
return f'({self.x}, {self.y})'
def copy(self):
return Vector2(self.x, self.y)
class Vector3:
def __init__(self, x: float, y: float, z: float):
self.x = float(x)
self.y = float(y)
self.z = float(z)
def __add__(self, other):
return Vector3(self.x+other.x, self.y+other.y, self.z+other.z)
def __sub__(self, other):
return Vector3(self.x-other.x, self.y-other.y, self.z-other.z)
def cross_product(self, other):
Cx = self.y*other.z - self.z-other.y
Cy = other.x*self.z - other.z*self.x
Cz = self.x*other.y - self.y*other.x
return Vector3(Cx, Cy, Cz)
def dot_product(self, other):
return self.x*other.x + self.y*other.y + self.z*other.z
def __str__(self):
return f'({self.x}, {self.y}, {self.z})'