e2D 1.4.11__py3-none-any.whl → 1.4.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- e2D/__init__.py +355 -1030
- e2D/__init__.pyi +1176 -0
- e2D/envs.py +95 -146
- e2D/plots.py +145 -101
- e2D/utils.py +169 -69
- e2D/winrec.py +1 -5
- {e2D-1.4.11.dist-info → e2D-1.4.12.dist-info}/METADATA +2 -2
- e2D-1.4.12.dist-info/RECORD +12 -0
- {e2D-1.4.11.dist-info → e2D-1.4.12.dist-info}/WHEEL +1 -1
- e2D-1.4.11.dist-info/RECORD +0 -11
- {e2D-1.4.11.dist-info → e2D-1.4.12.dist-info}/LICENSE +0 -0
- {e2D-1.4.11.dist-info → e2D-1.4.12.dist-info}/top_level.txt +0 -0
e2D/__init__.pyi
ADDED
|
@@ -0,0 +1,1176 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import Callable, Literal
|
|
3
|
+
|
|
4
|
+
PI : float
|
|
5
|
+
HALF_PI : float
|
|
6
|
+
QUARTER_PI : float
|
|
7
|
+
DOUBLE_PI : float
|
|
8
|
+
|
|
9
|
+
# regular expression to remove comments:
|
|
10
|
+
# """([\s\S]*?)"""
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
|
|
14
|
+
sign : Callable[[int|float], Literal[-1,0,1]]
|
|
15
|
+
|
|
16
|
+
class Vector2D:
|
|
17
|
+
round_values_on_print : int|float
|
|
18
|
+
def __init__(self:"Vector2D", x:int|float=.0, y:int|float=.0) -> None:
|
|
19
|
+
"""
|
|
20
|
+
# Initialize a 2D vector with the specified x and y components.
|
|
21
|
+
|
|
22
|
+
## Parameters:
|
|
23
|
+
x (int | float, optional): The x-component of the vector. Default is 0.
|
|
24
|
+
y (int | float, optional): The y-component of the vector. Default is 0.
|
|
25
|
+
|
|
26
|
+
## Example:
|
|
27
|
+
vector1 = Vector2D() # Creates a vector with x=0 and y=0
|
|
28
|
+
vector2 = Vector2D(3, -2.5) # Creates a vector with x=3 and y=-2.5
|
|
29
|
+
|
|
30
|
+
## Explanation:
|
|
31
|
+
This constructor initializes a 2D vector with the specified x and y components.
|
|
32
|
+
|
|
33
|
+
If no arguments are provided, the default values for x and y are both set to 0.
|
|
34
|
+
|
|
35
|
+
The x and y components can be integers or floating-point numbers.
|
|
36
|
+
|
|
37
|
+
Example usage is shown in the "Example" section above.
|
|
38
|
+
"""
|
|
39
|
+
self.x : int|float
|
|
40
|
+
self.y : int|float
|
|
41
|
+
|
|
42
|
+
def distance_to(self:"Vector2D", other:"Vector2D", sqrd:bool=True) -> int|float:
|
|
43
|
+
"""
|
|
44
|
+
# Calculate the distance between the current Vector2D other and another other.
|
|
45
|
+
|
|
46
|
+
## Parameters:
|
|
47
|
+
other (float or int or Vector2D or list|tuple): The other other to which the distance is calculated.
|
|
48
|
+
squared (bool, optional): If True, return the squared distance. If False, return the actual distance.
|
|
49
|
+
Default is True.
|
|
50
|
+
|
|
51
|
+
## Returns:
|
|
52
|
+
int|float: The squared distance between the current Vector2D other and the other other if `squared` is True,
|
|
53
|
+
otherwise the actual distance.
|
|
54
|
+
|
|
55
|
+
## Example:
|
|
56
|
+
point1 = Vector2D(0, 0)
|
|
57
|
+
|
|
58
|
+
point2 = Vector2D(3, 4)
|
|
59
|
+
|
|
60
|
+
squared_distance = point1.distance_to(point2)
|
|
61
|
+
|
|
62
|
+
print(f"Squared Distance: {squared_distance}")
|
|
63
|
+
|
|
64
|
+
distance = point1.distance_to(point2, squared=False)
|
|
65
|
+
|
|
66
|
+
print(f"Actual Distance: {distance}")
|
|
67
|
+
|
|
68
|
+
This will calculate the squared and actual distances between the two points.
|
|
69
|
+
|
|
70
|
+
## Explanation:
|
|
71
|
+
The function calculates the squared distance between the current Vector2D other (self) and another other
|
|
72
|
+
(other) using the formula: (self.x - other.x)**2 + (self.y - other.y)**2.
|
|
73
|
+
|
|
74
|
+
The result is returned as the squared distance if `squared` is True, or as the actual distance if `squared` is False.
|
|
75
|
+
"""
|
|
76
|
+
...
|
|
77
|
+
|
|
78
|
+
def angle_to(self:"Vector2D", other:"Vector2D") -> int|float:
|
|
79
|
+
"""
|
|
80
|
+
# Calculate the angle between the current Vector2D other and another other.
|
|
81
|
+
|
|
82
|
+
## Parameters:
|
|
83
|
+
other (float or int or Vector2D or list|tuple): The other other to which the angle is calculated.
|
|
84
|
+
|
|
85
|
+
## Returns:
|
|
86
|
+
int|float: The angle in radians between the current Vector2D other and the other other.
|
|
87
|
+
|
|
88
|
+
## Example:
|
|
89
|
+
point1 = Vector2D(0, 0)
|
|
90
|
+
|
|
91
|
+
point2 = Vector2D(1, 1)
|
|
92
|
+
|
|
93
|
+
angle = point1.angle_to(point2)
|
|
94
|
+
|
|
95
|
+
print(f"Angle in radians: {angle}")
|
|
96
|
+
|
|
97
|
+
This will calculate the angle in radians between the two points.
|
|
98
|
+
|
|
99
|
+
## Explanation:
|
|
100
|
+
The function calculates the angle in radians between the current Vector2D other (self) and another other
|
|
101
|
+
(other) using the `atan2` function from the `math` module.
|
|
102
|
+
|
|
103
|
+
The result is returned as the angle in radians.
|
|
104
|
+
"""
|
|
105
|
+
...
|
|
106
|
+
|
|
107
|
+
def point_from_angle_and_radius(self:"Vector2D", rad: int|float, radius: int|float) -> "Vector2D":
|
|
108
|
+
"""
|
|
109
|
+
# Calculate a new Vector2D point from the current point based on an angle in radians and a radius.
|
|
110
|
+
|
|
111
|
+
## Parameters:
|
|
112
|
+
rad (int|float): The angle in radians.
|
|
113
|
+
radius (int|float): The distance from the current point.
|
|
114
|
+
|
|
115
|
+
## Returns:
|
|
116
|
+
Vector2D: A new Vector2D point calculated from the current point.
|
|
117
|
+
|
|
118
|
+
## Example:
|
|
119
|
+
point1 = Vector2D(0, 0)
|
|
120
|
+
|
|
121
|
+
angle = 45
|
|
122
|
+
|
|
123
|
+
distance = 5
|
|
124
|
+
|
|
125
|
+
new_point = point1.point_from_degs(_mt.radians(angle), distance)
|
|
126
|
+
|
|
127
|
+
print(new_point.x, new_point.y)
|
|
128
|
+
|
|
129
|
+
This will calculate a new point 5 units away from point1 at a 45-degree angle.
|
|
130
|
+
|
|
131
|
+
## Explanation:
|
|
132
|
+
The function calculates a new Vector2D point based on an angle in radians (rad) and a distance (radius)
|
|
133
|
+
from the current Vector2D point.
|
|
134
|
+
|
|
135
|
+
It computes the new x and y coordinates of the point using the trigonometric functions `cos` and `sin`
|
|
136
|
+
to determine the horizontal and vertical components of the new point.
|
|
137
|
+
|
|
138
|
+
The result is returned as a new Vector2D point with the calculated coordinates.
|
|
139
|
+
"""
|
|
140
|
+
...
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def angle(self:"Vector2D") -> int|float: ...
|
|
144
|
+
|
|
145
|
+
@angle.setter
|
|
146
|
+
def angle(self:"Vector2D", argv) -> None: ...
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def copy(self:"Vector2D") -> "Vector2D":
|
|
150
|
+
"""
|
|
151
|
+
# Create a copy of the current Vector2D other.
|
|
152
|
+
|
|
153
|
+
## Returns:
|
|
154
|
+
Vector2D: A new Vector2D other with the same x and y coordinates as the current other.
|
|
155
|
+
|
|
156
|
+
## Example:
|
|
157
|
+
point1 = Vector2D(1, 2)
|
|
158
|
+
|
|
159
|
+
point2 = point1.copy()
|
|
160
|
+
|
|
161
|
+
print(point2.x, point2.y)
|
|
162
|
+
|
|
163
|
+
This will print the x and y coordinates of the copied Vector2D other (1, 2).
|
|
164
|
+
|
|
165
|
+
## Explanation:
|
|
166
|
+
The function creates a new Vector2D other with the same x and y coordinates as the current other.
|
|
167
|
+
|
|
168
|
+
The result is returned as a new Vector2D other, effectively making a copy of the original other.
|
|
169
|
+
"""
|
|
170
|
+
...
|
|
171
|
+
|
|
172
|
+
@property
|
|
173
|
+
def sign(self:"Vector2D") -> "Vector2D":
|
|
174
|
+
"""
|
|
175
|
+
# Perform an "absolute round" operation on the Vector2D other.
|
|
176
|
+
|
|
177
|
+
## Parameters:
|
|
178
|
+
n (int|float, optional): The numeric value to scale the "absolute rounded" vector. Default is 1.
|
|
179
|
+
|
|
180
|
+
## Returns:
|
|
181
|
+
Vector2D: The "absolute rounded" Vector2D other scaled by the provided numeric value.
|
|
182
|
+
|
|
183
|
+
## Example:
|
|
184
|
+
vector1 = Vector2D(3.3, -4.7)
|
|
185
|
+
|
|
186
|
+
result1 = vector1.absolute_round(0.5)
|
|
187
|
+
|
|
188
|
+
print(result1.x, result1.y)
|
|
189
|
+
|
|
190
|
+
vector2 = Vector2D(-2.8, 1.1)
|
|
191
|
+
|
|
192
|
+
result2 = vector2.absolute_round()
|
|
193
|
+
|
|
194
|
+
print(result2.x, result2.y)
|
|
195
|
+
|
|
196
|
+
## Explanation:
|
|
197
|
+
The function performs an "absolute round" operation on the Vector2D other.
|
|
198
|
+
|
|
199
|
+
The "absolute round" operation involves taking the absolute values of both the x and y components of the Vector2D other,
|
|
200
|
+
and then scaling the resulting vector by the provided numeric value (n).
|
|
201
|
+
|
|
202
|
+
The default value of n is 1, which means the "absolute rounded" vector will have the same magnitude as the original vector.
|
|
203
|
+
|
|
204
|
+
If the provided numeric value (n) is 0, the function returns a Vector2D other with zeros for both components.
|
|
205
|
+
|
|
206
|
+
If the provided numeric value (n) is negative, the resulting "absolute rounded" vector will point in the opposite direction
|
|
207
|
+
as the original vector but will have the same magnitude.
|
|
208
|
+
|
|
209
|
+
Note: The "absolute round" operation does not perform standard mathematical rounding; instead, it ensures the resulting
|
|
210
|
+
vector points in the same direction as the original vector but has non-negative components.
|
|
211
|
+
"""
|
|
212
|
+
...
|
|
213
|
+
|
|
214
|
+
@property
|
|
215
|
+
def normalize(self:"Vector2D") -> "Vector2D":
|
|
216
|
+
"""
|
|
217
|
+
# Vector Normalization
|
|
218
|
+
|
|
219
|
+
## Returns:
|
|
220
|
+
Vector2D: A new vector with the same direction as the current vector but with a magnitude of 1.
|
|
221
|
+
|
|
222
|
+
## Raises:
|
|
223
|
+
ValueError: If the magnitude of the current vector is zero (zero vector).
|
|
224
|
+
|
|
225
|
+
## Example:
|
|
226
|
+
v = Vector2D(3, 4)
|
|
227
|
+
normalized_v = v.normalize() # Normalize the vector (3, 4)
|
|
228
|
+
print(normalized_v) # Output: (0.6, 0.8)
|
|
229
|
+
|
|
230
|
+
## Explanation:
|
|
231
|
+
This method calculates the normalized version of the current vector, which means a new vector with the same direction as the original but with a magnitude of 1.
|
|
232
|
+
|
|
233
|
+
The method first calculates the magnitude of the current vector using the 'magnitude' method.
|
|
234
|
+
|
|
235
|
+
If the magnitude is zero (zero vector), a ValueError is raised, as normalization is not defined for zero vectors.
|
|
236
|
+
|
|
237
|
+
The normalized vector is obtained by dividing each component of the current vector by its magnitude.
|
|
238
|
+
|
|
239
|
+
The resulting normalized vector is returned.
|
|
240
|
+
|
|
241
|
+
Example usage is shown in the "Example" section above.
|
|
242
|
+
"""
|
|
243
|
+
...
|
|
244
|
+
|
|
245
|
+
@property
|
|
246
|
+
def length(self:"Vector2D") -> float:
|
|
247
|
+
...
|
|
248
|
+
|
|
249
|
+
def floor(self:"Vector2D", n:"int|float|Vector2D"=1) -> "Vector2D":
|
|
250
|
+
...
|
|
251
|
+
|
|
252
|
+
def ceil(self:"Vector2D", n:"int|float|Vector2D"=1) -> "Vector2D":
|
|
253
|
+
...
|
|
254
|
+
|
|
255
|
+
def round(self:"Vector2D", n:"int|float|Vector2D"=1) -> "Vector2D":
|
|
256
|
+
...
|
|
257
|
+
|
|
258
|
+
@classmethod
|
|
259
|
+
def randomize(cls, start:"int|float|Vector2D", end:"int|float|Vector2D") -> "Vector2D":
|
|
260
|
+
"""
|
|
261
|
+
# Generate a random Vector2D point within the specified range.
|
|
262
|
+
|
|
263
|
+
## Parameters:
|
|
264
|
+
start (int|float or Vector2D or None, optional): The starting point of the range.
|
|
265
|
+
Default is None, which corresponds to (0, 0).
|
|
266
|
+
If numeric, both x and y will have the same value.
|
|
267
|
+
end (int|float or Vector2D or None, optional): The ending point of the range.
|
|
268
|
+
Default is None, which corresponds to (1, 1).
|
|
269
|
+
If numeric, both x and y will have the same value.
|
|
270
|
+
|
|
271
|
+
## Returns:
|
|
272
|
+
Vector2D: A new random Vector2D point within the specified range.
|
|
273
|
+
|
|
274
|
+
## Example:
|
|
275
|
+
random_point = randomize(Vector2D(10, 20), Vector2D(50, 70))
|
|
276
|
+
|
|
277
|
+
print(random_point.x, random_point.y)
|
|
278
|
+
|
|
279
|
+
This will print a random point between (10, 20) and (50, 70).
|
|
280
|
+
|
|
281
|
+
## Explanation:
|
|
282
|
+
The function generates a random Vector2D point within the specified range defined by `start` and `end`.
|
|
283
|
+
|
|
284
|
+
If `start` and `end` are numeric values (int or float), both x and y coordinates will have the same value.
|
|
285
|
+
|
|
286
|
+
If `start` and `end` are None, the default range is assumed to be (0, 0) to (1, 1).
|
|
287
|
+
|
|
288
|
+
The function first checks if `start` and `end` are Vector2D others. If not, it creates new Vector2D others
|
|
289
|
+
based on the numeric values provided or the default values.
|
|
290
|
+
|
|
291
|
+
It then generates random x and y coordinates in the range [0, 1) using the `random()` function from the `random` module.
|
|
292
|
+
These random values are then scaled by (end - start) and added to the start point to obtain the final random Vector2D point.
|
|
293
|
+
"""
|
|
294
|
+
...
|
|
295
|
+
|
|
296
|
+
def dot_product(self:"Vector2D", other:"Vector2D") -> float:
|
|
297
|
+
"""
|
|
298
|
+
# Calculate the dot product of the current vector with another vector.
|
|
299
|
+
|
|
300
|
+
## Parameters:
|
|
301
|
+
other (Vector2D): The other vector for the dot product calculation.
|
|
302
|
+
|
|
303
|
+
## Returns:
|
|
304
|
+
float: The dot product value.
|
|
305
|
+
|
|
306
|
+
## Example:
|
|
307
|
+
v1 = Vector2D(2, 3)
|
|
308
|
+
v2 = Vector2D(4, -1)
|
|
309
|
+
result = v1.dot_product(v2)
|
|
310
|
+
print(result) # Output: 5
|
|
311
|
+
|
|
312
|
+
## Explanation:
|
|
313
|
+
The dot product of two vectors (A and B) is given by the formula: dot_product = A.x * B.x + A.y * B.y
|
|
314
|
+
|
|
315
|
+
The method takes another vector (other) as input and returns the dot product value.
|
|
316
|
+
|
|
317
|
+
Example usage is shown in the "Example" section above.
|
|
318
|
+
"""
|
|
319
|
+
...
|
|
320
|
+
|
|
321
|
+
def projection(self:"Vector2D", other:"Vector2D") -> "Vector2D":
|
|
322
|
+
"""
|
|
323
|
+
# Vector Projection
|
|
324
|
+
|
|
325
|
+
## Parameters:
|
|
326
|
+
other (float, int, Vector2D, V2, list, tuple): The vector onto which to project.
|
|
327
|
+
|
|
328
|
+
## Returns:
|
|
329
|
+
Vector2D or V2: The projection of the current vector onto the 'other' vector.
|
|
330
|
+
|
|
331
|
+
## Raises:
|
|
332
|
+
ValueError: If 'other' is a zero vector.
|
|
333
|
+
|
|
334
|
+
## Example:
|
|
335
|
+
v1 = Vector2D(3, 4)
|
|
336
|
+
v2 = Vector2D(1, 0)
|
|
337
|
+
projection_v = v1.projection(v2) # Calculate the projection of v1 onto v2
|
|
338
|
+
print(projection_v) # Output: (3.0, 0.0)
|
|
339
|
+
|
|
340
|
+
## Explanation:
|
|
341
|
+
This method calculates the projection of the current vector onto the 'other' vector.
|
|
342
|
+
The projection is a vector that represents the component of the current vector in the direction of the 'other' vector.
|
|
343
|
+
|
|
344
|
+
If 'other' is not a Vector2D instance, it will be converted to one using the '__normalize__' method.
|
|
345
|
+
The method first normalizes the 'other' vector using the '__normalize__' method of the vector.
|
|
346
|
+
|
|
347
|
+
Next, it calculates the dot product of the current vector and the normalized 'other' vector using the 'dot_product' method.
|
|
348
|
+
It also calculates the squared magnitude of the 'other' vector using the 'magnitude' method.
|
|
349
|
+
|
|
350
|
+
If the magnitude of 'other' is zero (a zero vector), a ValueError is raised, as projection is not defined for zero vectors.
|
|
351
|
+
|
|
352
|
+
The projection is then obtained by scaling the 'other' vector by the dot product divided by the squared magnitude.
|
|
353
|
+
|
|
354
|
+
The resulting projection vector is returned.
|
|
355
|
+
|
|
356
|
+
Example usage is shown in the "Example" section above.
|
|
357
|
+
"""
|
|
358
|
+
...
|
|
359
|
+
|
|
360
|
+
def reflection(self:"Vector2D", normal:"Vector2D") -> "Vector2D":
|
|
361
|
+
"""
|
|
362
|
+
# Vector Reflection
|
|
363
|
+
|
|
364
|
+
## Parameters:
|
|
365
|
+
normal (float, int, Vector2D, V2, list, tuple): The normal vector representing the surface of reflection.
|
|
366
|
+
|
|
367
|
+
## Returns:
|
|
368
|
+
Vector2D or V2: The reflected vector.
|
|
369
|
+
|
|
370
|
+
## Example:
|
|
371
|
+
incident_vector = Vector2D(3, 4)
|
|
372
|
+
normal_vector = Vector2D(1, 0)
|
|
373
|
+
reflected_vector = incident_vector.reflection(normal_vector) # Calculate the reflection of the incident vector over the given normal
|
|
374
|
+
print(reflected_vector) # Output: (-3.0, 4.0)
|
|
375
|
+
|
|
376
|
+
## Explanation:
|
|
377
|
+
This method calculates the reflection of the current vector over the given normal vector.
|
|
378
|
+
The normal vector represents the surface of reflection, and it should be normalized (unit vector).
|
|
379
|
+
|
|
380
|
+
The method first normalizes the 'normal' vector using the '__normalize__' method of the vector.
|
|
381
|
+
Next, it calculates the projection of the current vector onto the 'normal' vector using the 'projection' method.
|
|
382
|
+
The reflected vector is obtained by subtracting twice the projection from the current vector.
|
|
383
|
+
|
|
384
|
+
The resulting reflected vector is returned.
|
|
385
|
+
|
|
386
|
+
Example usage is shown in the "Example" section above.
|
|
387
|
+
"""
|
|
388
|
+
...
|
|
389
|
+
|
|
390
|
+
def cartesian_to_polar(self:"Vector2D") -> tuple:
|
|
391
|
+
"""
|
|
392
|
+
# Convert Cartesian Coordinates to Polar Coordinates
|
|
393
|
+
|
|
394
|
+
## Returns:
|
|
395
|
+
tuple: A tuple containing the radial distance (magnitude) 'r' and the angle 'theta' in radians.
|
|
396
|
+
|
|
397
|
+
## Example:
|
|
398
|
+
v = Vector2D(3, 4)
|
|
399
|
+
r, theta = v.cartesian_to_polar() # Convert Cartesian coordinates (3, 4) to polar
|
|
400
|
+
print(r, theta) # Output: (5.0, 0.9272952180016122)
|
|
401
|
+
|
|
402
|
+
## Explanation:
|
|
403
|
+
This method converts Cartesian coordinates (x, y) to polar coordinates (r, theta).
|
|
404
|
+
'r' is the radial distance (magnitude) from the origin to the point, and 'theta' is the angle
|
|
405
|
+
(in radians) measured from the positive x-axis to the point.
|
|
406
|
+
|
|
407
|
+
The method calculates the radial distance 'r' using the 'magnitude' method of the vector.
|
|
408
|
+
The angle 'theta' is calculated using the arctan2 function, which takes the y and x components of the vector.
|
|
409
|
+
|
|
410
|
+
The resulting 'r' and 'theta' are returned as a tuple.
|
|
411
|
+
|
|
412
|
+
Example usage is shown in the "Example" section above.
|
|
413
|
+
"""
|
|
414
|
+
...
|
|
415
|
+
|
|
416
|
+
@classmethod
|
|
417
|
+
def polar_to_cartesian(cls, r: int|float, theta: int|float) -> "Vector2D":
|
|
418
|
+
"""
|
|
419
|
+
# Convert Polar Coordinates to Cartesian Coordinates
|
|
420
|
+
|
|
421
|
+
## Parameters:
|
|
422
|
+
r (float or int): The radial distance (magnitude) from the origin to the point.
|
|
423
|
+
theta (float or int): The angle (in radians or degrees) measured from the positive x-axis to the point.
|
|
424
|
+
|
|
425
|
+
## Returns:
|
|
426
|
+
Vector2D or V2: A new vector representing the Cartesian coordinates (x, y) of the point.
|
|
427
|
+
|
|
428
|
+
## Example:
|
|
429
|
+
cartesian_point = Vector2D.polar_to_cartesian(5, math.pi/4) # Convert polar coordinates (r=5, theta=45 degrees) to Cartesian
|
|
430
|
+
print(cartesian_point) # Output: (3.5355339059327378, 3.5355339059327373)
|
|
431
|
+
|
|
432
|
+
## Explanation:
|
|
433
|
+
This class method converts polar coordinates (r, theta) to Cartesian coordinates (x, y).
|
|
434
|
+
'r' is the radial distance (magnitude) from the origin to the point, and 'theta' is the angle
|
|
435
|
+
(in radians or degrees) measured from the positive x-axis to the point.
|
|
436
|
+
|
|
437
|
+
The method calculates the x and y components using trigonometric functions (cosine and sine) based on 'r' and 'theta'.
|
|
438
|
+
|
|
439
|
+
Example usage is shown in the "Example" section above.
|
|
440
|
+
"""
|
|
441
|
+
...
|
|
442
|
+
|
|
443
|
+
def cartesian_to_complex(self:"Vector2D") -> complex: ...
|
|
444
|
+
|
|
445
|
+
@classmethod
|
|
446
|
+
def complex_to_cartesian(cls, complex_n: complex) -> "Vector2D": ...
|
|
447
|
+
|
|
448
|
+
def lerp(self:"Vector2D", other:"int|float|Vector2D|list|tuple", t:float=.1) -> "Vector2D":
|
|
449
|
+
"""
|
|
450
|
+
# Linear Interpolation (LERP)
|
|
451
|
+
|
|
452
|
+
## Parameters:
|
|
453
|
+
other (float, int, Vector2D, V2, list, tuple): The vector to interpolate towards.
|
|
454
|
+
t (float): The interpolation parameter. Must be between 0 and 1.
|
|
455
|
+
|
|
456
|
+
## Returns:
|
|
457
|
+
Vector2D or V2: The result of the linear interpolation.
|
|
458
|
+
|
|
459
|
+
## Raises:
|
|
460
|
+
ValueError: If t is not within the range [0, 1].
|
|
461
|
+
|
|
462
|
+
## Example:
|
|
463
|
+
v1 = Vector2D(1, 2)
|
|
464
|
+
v2 = Vector2D(5, 7)
|
|
465
|
+
interpolated_v = v1.lerp(v2, 0.5) # Linearly interpolate between v1 and v2 with t = 0.5
|
|
466
|
+
print(interpolated_v) # Output: (3.0, 4.5)
|
|
467
|
+
|
|
468
|
+
## Explanation:
|
|
469
|
+
This method performs linear interpolation between the current vector and the 'other' vector.
|
|
470
|
+
The 't' parameter represents the interpolation parameter, which controls how much the interpolation
|
|
471
|
+
leans towards the 'other' vector. When 't' is 0, the result will be equal to the current vector (self).
|
|
472
|
+
When 't' is 1, the result will be equal to the 'other' vector. For intermediate values of 't', the
|
|
473
|
+
result will be a linear combination of the two vectors, smoothly transitioning between them.
|
|
474
|
+
|
|
475
|
+
If 'other' is not a Vector2D instance, it will be converted to one using the '__normalize__' method.
|
|
476
|
+
If 't' is not within the range [0, 1], a ValueError is raised.
|
|
477
|
+
|
|
478
|
+
Example usage is shown in the "Example" section above.
|
|
479
|
+
"""
|
|
480
|
+
...
|
|
481
|
+
|
|
482
|
+
def rotate(self:"Vector2D", angle: int|float, center:"Vector2D|None"=None) -> "Vector2D":
|
|
483
|
+
"""
|
|
484
|
+
# Rotate the vector by a given angle around the origin or a specified center.
|
|
485
|
+
|
|
486
|
+
## Parameters:
|
|
487
|
+
angle (int or float): The angle of rotation in radians or degrees, depending on the trigonometric functions used.
|
|
488
|
+
center (float, int, Vector2D, V2, list, tuple, or None): The center of rotation.
|
|
489
|
+
If None, the vector is rotated around the origin (0, 0).
|
|
490
|
+
|
|
491
|
+
## Returns:
|
|
492
|
+
Vector2D or V2: The rotated vector.
|
|
493
|
+
|
|
494
|
+
## Example:
|
|
495
|
+
v = Vector2D(3, 4)
|
|
496
|
+
rotated_v = v.rotate(math.pi / 4) # Rotate 45 degrees around the origin
|
|
497
|
+
print(rotated_v) # Output: (0.7071067811865476, 5.656854249492381)
|
|
498
|
+
|
|
499
|
+
center = Vector2D(1, 1)
|
|
500
|
+
rotated_v = v.rotate(math.pi / 4, center) # Rotate 45 degrees around the center (1, 1)
|
|
501
|
+
print(rotated_v) # Output: (1.7071067811865475, 2.656854249492381)
|
|
502
|
+
|
|
503
|
+
## Explanation:
|
|
504
|
+
This method rotates the vector by the specified angle around the given center.
|
|
505
|
+
If no center is provided, the vector is rotated around the origin (0, 0).
|
|
506
|
+
|
|
507
|
+
The method calculates the trigonometric functions (cosine and sine) of the angle to perform the rotation.
|
|
508
|
+
The translated vector is obtained by subtracting the center from the current vector.
|
|
509
|
+
The rotated vector is then obtained by applying the rotation transformation to the translated vector.
|
|
510
|
+
The center is added back to the rotated vector to obtain the final result.
|
|
511
|
+
|
|
512
|
+
Example usage is shown in the "Example" section above.
|
|
513
|
+
"""
|
|
514
|
+
...
|
|
515
|
+
|
|
516
|
+
def no_zero_div_error(self:"Vector2D", n:"int|float|Vector2D", error_mode:Literal["zero", "null", "nan"]="zero") -> "Vector2D":
|
|
517
|
+
"""
|
|
518
|
+
# Handle division between the Vector2D other and a numeric value or another Vector2D other.
|
|
519
|
+
|
|
520
|
+
## Parameters:
|
|
521
|
+
n (int|float or Vector2D): The numeric value or Vector2D other for division.
|
|
522
|
+
error_mode (str, optional): The mode to handle division by zero scenarios.
|
|
523
|
+
- "zero" (default): Return a Vector2D other with zeros for both components.
|
|
524
|
+
- "null": Return a Vector2D other with the original x or y component if available,
|
|
525
|
+
otherwise, return NaN (Not a Number) for the component.
|
|
526
|
+
|
|
527
|
+
## Returns:
|
|
528
|
+
Vector2D: A new Vector2D other after division or handling division by zero scenarios.
|
|
529
|
+
|
|
530
|
+
## Example:
|
|
531
|
+
vector1 = Vector2D(3, 4)
|
|
532
|
+
|
|
533
|
+
result1 = vector1.no_zero_div_error(2)
|
|
534
|
+
|
|
535
|
+
print(result1.x, result1.y)
|
|
536
|
+
|
|
537
|
+
vector2 = Vector2D(5, 0)
|
|
538
|
+
|
|
539
|
+
result2 = vector1.no_zero_div_error(vector2, error_mode="null")
|
|
540
|
+
|
|
541
|
+
print(result2.x, result2.y)
|
|
542
|
+
|
|
543
|
+
## Explanation:
|
|
544
|
+
The function handles division between the Vector2D other and a numeric value or another Vector2D other.
|
|
545
|
+
|
|
546
|
+
If n is a numeric value (int or float):
|
|
547
|
+
- If n is zero, the function returns a Vector2D other with zeros for both components if error_mode is "zero".
|
|
548
|
+
- If error_mode is "null", the function returns a Vector2D other with the original x or y component if available,
|
|
549
|
+
otherwise, return NaN (Not a Number) for the component.
|
|
550
|
+
|
|
551
|
+
If n is a Vector2D other:
|
|
552
|
+
- If n's x or y component is zero, the function returns a Vector2D other with zeros for the corresponding component
|
|
553
|
+
if error_mode is "zero".
|
|
554
|
+
- If error_mode is "null", the function returns a Vector2D other with the original x or y component if available,
|
|
555
|
+
otherwise, return NaN (Not a Number) for the component.
|
|
556
|
+
|
|
557
|
+
If n is neither a numeric value nor a Vector2D other, the function raises an exception.
|
|
558
|
+
"""
|
|
559
|
+
...
|
|
560
|
+
|
|
561
|
+
def min(self:"Vector2D", other:"Vector2D") -> "Vector2D": ...
|
|
562
|
+
|
|
563
|
+
def max(self:"Vector2D", other:"Vector2D") -> "Vector2D": ...
|
|
564
|
+
|
|
565
|
+
def advanced_stringify(self:"Vector2D", precision: float|None=None, use_scientific_notation:bool=False, return_as_list=False) -> str|list[str]:
|
|
566
|
+
def optimize(value: int|float) -> str: ...
|
|
567
|
+
...
|
|
568
|
+
|
|
569
|
+
def __str__(self:"Vector2D") -> str: ...
|
|
570
|
+
|
|
571
|
+
def __repr__(self:"Vector2D") -> str: ...
|
|
572
|
+
|
|
573
|
+
def __call__(self:"Vector2D") -> list: ...
|
|
574
|
+
|
|
575
|
+
# fast operations Vector2D.operation(both,x,y)
|
|
576
|
+
def add(self, both:int|float=.0, x:int|float=.0, y:int|float=.0) -> Vector2D: ...
|
|
577
|
+
|
|
578
|
+
def sub(self, both:int|float=.0, x:int|float=.0, y:int|float=.0) -> Vector2D: ...
|
|
579
|
+
|
|
580
|
+
def mult(self, both:int|float=.0, x:int|float=.0, y:int|float=.0) -> Vector2D: ...
|
|
581
|
+
|
|
582
|
+
def pow(self, both:int|float=.0, x:int|float=.0, y:int|float=.0) -> Vector2D: ...
|
|
583
|
+
|
|
584
|
+
def mod(self, both:int|float=.0, x:int|float=.0, y:int|float=.0) -> Vector2D: ...
|
|
585
|
+
|
|
586
|
+
def div(self, both:int|float=.0, x:int|float=.0, y:int|float=.0) -> Vector2D: ...
|
|
587
|
+
|
|
588
|
+
def fdiv(self, both:int|float=.0, x:int|float=.0, y:int|float=.0) -> Vector2D: ...
|
|
589
|
+
|
|
590
|
+
# fast inplace operations Vector2D.ioperation(both,x,y)
|
|
591
|
+
def set(self, both=int|float, x:int|float, y:int|float) -> Vector2D: ...
|
|
592
|
+
|
|
593
|
+
def iadd(self, both:int|float, x:int|float, y=int|float) -> Vector2D: ...
|
|
594
|
+
|
|
595
|
+
def isub(self, both:int|float, x:int|float, y=int|float) -> Vector2D: ...
|
|
596
|
+
|
|
597
|
+
def imult(self, both:int|float, x:int|float, y=int|float) -> Vector2D: ...
|
|
598
|
+
|
|
599
|
+
def ipow(self, both:int|float, x:int|float, y=int|float) -> Vector2D: ...
|
|
600
|
+
|
|
601
|
+
def imod(self, both:int|float, x:int|float, y=int|float) -> Vector2D: ...
|
|
602
|
+
|
|
603
|
+
def idiv(self, both:int|float, x:int|float, y=int|float) -> Vector2D: ...
|
|
604
|
+
|
|
605
|
+
def ifdiv(self, both:int|float, x:int|float, y=int|float) -> Vector2D: ...
|
|
606
|
+
|
|
607
|
+
# normal operations Vector2D + a
|
|
608
|
+
def __add__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
609
|
+
|
|
610
|
+
def __sub__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
611
|
+
|
|
612
|
+
def __mul__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
613
|
+
|
|
614
|
+
def __mod__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
615
|
+
|
|
616
|
+
def __pow__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
617
|
+
|
|
618
|
+
def __truediv__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
619
|
+
|
|
620
|
+
def __floordiv__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
621
|
+
|
|
622
|
+
# right operations a + Vector2D
|
|
623
|
+
def __radd__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
624
|
+
|
|
625
|
+
def __rsub__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
626
|
+
|
|
627
|
+
def __rmul__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
628
|
+
|
|
629
|
+
def __rmod__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
630
|
+
|
|
631
|
+
def __rpow__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
632
|
+
|
|
633
|
+
def __rtruediv__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
634
|
+
|
|
635
|
+
def __rfloordiv__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
636
|
+
|
|
637
|
+
# in-place operations Vector2D += a
|
|
638
|
+
def __iadd__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
639
|
+
|
|
640
|
+
def __isub__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
641
|
+
|
|
642
|
+
def __imul__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
643
|
+
|
|
644
|
+
def __itruediv__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
645
|
+
|
|
646
|
+
def __imod__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
647
|
+
|
|
648
|
+
def __ipow__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
649
|
+
|
|
650
|
+
def __ifloordiv__(self:"Vector2D", other:"int|float|Vector2D|list|tuple") -> "Vector2D": ...
|
|
651
|
+
|
|
652
|
+
# comparasion
|
|
653
|
+
def __eq__(self:"Vector2D", other) -> bool: ...
|
|
654
|
+
|
|
655
|
+
def __ne__(self:"Vector2D", other) -> bool: ...
|
|
656
|
+
|
|
657
|
+
def __abs__(self:"Vector2D") -> "Vector2D": ...
|
|
658
|
+
|
|
659
|
+
def __round__(self:"Vector2D", n:"int|float|Vector2D"=1) -> "Vector2D": ...
|
|
660
|
+
|
|
661
|
+
def __floor__(self:"Vector2D", n:"int|float|Vector2D"=1) -> "Vector2D": ...
|
|
662
|
+
|
|
663
|
+
def __ceil__(self:"Vector2D", n:"int|float|Vector2D"=1) -> "Vector2D": ...
|
|
664
|
+
|
|
665
|
+
def __float__(self:"Vector2D") -> "Vector2D": ...
|
|
666
|
+
|
|
667
|
+
def __getitem__(self:"Vector2D", n) -> int|float: ...
|
|
668
|
+
|
|
669
|
+
@classmethod
|
|
670
|
+
def __normalize__(cls, other:"Vector2D|int|float|tuple|list") -> "Vector2D": ...
|
|
671
|
+
|
|
672
|
+
@classmethod
|
|
673
|
+
def zero(cls) -> "Vector2D": ...
|
|
674
|
+
@classmethod
|
|
675
|
+
def one(cls) -> "Vector2D": ...
|
|
676
|
+
@classmethod
|
|
677
|
+
def two(cls) -> "Vector2D": ...
|
|
678
|
+
@classmethod
|
|
679
|
+
def pi(cls) -> "Vector2D": ...
|
|
680
|
+
@classmethod
|
|
681
|
+
def inf(cls) -> "Vector2D": ...
|
|
682
|
+
@classmethod
|
|
683
|
+
def neg_one(cls) -> "Vector2D": ...
|
|
684
|
+
@classmethod
|
|
685
|
+
def neg_two(cls) -> "Vector2D": ...
|
|
686
|
+
@classmethod
|
|
687
|
+
def neg_pi(cls) -> "Vector2D": ...
|
|
688
|
+
@classmethod
|
|
689
|
+
def neg_inf(cls) -> "Vector2D": ...
|
|
690
|
+
@classmethod
|
|
691
|
+
def up(cls) -> "Vector2D": ...
|
|
692
|
+
@classmethod
|
|
693
|
+
def right(cls) -> "Vector2D": ...
|
|
694
|
+
@classmethod
|
|
695
|
+
def down(cls) -> "Vector2D": ...
|
|
696
|
+
@classmethod
|
|
697
|
+
def left(cls) -> "Vector2D": ...
|
|
698
|
+
@classmethod
|
|
699
|
+
def up_right(cls) -> "Vector2D": ...
|
|
700
|
+
@classmethod
|
|
701
|
+
def down_right(cls) -> "Vector2D": ...
|
|
702
|
+
@classmethod
|
|
703
|
+
def up_left(cls) -> "Vector2D": ...
|
|
704
|
+
@classmethod
|
|
705
|
+
def down_left(cls) -> "Vector2D": ...
|
|
706
|
+
@classmethod
|
|
707
|
+
def up_right_norm(cls) -> "Vector2D": ...
|
|
708
|
+
@classmethod
|
|
709
|
+
def down_right_norm(cls) -> "Vector2D": ...
|
|
710
|
+
@classmethod
|
|
711
|
+
def up_left_norm(cls) -> "Vector2D": ...
|
|
712
|
+
@classmethod
|
|
713
|
+
def down_left_norm(cls) -> "Vector2D": ...
|
|
714
|
+
|
|
715
|
+
@classmethod
|
|
716
|
+
def new_zero(cls) -> "Vector2D": ...
|
|
717
|
+
@classmethod
|
|
718
|
+
def new_one(cls) -> "Vector2D": ...
|
|
719
|
+
@classmethod
|
|
720
|
+
def new_two(cls) -> "Vector2D": ...
|
|
721
|
+
@classmethod
|
|
722
|
+
def new_pi(cls) -> "Vector2D": ...
|
|
723
|
+
@classmethod
|
|
724
|
+
def new_inf(cls) -> "Vector2D": ...
|
|
725
|
+
@classmethod
|
|
726
|
+
def new_neg_one(cls) -> "Vector2D": ...
|
|
727
|
+
@classmethod
|
|
728
|
+
def new_neg_two(cls) -> "Vector2D": ...
|
|
729
|
+
@classmethod
|
|
730
|
+
def new_neg_pi(cls) -> "Vector2D": ...
|
|
731
|
+
@classmethod
|
|
732
|
+
def new_neg_inf(cls) -> "Vector2D": ...
|
|
733
|
+
@classmethod
|
|
734
|
+
def new_up(cls) -> "Vector2D": ...
|
|
735
|
+
@classmethod
|
|
736
|
+
def new_right(cls) -> "Vector2D": ...
|
|
737
|
+
@classmethod
|
|
738
|
+
def new_down(cls) -> "Vector2D": ...
|
|
739
|
+
@classmethod
|
|
740
|
+
def new_left(cls) -> "Vector2D": ...
|
|
741
|
+
@classmethod
|
|
742
|
+
def new_up_right(cls) -> "Vector2D": ...
|
|
743
|
+
@classmethod
|
|
744
|
+
def new_down_right(cls) -> "Vector2D": ...
|
|
745
|
+
@classmethod
|
|
746
|
+
def new_up_left(cls) -> "Vector2D": ...
|
|
747
|
+
@classmethod
|
|
748
|
+
def new_down_left(cls) -> "Vector2D": ...
|
|
749
|
+
@classmethod
|
|
750
|
+
def new_up_right_norm(cls) -> "Vector2D": ...
|
|
751
|
+
@classmethod
|
|
752
|
+
def new_down_right_norm(cls) -> "Vector2D": ...
|
|
753
|
+
@classmethod
|
|
754
|
+
def new_up_left_norm(cls) -> "Vector2D": ...
|
|
755
|
+
@classmethod
|
|
756
|
+
def new_down_left_norm(cls) -> "Vector2D": ...
|
|
757
|
+
|
|
758
|
+
V2 = Vector2D
|
|
759
|
+
|
|
760
|
+
V2zero : Vector2D
|
|
761
|
+
|
|
762
|
+
V2one : Vector2D
|
|
763
|
+
V2two : Vector2D
|
|
764
|
+
V2pi : Vector2D
|
|
765
|
+
V2inf : Vector2D
|
|
766
|
+
|
|
767
|
+
V2neg_one : Vector2D
|
|
768
|
+
V2neg_two : Vector2D
|
|
769
|
+
V2neg_pi : Vector2D
|
|
770
|
+
V2neg_inf : Vector2D
|
|
771
|
+
|
|
772
|
+
V2up : Vector2D
|
|
773
|
+
V2right : Vector2D
|
|
774
|
+
V2down : Vector2D
|
|
775
|
+
V2left : Vector2D
|
|
776
|
+
|
|
777
|
+
V2up_right : Vector2D
|
|
778
|
+
V2down_right : Vector2D
|
|
779
|
+
V2up_left : Vector2D
|
|
780
|
+
V2down_left : Vector2D
|
|
781
|
+
|
|
782
|
+
V2up_right_norm : Vector2D
|
|
783
|
+
V2down_right_norm : Vector2D
|
|
784
|
+
V2up_left_norm : Vector2D
|
|
785
|
+
V2down_left_norm : Vector2D
|
|
786
|
+
|
|
787
|
+
VECTORS_4_DIRECTIONS : tuple[Vector2D, Vector2D, Vector2D, Vector2D]
|
|
788
|
+
VECTORS_4_SEMIDIRECTIONS : tuple[Vector2D, Vector2D, Vector2D, Vector2D]
|
|
789
|
+
VECTORS_4_SEMIDIRECTIONS_NORM : tuple[Vector2D, Vector2D, Vector2D, Vector2D,]
|
|
790
|
+
VECTORS_8_DIRECTIONS : tuple[Vector2D, Vector2D, Vector2D, Vector2D, Vector2D, Vector2D, Vector2D, Vector2D]
|
|
791
|
+
VECTORS_8_DIRECTIONS_NORM : tuple[Vector2D, Vector2D, Vector2D, Vector2D, Vector2D, Vector2D, Vector2D, Vector2D]
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
def rgb(r:float, g:float, b:float) -> tuple[float, float, float]:
|
|
795
|
+
return (r,g,b)
|
|
796
|
+
|
|
797
|
+
def lerp(starting: int|float, ending: int|float, step: int|float=.1) -> float: ...
|
|
798
|
+
|
|
799
|
+
# def color_lerp(current_c: list|tuple, final_c: list|tuple, step: int|float=.1) -> tuple[float, float, float]:
|
|
800
|
+
# """
|
|
801
|
+
# # Linearly interpolate between two colors.
|
|
802
|
+
|
|
803
|
+
# ## Parameters:
|
|
804
|
+
# current_c (tuple or list): The RGB values of the current color as a tuple or list.
|
|
805
|
+
# final_c (tuple or list): The RGB values of the target color as a tuple or list.
|
|
806
|
+
# step (int or float): The interpolation step, ranging from 0.0 (current color) to 1.0 (target color).
|
|
807
|
+
|
|
808
|
+
# ## Returns:
|
|
809
|
+
# tuple: The RGB values of the interpolated color as a tuple.
|
|
810
|
+
|
|
811
|
+
# ## Example:
|
|
812
|
+
# current_c = (255, 0, 0)
|
|
813
|
+
|
|
814
|
+
# final_c = (0, 0, 255)
|
|
815
|
+
|
|
816
|
+
# step = 0.5
|
|
817
|
+
|
|
818
|
+
# interpolated_color = color_lerp(current_c, final_c, step)
|
|
819
|
+
|
|
820
|
+
# print(f"At step {step}: RGB {interpolated_color}")
|
|
821
|
+
|
|
822
|
+
# This will calculate the color at an interpolation step of 0.5 between (255, 0, 0) and (0, 0, 255).
|
|
823
|
+
# """
|
|
824
|
+
# return tuple(c + (final_c[i] - c) * step for i,c in enumerate(current_c)) #type: ignore
|
|
825
|
+
|
|
826
|
+
# def color_fade(starting_c: list|tuple, final_c: list|tuple, index: int|float, max_index: int|float) -> tuple[float, float, float]:
|
|
827
|
+
# """
|
|
828
|
+
# # Calculate the color at a specific index of a color fade between two given colors.
|
|
829
|
+
|
|
830
|
+
# ## Parameters:
|
|
831
|
+
# starting_c (tuple or list): The RGB values of the starting color as a tuple or list.
|
|
832
|
+
# final_c (tuple or list): The RGB values of the final color as a tuple or list.
|
|
833
|
+
# index (int or float): The current index of the color fade, representing a position
|
|
834
|
+
# between the starting and final colors.
|
|
835
|
+
# max_index (int or float): The maximum index of the color fade, indicating the endpoint
|
|
836
|
+
# position between the starting and final colors.
|
|
837
|
+
|
|
838
|
+
# ## Returns:
|
|
839
|
+
# tuple: The RGB values of the color at the specified index as a tuple.
|
|
840
|
+
|
|
841
|
+
# ## Example:
|
|
842
|
+
# starting_c = (255, 0, 0)
|
|
843
|
+
|
|
844
|
+
# final_c = (0, 0, 255)
|
|
845
|
+
|
|
846
|
+
# max_index = 100
|
|
847
|
+
|
|
848
|
+
# for i in range(max_index + 1):
|
|
849
|
+
|
|
850
|
+
# color_at_index = color_fade(starting_c, final_c, i, max_index)
|
|
851
|
+
|
|
852
|
+
# print(f"At index {i}: RGB {color_at_index}")
|
|
853
|
+
|
|
854
|
+
# This will print the colors transitioning from (255, 0, 0) to (0, 0, 255).
|
|
855
|
+
# """
|
|
856
|
+
# return tuple((starting_c[i] - final_c[i]) / max_index * (max_index - index) + final_c[i] for i in range(3)) #type: ignore
|
|
857
|
+
|
|
858
|
+
# def weighted_color_fade(colors_dict:dict) -> tuple[float, float, float]:
|
|
859
|
+
# """
|
|
860
|
+
# # Calculate the weighted color based on a dictionary of colors and their corresponding weights.
|
|
861
|
+
|
|
862
|
+
# ## Parameters:
|
|
863
|
+
# colors_dict (dict): A dictionary where keys represent RGB color values as tuples,
|
|
864
|
+
# and values represent the weights (floats) for each color.
|
|
865
|
+
|
|
866
|
+
# ## Returns:
|
|
867
|
+
# tuple: The RGB values of the calculated weighted color as a tuple.
|
|
868
|
+
|
|
869
|
+
# ## Example:
|
|
870
|
+
# colors_dict = {
|
|
871
|
+
|
|
872
|
+
# (255, 255, 255): 0.1,
|
|
873
|
+
|
|
874
|
+
# (0, 0, 0): 0.9,
|
|
875
|
+
|
|
876
|
+
# }
|
|
877
|
+
|
|
878
|
+
# weighted_color = weighted_color_fade(colors_dict)
|
|
879
|
+
|
|
880
|
+
# print(f"Weighted color: RGB {weighted_color}")
|
|
881
|
+
|
|
882
|
+
# This will print the weighted color based on the provided dictionary.
|
|
883
|
+
# """
|
|
884
|
+
# colors = colors_dict.keys()
|
|
885
|
+
# weights = colors_dict.values()
|
|
886
|
+
|
|
887
|
+
# if float("inf") in weights: return list(colors)[list(weights).index(float("inf"))]
|
|
888
|
+
# return tuple(sum(n[i]*w for n,w in zip(colors, weights)) / sum(weights) for i in range(3)) #type: ignore
|
|
889
|
+
|
|
890
|
+
# def color_distance(starting_c: list|tuple, final_c: list|tuple, sqrd:bool=True) -> float:
|
|
891
|
+
# """
|
|
892
|
+
# # Calculate the distance between two colors in RGB space.
|
|
893
|
+
|
|
894
|
+
# ## Parameters:
|
|
895
|
+
# starting_c (list or tuple): The RGB values of the starting color.
|
|
896
|
+
# final_c (list or tuple): The RGB values of the final color.
|
|
897
|
+
# sqrd (bool, optional): If True, return the squared distance. If False, return
|
|
898
|
+
# the actual distance. Default is True.
|
|
899
|
+
|
|
900
|
+
# ## Returns:
|
|
901
|
+
# float: The squared distance between the two colors if `sqrd` is True, otherwise
|
|
902
|
+
# the actual distance.
|
|
903
|
+
|
|
904
|
+
# ## Example:
|
|
905
|
+
# starting_c = [255, 0, 0]
|
|
906
|
+
|
|
907
|
+
# final_c = [0, 255, 0]
|
|
908
|
+
|
|
909
|
+
# squared_distance = color_distance(starting_c, final_c)
|
|
910
|
+
|
|
911
|
+
# print(f"Squared Distance: {squared_distance}")
|
|
912
|
+
|
|
913
|
+
# distance = color_distance(starting_c, final_c, sqrd=False)
|
|
914
|
+
|
|
915
|
+
# print(f"Actual Distance: {distance}")
|
|
916
|
+
|
|
917
|
+
# This will calculate the squared and actual distances between the colors.
|
|
918
|
+
|
|
919
|
+
# ## Explanation:
|
|
920
|
+
# The function first calculates the squared distance between the two colors in RGB
|
|
921
|
+
# space. It does this by computing the sum of the squared differences of the RGB
|
|
922
|
+
# components for each color. The squared distance is obtained by taking the square
|
|
923
|
+
# root of this sum.
|
|
924
|
+
|
|
925
|
+
# The `sqrd` parameter allows the user to choose between returning the squared
|
|
926
|
+
# distance or the actual distance. If `sqrd` is True, the function returns the
|
|
927
|
+
# squared distance, and if `sqrd` is False, it returns the actual distance.
|
|
928
|
+
# """
|
|
929
|
+
# distance = sum([(starting_c[i]-final_c[i])**2 for i in range(3)])
|
|
930
|
+
# return (distance ** .5) if sqrd else distance
|
|
931
|
+
|
|
932
|
+
def angular_interpolation(starting_angle: int|float, final_angle: int|float, step: int|float=.1) -> float:
|
|
933
|
+
"""
|
|
934
|
+
# Perform angular interpolation between two angles using the shortest distance.
|
|
935
|
+
|
|
936
|
+
## Parameters:
|
|
937
|
+
starting_angle (int or float): The initial angle in radians.
|
|
938
|
+
final_angle (int or float): The target angle in radians to interpolate towards.
|
|
939
|
+
step (int or float, optional): The step size for interpolation in radians. Default is 0.1.
|
|
940
|
+
|
|
941
|
+
## Returns:
|
|
942
|
+
float: The interpolated angle as a result of angular interpolation.
|
|
943
|
+
|
|
944
|
+
## Example:
|
|
945
|
+
starting_angle = 1.0
|
|
946
|
+
|
|
947
|
+
final_angle = 5.0
|
|
948
|
+
|
|
949
|
+
interpolated_angle = angular_interpolation(starting_angle, final_angle)
|
|
950
|
+
|
|
951
|
+
print(f"Interpolated angle: {interpolated_angle}")
|
|
952
|
+
|
|
953
|
+
This will print the interpolated angle using angular interpolation.
|
|
954
|
+
|
|
955
|
+
## Explanation:
|
|
956
|
+
The function calculates three distances between the `starting_angle` and the
|
|
957
|
+
`final_angle`. These distances represent possible angular interpolations:
|
|
958
|
+
1. The direct interpolation from `starting_angle` to `final_angle`.
|
|
959
|
+
2. The interpolation by taking a full circle (2 * pi) and then proceeding from
|
|
960
|
+
`starting_angle` to `final_angle`.
|
|
961
|
+
3. The interpolation by taking a full circle (2 * pi) in the opposite direction
|
|
962
|
+
and then proceeding from `starting_angle` to `final_angle`.
|
|
963
|
+
|
|
964
|
+
The function then chooses the shortest distance from the three options and returns
|
|
965
|
+
the interpolated angle obtained by multiplying the shortest distance by the `step`
|
|
966
|
+
value.
|
|
967
|
+
|
|
968
|
+
The `step` parameter controls the granularity of interpolation. Smaller `step` values
|
|
969
|
+
provide more fine-grained interpolation but may require more iterations.
|
|
970
|
+
"""
|
|
971
|
+
...
|
|
972
|
+
|
|
973
|
+
def bezier_cubic_interpolation(t: float, p0:Vector2D, p1:Vector2D) -> float: ...
|
|
974
|
+
|
|
975
|
+
def bezier_quadratic_interpolation(t: float, p0:Vector2D) -> float: ...
|
|
976
|
+
|
|
977
|
+
def avg_position(*others:"Vector2D") -> Vector2D:
|
|
978
|
+
"""
|
|
979
|
+
# Calculate the average position for a variable number of Vector2D others.
|
|
980
|
+
|
|
981
|
+
## Parameters:
|
|
982
|
+
*others (Vector2D): Variable number of Vector2D others representing positions.
|
|
983
|
+
|
|
984
|
+
## Returns:
|
|
985
|
+
Vector2D: The average position as a new Vector2D other.
|
|
986
|
+
|
|
987
|
+
## Example:
|
|
988
|
+
position1 = Vector2D(10, 20)
|
|
989
|
+
|
|
990
|
+
position2 = Vector2D(30, 40)
|
|
991
|
+
|
|
992
|
+
position3 = Vector2D(50, 60)
|
|
993
|
+
|
|
994
|
+
average_pos = avg_position(position1, position2, position3)
|
|
995
|
+
|
|
996
|
+
print(average_pos)
|
|
997
|
+
|
|
998
|
+
This will print the average position of the three Vector2D others.
|
|
999
|
+
|
|
1000
|
+
## Explanation:
|
|
1001
|
+
The function takes a variable number of Vector2D others as input, representing positions.
|
|
1002
|
+
It calculates the sum of all the Vector2D others using the `sum` function and then divides
|
|
1003
|
+
it by the total number of others (length of `others`) to find the average position.
|
|
1004
|
+
|
|
1005
|
+
The result is returned as a new Vector2D other representing the average position.
|
|
1006
|
+
"""
|
|
1007
|
+
...
|
|
1008
|
+
|
|
1009
|
+
def inter_points(ray: list["Vector2D"]|tuple["Vector2D", "Vector2D"], lines: list[tuple["Vector2D", "Vector2D"]], return_inter_lines:bool=False, sort:bool=False, return_empty:bool=False) -> list[tuple[Vector2D | None, tuple[Vector2D | V2, Vector2D | V2]]] | list[Vector2D | None] | list[Vector2D]:
|
|
1010
|
+
"""
|
|
1011
|
+
# Find intersection points between a ray or line segment and multiple line segments.
|
|
1012
|
+
|
|
1013
|
+
## Parameters:
|
|
1014
|
+
ray (list[Vector2D] | tuple[Vector2D, Vector2D]): The ray or line segment represented by two endpoints
|
|
1015
|
+
(start and end points).
|
|
1016
|
+
lines (list[tuple[Vector2D, Vector2D]]): A list of line segments represented by tuples of their endpoints.
|
|
1017
|
+
return_inter_lines (bool, optional): If True, return a list of tuples containing the intersection points and the
|
|
1018
|
+
corresponding intersecting line segment. Default is False.
|
|
1019
|
+
sort (bool, optional): If True, sort the intersection points by their distance from the ray's start point.
|
|
1020
|
+
Default is False.
|
|
1021
|
+
return_empty (bool, optional): If True, include None for line segments with no intersection. Default is False.
|
|
1022
|
+
|
|
1023
|
+
## Returns:
|
|
1024
|
+
list[tuple[Vector2D | None, tuple[Vector2D | V2, Vector2D | V2]]] | list[Vector2D | None]:
|
|
1025
|
+
- If return_inter_lines is True, returns a list of tuples, each containing:
|
|
1026
|
+
- The intersection point (Vector2D) if it exists, or None otherwise.
|
|
1027
|
+
- The corresponding intersecting line segment (tuple[Vector2D | V2, Vector2D | V2]).
|
|
1028
|
+
- If return_inter_lines is False, returns a list of intersection points (Vector2D) if they exist, or None otherwise.
|
|
1029
|
+
|
|
1030
|
+
# Example:
|
|
1031
|
+
ray = [Vector2D(1, 2), Vector2D(5, 3)]
|
|
1032
|
+
lines = [(Vector2D(2, 2), Vector2D(4, 4)), (Vector2D(3, 3), Vector2D(6, 2))]
|
|
1033
|
+
result = inter_points(ray, lines, return_inter_lines=True, sort=True)
|
|
1034
|
+
print(result)
|
|
1035
|
+
|
|
1036
|
+
## Explanation:
|
|
1037
|
+
The function finds the intersection points (if any) between the given ray (or line segment) and the provided list
|
|
1038
|
+
of line segments. The intersection points are returned in a list.
|
|
1039
|
+
|
|
1040
|
+
If return_inter_lines is True, the function returns a list of tuples, where each tuple contains the intersection point
|
|
1041
|
+
(Vector2D) and the corresponding intersecting line segment (tuple[Vector2D | V2, Vector2D | V2]). If return_inter_lines
|
|
1042
|
+
is False, the function returns only a list of intersection points (Vector2D) without the corresponding line segments.
|
|
1043
|
+
|
|
1044
|
+
If sort is True, the intersection points are sorted by their distance from the ray's start point. If sort is False,
|
|
1045
|
+
the intersection points are returned in the order they were found.
|
|
1046
|
+
|
|
1047
|
+
If return_empty is True, the function includes None for line segments with no intersection. If return_empty is False,
|
|
1048
|
+
line segments with no intersection are omitted from the result.
|
|
1049
|
+
|
|
1050
|
+
Example usage is shown in the "Example" section above.
|
|
1051
|
+
"""
|
|
1052
|
+
def lineLineIntersect(P0:"Vector2D", P1:"Vector2D", Q0:"Vector2D", Q1:"Vector2D") -> "Vector2D | None": ...
|
|
1053
|
+
...
|
|
1054
|
+
|
|
1055
|
+
def get_points(position:Vector2D, size:Vector2D, rotation: int|float=0, pos_in_middle:bool=True, return_list:bool=False, clockwise_return:bool=False) -> tuple["Vector2D", "Vector2D", "Vector2D", "Vector2D"] | tuple[list[int|float]|tuple[int|float], list[int|float]|tuple[int|float], list[int|float]|tuple[int|float], list[int|float]|tuple[int|float]]:
|
|
1056
|
+
"""
|
|
1057
|
+
# Generate points for a rectangle based on the given parameters.
|
|
1058
|
+
|
|
1059
|
+
## Parameters:
|
|
1060
|
+
position (Vector2D): The center position of the rectangle.
|
|
1061
|
+
size (Vector2D): The size of the rectangle (width and height).
|
|
1062
|
+
rotation (int|float, optional): The rotation angle in degrees. Default is 0.
|
|
1063
|
+
pos_in_middle (bool, optional): If True, the points represent corners of the rectangle.
|
|
1064
|
+
If False, the points represent the rectangle's edges.
|
|
1065
|
+
Default is True.
|
|
1066
|
+
return_list (bool, optional): If True, return the points as lists instead of Vector2D others.
|
|
1067
|
+
Default is False.
|
|
1068
|
+
clockwise_return (bool, optional): If True, return the points in clockwise order (A, B, D, C).
|
|
1069
|
+
If False, return the points in counterclockwise order (A, B, C, D).
|
|
1070
|
+
Default is False.
|
|
1071
|
+
|
|
1072
|
+
## Returns:
|
|
1073
|
+
tuple: A tuple containing the four points of the rectangle.
|
|
1074
|
+
|
|
1075
|
+
## Example:
|
|
1076
|
+
position = Vector2D(100, 100)
|
|
1077
|
+
|
|
1078
|
+
size = Vector2D(50, 30)
|
|
1079
|
+
|
|
1080
|
+
rotation = 45
|
|
1081
|
+
|
|
1082
|
+
points = get_points(position, size, rotation)
|
|
1083
|
+
|
|
1084
|
+
print(points)
|
|
1085
|
+
|
|
1086
|
+
This will print the four points of the rotated rectangle.
|
|
1087
|
+
|
|
1088
|
+
## Explanation:
|
|
1089
|
+
The function calculates the four points (A, B, C, D) of the rectangle based on the center position,
|
|
1090
|
+
size, rotation, and pos_in_middle parameters. The points represent the rectangle's corners if pos_in_middle
|
|
1091
|
+
is True, and the edges if pos_in_middle is False.
|
|
1092
|
+
|
|
1093
|
+
The points are returned as Vector2D others unless the return_list parameter is set to True. In that case,
|
|
1094
|
+
the points will be returned as lists.
|
|
1095
|
+
|
|
1096
|
+
The clockwise_return parameter determines the order of the points. If True, the points will be returned in
|
|
1097
|
+
clockwise order (A, B, D, C), otherwise, they will be returned in counterclockwise order (A, B, C, D).
|
|
1098
|
+
"""
|
|
1099
|
+
...
|
|
1100
|
+
|
|
1101
|
+
def get_lines(position:Vector2D, size:Vector2D, rotation:int|float=0, pos_in_middle:bool=True) -> list[list]:
|
|
1102
|
+
"""
|
|
1103
|
+
# Generate lines representing the sides of a rectangle based on the given parameters.
|
|
1104
|
+
|
|
1105
|
+
## Parameters:
|
|
1106
|
+
position (Vector2D): The center position of the rectangle.
|
|
1107
|
+
size (Vector2D): The size of the rectangle (width and height).
|
|
1108
|
+
rotation (int|float, optional): The rotation angle in degrees. Default is 0.
|
|
1109
|
+
pos_in_middle (bool, optional): If True, the points represent corners of the rectangle.
|
|
1110
|
+
If False, the points represent the rectangle's edges.
|
|
1111
|
+
Default is True.
|
|
1112
|
+
|
|
1113
|
+
## Returns:
|
|
1114
|
+
list[list[Vector2D]]: A list of lists, where each sublist contains two Vector2D others
|
|
1115
|
+
representing the start and end points of a line segment.
|
|
1116
|
+
|
|
1117
|
+
## Example:
|
|
1118
|
+
position = Vector2D(100, 100)
|
|
1119
|
+
|
|
1120
|
+
size = Vector2D(50, 30)
|
|
1121
|
+
|
|
1122
|
+
rotation = 45
|
|
1123
|
+
|
|
1124
|
+
lines = get_lines(position, size, rotation)
|
|
1125
|
+
|
|
1126
|
+
print(lines)
|
|
1127
|
+
|
|
1128
|
+
This will print the four line segments representing the sides of the rotated rectangle.
|
|
1129
|
+
|
|
1130
|
+
## Explanation:
|
|
1131
|
+
The function calculates the four points (A, B, C, D) of the rectangle using the `get_points` function
|
|
1132
|
+
based on the center position, size, rotation, and pos_in_middle parameters.
|
|
1133
|
+
|
|
1134
|
+
The function then returns a list of lists, where each sublist contains two Vector2D others representing
|
|
1135
|
+
the start and end points of a line segment forming the sides of the rectangle.
|
|
1136
|
+
"""
|
|
1137
|
+
...
|
|
1138
|
+
|
|
1139
|
+
def distance_line_point(line_point_a:Vector2D, line_point_b:Vector2D, point_c:Vector2D) -> float:
|
|
1140
|
+
"""
|
|
1141
|
+
# Calculate the distance between a line segment and a point.
|
|
1142
|
+
|
|
1143
|
+
## Parameters:
|
|
1144
|
+
line_point_a (Vector2D): The starting point of the line segment.
|
|
1145
|
+
line_point_b (Vector2D): The ending point of the line segment.
|
|
1146
|
+
point_c (Vector2D): The point to which the distance is calculated.
|
|
1147
|
+
|
|
1148
|
+
## Returns:
|
|
1149
|
+
float: The distance between the line segment and the point.
|
|
1150
|
+
|
|
1151
|
+
## Example:
|
|
1152
|
+
line_point_a = Vector2D(0, 0)
|
|
1153
|
+
|
|
1154
|
+
line_point_b = Vector2D(10, 0)
|
|
1155
|
+
|
|
1156
|
+
point_c = Vector2D(5, 5)
|
|
1157
|
+
|
|
1158
|
+
distance = distance_line_point(line_point_a, line_point_b, point_c)
|
|
1159
|
+
|
|
1160
|
+
print(distance)
|
|
1161
|
+
|
|
1162
|
+
This will print the distance between the line segment and the point.
|
|
1163
|
+
|
|
1164
|
+
## Explanation:
|
|
1165
|
+
The function calculates the distance between a line segment defined by two points (line_point_a and line_point_b)
|
|
1166
|
+
and a third point (point_c).
|
|
1167
|
+
|
|
1168
|
+
It does this by first computing the cross product of vectors (line_point_b - line_point_a) and (line_point_a - point_c).
|
|
1169
|
+
The magnitude of the resulting vector is divided by the magnitude of (line_point_b - line_point_a) to obtain the distance.
|
|
1170
|
+
|
|
1171
|
+
The result is returned as a float representing the distance between the line segment and the point.
|
|
1172
|
+
"""
|
|
1173
|
+
...
|
|
1174
|
+
|
|
1175
|
+
def optimize_value_string(value: int|float, precision: int) -> str:
|
|
1176
|
+
...
|