-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
140 lines (107 loc) · 3.46 KB
/
Copy pathvector.py
File metadata and controls
140 lines (107 loc) · 3.46 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
import math
class Vector3:
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0) -> None:
self.x: float = float(x)
self.y: float = float(y)
self.z: float = float(z)
def __repr__(self) -> str:
return f"Vector3({self.x}, {self.y}, {self.z})"
def __add__(self, other: "Vector3") -> "Vector3":
return Vector3(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other: "Vector3") -> "Vector3":
return Vector3(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, scalar: float) -> "Vector3":
return Vector3(self.x * scalar, self.y * scalar, self.z * scalar)
def __truediv__(self, scalar: float) -> "Vector3":
return Vector3(self.x / scalar, self.y / scalar, self.z / scalar)
def __iadd__(self, other: "Vector3") -> "Vector3":
self.x += other.x
self.y += other.y
self.z += other.z
return self
def __isub__(self, other: "Vector3") -> "Vector3":
self.x -= other.x
self.y -= other.y
self.z -= other.z
return self
def __imul__(self, scalar: float) -> "Vector3":
self.x *= scalar
self.y *= scalar
self.z *= scalar
return self
def __itruediv__(self, scalar: float) -> "Vector3":
self.x /= scalar
self.y /= scalar
self.z /= scalar
return self
def length(self) -> float:
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
def length_squared(self) -> float:
return self.x**2 + self.y**2 + self.z**2
def normalized(self) -> "Vector3":
l = self.length()
if l == 0:
return Vector3()
return self / l
def dot(self, other: "Vector3") -> float:
return self.x * other.x + self.y * other.y + self.z * other.z
def cross(self, other: "Vector3") -> "Vector3":
return Vector3(
self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x
)
def to_radians(self) -> "Vector3":
return Vector3(
math.radians(self.x),
math.radians(self.y),
math.radians(self.z)
)
class Vector2:
def __init__(self, x: float = 0.0, y: float = 0.0) -> None:
self.x: float = float(x)
self.y: float = float(y)
def __repr__(self) -> str:
return f"Vector2({self.x}, {self.y})"
def __add__(self, other: "Vector2") -> "Vector2":
return Vector2(self.x + other.x, self.y + other.y)
def __sub__(self, other: "Vector2") -> "Vector2":
return Vector2(self.x - other.x, self.y - other.y)
def __mul__(self, scalar: float) -> "Vector2":
return Vector2(self.x * scalar, self.y * scalar)
def __truediv__(self, scalar: float) -> "Vector2":
return Vector2(self.x / scalar, self.y / scalar)
def __iadd__(self, other: "Vector2") -> "Vector2":
self.x += other.x
self.y += other.y
return self
def __isub__(self, other: "Vector2") -> "Vector2":
self.x -= other.x
self.y -= other.y
return self
def __imul__(self, scalar: float) -> "Vector2":
self.x *= scalar
self.y *= scalar
return self
def __itruediv__(self, scalar: float) -> "Vector2":
self.x /= scalar
self.y /= scalar
return self
def length(self) -> float:
return math.sqrt(self.x * self.x + self.y * self.y)
def length_squared(self) -> float:
return self.x * self.x + self.y * self.y
def normalize(self) -> "Vector2":
l = self.length()
if l == 0:
return Vector2()
return self / l
def dot(self, other: "Vector2") -> float:
return self.x * other.x + self.y * other.y
def cross(self, other: "Vector2") -> float:
return self.x * other.y - self.y * other.x
def to_radians(self) -> "Vector2":
return Vector2(
math.radians(self.x),
math.radians(self.y),
)