-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemath.py
114 lines (93 loc) · 3.6 KB
/
emath.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
"""
Quadcopter simulation
Copyright (C) 2024 Ivan Zabrodin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
# For things like Vector3 implementations for readable code
class Vector3:
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0) -> None:
self.x = x
self.y = y
self.z = z
def __getitem__(self, key: int) -> float:
return [self.x, self.y, self.z][key]
def __setitem__(self, key: int, value: float | int) -> None:
[self.x, self.y, self.z][key] = value
def __add__(self, vec: "Vector3 | tuple[float, float, float]") -> "Vector3":
return Vector3(
self.x + vec[0],
self.y + vec[1],
self.z + vec[2],
)
def __sub__(self, vec: "Vector3 | tuple[float, float, float]") -> "Vector3":
return Vector3(
self.x - vec[0],
self.y - vec[1],
self.z - vec[2],
)
def __mul__(self, val: float | int) -> "Vector3":
return Vector3(
self.x * val,
self.y * val,
self.z * val,
)
def __iter__(self):
return iter([self.x, self.y, self.z])
def __repr__(self) -> str:
return f"({self.x}, {self.y}, {self.z})"
class Vector4:
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0, w: float = 0.0) -> None:
self.x = x
self.y = y
self.z = z
self.w = w
def __getitem__(self, key: int) -> float:
return [self.x, self.y, self.z, self.w][key]
def __setitem__(self, key: int, value: float | int) -> None:
[self.x, self.y, self.z, self.w][key] = value
def __add__(self, vec: "Vector4 | tuple[float, float, float, float]") -> "Vector4":
return Vector4(
self.x + vec[0],
self.y + vec[1],
self.z + vec[2],
self.w + vec[3],
)
def __sub__(self, vec: "Vector4 | tuple[float, float, float, float]") -> "Vector4":
return Vector4(
self.x - vec[0],
self.y - vec[1],
self.z - vec[2],
self.w - vec[3],
)
def __mul__(self, val: "float | int | Vector4") -> "Vector4":
if isinstance(val, Vector4):
return Vector4(
self.x * val.x,
self.y * val.y,
self.z * val.z,
self.w * val.w,
)
return Vector4(
self.x * val,
self.y * val,
self.z * val,
self.w * val,
)
def __iter__(self):
return iter([self.x, self.y, self.z, self.w])
def __repr__(self) -> str:
return f"({self.x}, {self.y}, {self.z}, {self.w})"
def frange(start: float, end: float, step: float) -> list[float]:
res = []
for n in range(int(start // step), int((end - start) // step) + 1):
res.append(n * step)
return res