@@ -73,11 +73,11 @@ def __repr__(self) -> str:
73
73
74
74
# Addition
75
75
def __add__ (self , other : Tuple [float , float ]) -> "Vec2d" : # type: ignore[override]
76
- """Add a Vec2d with another Vec2d or Tuple of size 2
76
+ """Add a Vec2d with another Vec2d or Tuple of size 2.
77
77
78
- >>> Vec2d(3,4) + Vec2d(1,2)
78
+ >>> Vec2d(3, 4) + Vec2d(1, 2)
79
79
Vec2d(4, 6)
80
- >>> Vec2d(3,4) + (1,2)
80
+ >>> Vec2d(3, 4) + (1, 2)
81
81
Vec2d(4, 6)
82
82
"""
83
83
assert (
@@ -87,28 +87,28 @@ def __add__(self, other: Tuple[float, float]) -> "Vec2d": # type: ignore[overri
87
87
return Vec2d (self .x + other [0 ], self .y + other [1 ])
88
88
89
89
def __radd__ (self , other : Tuple [float , float ]) -> "Vec2d" :
90
- """Add a Tuple of size 2 with a Vec2d
90
+ """Add a Tuple of size 2 with a Vec2d.
91
91
92
- >>> (1,2) + Vec2d(3,4)
92
+ >>> (1, 2) + Vec2d(3, 4)
93
93
Vec2d(4, 6)
94
94
"""
95
95
return self .__add__ (other )
96
96
97
97
# Subtraction
98
98
def __sub__ (self , other : Tuple [float , float ]) -> "Vec2d" :
99
- """Subtract a Vec2d with another Vec2d or Tuple of size 2
99
+ """Subtract a Vec2d with another Vec2d or Tuple of size 2.
100
100
101
- >>> Vec2d(3,4) - Vec2d(1,2)
101
+ >>> Vec2d(3, 4) - Vec2d(1, 2)
102
102
Vec2d(2, 2)
103
- >>> Vec2d(3,4) - (1,2)
103
+ >>> Vec2d(3, 4) - (1, 2)
104
104
Vec2d(2, 2)
105
105
"""
106
106
return Vec2d (self .x - other [0 ], self .y - other [1 ])
107
107
108
108
def __rsub__ (self , other : Tuple [float , float ]) -> "Vec2d" :
109
- """Subtract a Tuple of size 2 with a Vec2d
109
+ """Subtract a Tuple of size 2 with a Vec2d.
110
110
111
- >>> (1,2) - Vec2d(3,4)
111
+ >>> (1, 2) - Vec2d(3, 4)
112
112
Vec2d(-2, -2)
113
113
"""
114
114
assert (
@@ -118,74 +118,74 @@ def __rsub__(self, other: Tuple[float, float]) -> "Vec2d":
118
118
119
119
# Multiplication
120
120
def __mul__ (self , other : float ) -> "Vec2d" : # type: ignore[override]
121
- """Multiply with a float
121
+ """Multiply a Vec2d with a float.
122
122
123
- >>> Vec2d(3,6) * 2.5
123
+ >>> Vec2d(3, 6) * 2.5
124
124
Vec2d(7.5, 15.0)
125
125
"""
126
126
assert isinstance (other , numbers .Real )
127
127
return Vec2d (self .x * other , self .y * other )
128
128
129
129
def __rmul__ (self , other : float ) -> "Vec2d" : # type: ignore[override]
130
- """Multiply a float with a Vec2d
130
+ """Multiply a float with a Vec2d.
131
131
132
- >>> 2.5 * Vec2d(3,6)
132
+ >>> 2.5 * Vec2d(3, 6)
133
133
Vec2d(7.5, 15.0)
134
134
"""
135
135
return self .__mul__ (other )
136
136
137
137
# Division
138
138
def __floordiv__ (self , other : float ) -> "Vec2d" :
139
- """Floor division by a float (also known as integer division)
139
+ """Floor division by a float (also known as integer division).
140
140
141
- >>> Vec2d(3,6) // 2.0
141
+ >>> Vec2d(3, 6) // 2.0
142
142
Vec2d(1.0, 3.0)
143
143
"""
144
144
assert isinstance (other , numbers .Real )
145
145
return Vec2d (self .x // other , self .y // other )
146
146
147
147
def __truediv__ (self , other : float ) -> "Vec2d" :
148
- """Division by a float
148
+ """Division by a float.
149
149
150
- >>> Vec2d(3,6) / 2.0
150
+ >>> Vec2d(3, 6) / 2.0
151
151
Vec2d(1.5, 3.0)
152
152
"""
153
153
assert isinstance (other , numbers .Real )
154
154
return Vec2d (self .x / other , self .y / other )
155
155
156
156
# Unary operations
157
157
def __neg__ (self ) -> "Vec2d" :
158
- """Return the negated version of the Vec2d
158
+ """Return the negated version of the Vec2d.
159
159
160
- >>> -Vec2d(1,-2)
160
+ >>> -Vec2d(1, -2)
161
161
Vec2d(-1, 2)
162
162
"""
163
163
return Vec2d (operator .neg (self .x ), operator .neg (self .y ))
164
164
165
165
def __pos__ (self ) -> "Vec2d" :
166
166
"""Return the unary pos of the Vec2d.
167
167
168
- >>> +Vec2d(1,-2)
168
+ >>> +Vec2d(1, -2)
169
169
Vec2d(1, -2)
170
170
"""
171
171
return Vec2d (operator .pos (self .x ), operator .pos (self .y ))
172
172
173
173
def __abs__ (self ) -> float :
174
- """Return the length of the Vec2d
174
+ """Return the length of the Vec2d.
175
175
176
- >>> abs(Vec2d(3,4))
176
+ >>> abs(Vec2d(3, 4))
177
177
5.0
178
178
"""
179
179
return self .length
180
180
181
181
# vectory functions
182
182
def get_length_sqrd (self ) -> float :
183
183
"""Get the squared length of the vector.
184
- If the squared length is enough it is more efficient to use this method
184
+ If the squared length is enough, it is more efficient to use this method
185
185
instead of first calling get_length() or access .length and then do a
186
186
x**2.
187
187
188
- >>> v = Vec2d(3,4)
188
+ >>> v = Vec2d(3, 4)
189
189
>>> v.get_length_sqrd() == v.length**2
190
190
True
191
191
@@ -231,13 +231,13 @@ def rotated_degrees(self, angle_degrees: float) -> "Vec2d":
231
231
"""Create and return a new vector by rotating this vector by
232
232
angle_degrees degrees.
233
233
234
- :return: Rotade vector
234
+ :return: Rotated vector
235
235
"""
236
236
return self .rotated (math .radians (angle_degrees ))
237
237
238
238
@property
239
239
def angle (self ) -> float :
240
- """The angle (in radians) of the vector
240
+ """The angle (in radians) of the vector.
241
241
242
242
>>> '%.2f' % Vec2d(-1, 0).angle
243
243
'3.14'
@@ -250,7 +250,7 @@ def angle(self) -> float:
250
250
251
251
@property
252
252
def angle_degrees (self ) -> float :
253
- """Gets the angle (in degrees) of a vector
253
+ """Get the angle (in degrees) of a vector.
254
254
255
255
>>> Vec2d(0, 1).angle_degrees
256
256
90.0
@@ -260,7 +260,7 @@ def angle_degrees(self) -> float:
260
260
return math .degrees (self .angle )
261
261
262
262
def get_angle_between (self , other : Tuple [float , float ]) -> float :
263
- """Get the angle between the vector and the other in radians
263
+ """Get the angle between the vector and the other in radians.
264
264
265
265
>>> '%.2f' % Vec2d(3, 0).get_angle_between(Vec2d(-1, 0))
266
266
'3.14'
@@ -275,7 +275,7 @@ def get_angle_between(self, other: Tuple[float, float]) -> float:
275
275
return math .atan2 (cross , dot )
276
276
277
277
def get_angle_degrees_between (self , other : "Vec2d" ) -> float :
278
- """Get the angle between the vector and the other in degrees
278
+ """Get the angle between the vector and the other in degrees.
279
279
280
280
>>> Vec2d(3, 0).get_angle_degrees_between(Vec2d(-1, 0))
281
281
180.0
@@ -287,7 +287,7 @@ def get_angle_degrees_between(self, other: "Vec2d") -> float:
287
287
return math .degrees (self .get_angle_between (other ))
288
288
289
289
def normalized (self ) -> "Vec2d" :
290
- """Get a normalized copy of the vector
290
+ """Get a normalized copy of the vector.
291
291
Note: This function will return 0 if the length of the vector is 0.
292
292
293
293
>>> Vec2d(3, 0).normalized()
@@ -303,7 +303,7 @@ def normalized(self) -> "Vec2d":
303
303
return Vec2d (0 , 0 )
304
304
305
305
def normalized_and_length (self ) -> Tuple ["Vec2d" , float ]:
306
- """Normalize the vector and return its length before the normalization
306
+ """Normalize the vector and return its length before the normalization.
307
307
308
308
>>> Vec2d(3, 0).normalized_and_length()
309
309
(Vec2d(1.0, 0.0), 3.0)
@@ -345,7 +345,7 @@ def perpendicular_normal(self) -> "Vec2d":
345
345
return Vec2d (self .x , self .y )
346
346
347
347
def dot (self , other : Tuple [float , float ]) -> float :
348
- """The dot product between the vector and other vector
348
+ """The dot product between the vector and other vector.
349
349
v1.dot(v2) -> v1.x*v2.x + v1.y*v2.y
350
350
351
351
>>> Vec2d(5, 0).dot((0, 5))
@@ -357,7 +357,7 @@ def dot(self, other: Tuple[float, float]) -> float:
357
357
return float (self .x * other [0 ] + self .y * other [1 ])
358
358
359
359
def get_distance (self , other : Tuple [float , float ]) -> float :
360
- """The distance between the vector and other vector
360
+ """The distance between the vector and other vector.
361
361
362
362
>>> Vec2d(0, 2).get_distance((0, -3))
363
363
5.0
@@ -369,11 +369,11 @@ def get_distance(self, other: Tuple[float, float]) -> float:
369
369
return math .sqrt ((self .x - other [0 ]) ** 2 + (self .y - other [1 ]) ** 2 )
370
370
371
371
def get_dist_sqrd (self , other : Tuple [float , float ]) -> float :
372
- """The squared distance between the vector and other vector
372
+ """The squared distance between the vector and other vector.
373
373
It is more efficent to use this method than to call get_distance()
374
374
first and then do a square() on the result.
375
375
376
- >>> Vec2d(1, 0).get_dist_sqrd((1,10))
376
+ >>> Vec2d(1, 0).get_dist_sqrd((1, 10))
377
377
100
378
378
>>> Vec2d(1, 2).get_dist_sqrd((10, 11))
379
379
162
@@ -384,7 +384,7 @@ def get_dist_sqrd(self, other: Tuple[float, float]) -> float:
384
384
return (self .x - other [0 ]) ** 2 + (self .y - other [1 ]) ** 2
385
385
386
386
def projection (self , other : Tuple [float , float ]) -> "Vec2d" :
387
- """Project this vector on top of other vector
387
+ """Project this vector on top of other vector.
388
388
389
389
>>> Vec2d(10, 1).projection((5.0, 0))
390
390
Vec2d(10.0, 0.0)
@@ -397,12 +397,13 @@ def projection(self, other: Tuple[float, float]) -> "Vec2d":
397
397
other_length_sqrd = other [0 ] * other [0 ] + other [1 ] * other [1 ]
398
398
if other_length_sqrd == 0.0 :
399
399
return Vec2d (0 , 0 )
400
- projected_length_times_other_length = self .dot (other )
401
- new_length = projected_length_times_other_length / other_length_sqrd
400
+ # projected_length_times_other_length = self.dot(other)
401
+ # new_length = projected_length_times_other_length / other_length_sqrd
402
+ new_length = self .dot (other ) / other_length_sqrd
402
403
return Vec2d (other [0 ] * new_length , other [1 ] * new_length )
403
404
404
405
def cross (self , other : Tuple [float , float ]) -> float :
405
- """The cross product between the vector and other vector
406
+ """The cross product between the vector and the other.
406
407
407
408
v1.cross(v2) -> v1.x*v2.y - v1.y*v2.x
408
409
@@ -426,7 +427,7 @@ def interpolate_to(self, other: Tuple[float, float], range: float) -> "Vec2d":
426
427
def convert_to_basis (
427
428
self , x_vector : Tuple [float , float ], y_vector : Tuple [float , float ]
428
429
) -> "Vec2d" :
429
- """Converts the vector to a new basis defined by the given x and y vectors.
430
+ """Convert the vector to a new basis defined by the given x and y vectors.
430
431
431
432
>>> Vec2d(10, 1).convert_to_basis((5, 0), (0, 0.5))
432
433
Vec2d(2.0, 2.0)
@@ -440,7 +441,7 @@ def convert_to_basis(
440
441
@property
441
442
def int_tuple (self ) -> Tuple [int , int ]:
442
443
"""The x and y values of this vector as a tuple of ints.
443
- Uses round() to round to closest int.
444
+ Use ` round()` to round to closest int.
444
445
445
446
>>> Vec2d(0.9, 2.4).int_tuple
446
447
(1, 2)
@@ -458,7 +459,7 @@ def zero() -> "Vec2d":
458
459
459
460
@staticmethod
460
461
def unit () -> "Vec2d" :
461
- """A unit vector pointing up
462
+ """A unit vector pointing up.
462
463
463
464
>>> Vec2d.unit()
464
465
Vec2d(0, 1)
@@ -467,7 +468,7 @@ def unit() -> "Vec2d":
467
468
468
469
@staticmethod
469
470
def ones () -> "Vec2d" :
470
- """A vector where both x and y is 1
471
+ """A vector where both x and y is 1.
471
472
472
473
>>> Vec2d.ones()
473
474
Vec2d(1, 1)
@@ -490,14 +491,14 @@ def from_polar(length: float, angle: float) -> "Vec2d":
490
491
491
492
# Extra functions, mainly for chipmunk
492
493
def cpvrotate (self , other : Tuple [float , float ]) -> "Vec2d" :
493
- """Uses complex multiplication to rotate this vector by the other."""
494
+ """Use complex multiplication to rotate this vector by the other."""
494
495
assert len (other ) == 2
495
496
return Vec2d (
496
497
self .x * other [0 ] - self .y * other [1 ], self .x * other [1 ] + self .y * other [0 ]
497
498
)
498
499
499
500
def cpvunrotate (self , other : Tuple [float , float ]) -> "Vec2d" :
500
- """The inverse of cpvrotate"""
501
+ """The inverse of cpvrotate. """
501
502
assert len (other ) == 2
502
503
return Vec2d (
503
504
self .x * other [0 ] + self .y * other [1 ], self .y * other [0 ] - self .x * other [1 ]
0 commit comments