Skip to content

Commit 5a78eef

Browse files
committed
improve docs of vec2d.py by unifying doc styles
1 parent 4d5f4a5 commit 5a78eef

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

pymunk/vec2d.py

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ def __repr__(self) -> str:
7373

7474
# Addition
7575
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.
7777
78-
>>> Vec2d(3,4) + Vec2d(1,2)
78+
>>> Vec2d(3, 4) + Vec2d(1, 2)
7979
Vec2d(4, 6)
80-
>>> Vec2d(3,4) + (1,2)
80+
>>> Vec2d(3, 4) + (1, 2)
8181
Vec2d(4, 6)
8282
"""
8383
assert (
@@ -87,28 +87,28 @@ def __add__(self, other: Tuple[float, float]) -> "Vec2d": # type: ignore[overri
8787
return Vec2d(self.x + other[0], self.y + other[1])
8888

8989
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.
9191
92-
>>> (1,2) + Vec2d(3,4)
92+
>>> (1, 2) + Vec2d(3, 4)
9393
Vec2d(4, 6)
9494
"""
9595
return self.__add__(other)
9696

9797
# Subtraction
9898
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.
100100
101-
>>> Vec2d(3,4) - Vec2d(1,2)
101+
>>> Vec2d(3, 4) - Vec2d(1, 2)
102102
Vec2d(2, 2)
103-
>>> Vec2d(3,4) - (1,2)
103+
>>> Vec2d(3, 4) - (1, 2)
104104
Vec2d(2, 2)
105105
"""
106106
return Vec2d(self.x - other[0], self.y - other[1])
107107

108108
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.
110110
111-
>>> (1,2) - Vec2d(3,4)
111+
>>> (1, 2) - Vec2d(3, 4)
112112
Vec2d(-2, -2)
113113
"""
114114
assert (
@@ -118,74 +118,74 @@ def __rsub__(self, other: Tuple[float, float]) -> "Vec2d":
118118

119119
# Multiplication
120120
def __mul__(self, other: float) -> "Vec2d": # type: ignore[override]
121-
"""Multiply with a float
121+
"""Multiply a Vec2d with a float.
122122
123-
>>> Vec2d(3,6) * 2.5
123+
>>> Vec2d(3, 6) * 2.5
124124
Vec2d(7.5, 15.0)
125125
"""
126126
assert isinstance(other, numbers.Real)
127127
return Vec2d(self.x * other, self.y * other)
128128

129129
def __rmul__(self, other: float) -> "Vec2d": # type: ignore[override]
130-
"""Multiply a float with a Vec2d
130+
"""Multiply a float with a Vec2d.
131131
132-
>>> 2.5 * Vec2d(3,6)
132+
>>> 2.5 * Vec2d(3, 6)
133133
Vec2d(7.5, 15.0)
134134
"""
135135
return self.__mul__(other)
136136

137137
# Division
138138
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).
140140
141-
>>> Vec2d(3,6) // 2.0
141+
>>> Vec2d(3, 6) // 2.0
142142
Vec2d(1.0, 3.0)
143143
"""
144144
assert isinstance(other, numbers.Real)
145145
return Vec2d(self.x // other, self.y // other)
146146

147147
def __truediv__(self, other: float) -> "Vec2d":
148-
"""Division by a float
148+
"""Division by a float.
149149
150-
>>> Vec2d(3,6) / 2.0
150+
>>> Vec2d(3, 6) / 2.0
151151
Vec2d(1.5, 3.0)
152152
"""
153153
assert isinstance(other, numbers.Real)
154154
return Vec2d(self.x / other, self.y / other)
155155

156156
# Unary operations
157157
def __neg__(self) -> "Vec2d":
158-
"""Return the negated version of the Vec2d
158+
"""Return the negated version of the Vec2d.
159159
160-
>>> -Vec2d(1,-2)
160+
>>> -Vec2d(1, -2)
161161
Vec2d(-1, 2)
162162
"""
163163
return Vec2d(operator.neg(self.x), operator.neg(self.y))
164164

165165
def __pos__(self) -> "Vec2d":
166166
"""Return the unary pos of the Vec2d.
167167
168-
>>> +Vec2d(1,-2)
168+
>>> +Vec2d(1, -2)
169169
Vec2d(1, -2)
170170
"""
171171
return Vec2d(operator.pos(self.x), operator.pos(self.y))
172172

173173
def __abs__(self) -> float:
174-
"""Return the length of the Vec2d
174+
"""Return the length of the Vec2d.
175175
176-
>>> abs(Vec2d(3,4))
176+
>>> abs(Vec2d(3, 4))
177177
5.0
178178
"""
179179
return self.length
180180

181181
# vectory functions
182182
def get_length_sqrd(self) -> float:
183183
"""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
185185
instead of first calling get_length() or access .length and then do a
186186
x**2.
187187
188-
>>> v = Vec2d(3,4)
188+
>>> v = Vec2d(3, 4)
189189
>>> v.get_length_sqrd() == v.length**2
190190
True
191191
@@ -231,13 +231,13 @@ def rotated_degrees(self, angle_degrees: float) -> "Vec2d":
231231
"""Create and return a new vector by rotating this vector by
232232
angle_degrees degrees.
233233
234-
:return: Rotade vector
234+
:return: Rotated vector
235235
"""
236236
return self.rotated(math.radians(angle_degrees))
237237

238238
@property
239239
def angle(self) -> float:
240-
"""The angle (in radians) of the vector
240+
"""The angle (in radians) of the vector.
241241
242242
>>> '%.2f' % Vec2d(-1, 0).angle
243243
'3.14'
@@ -250,7 +250,7 @@ def angle(self) -> float:
250250

251251
@property
252252
def angle_degrees(self) -> float:
253-
"""Gets the angle (in degrees) of a vector
253+
"""Get the angle (in degrees) of a vector.
254254
255255
>>> Vec2d(0, 1).angle_degrees
256256
90.0
@@ -260,7 +260,7 @@ def angle_degrees(self) -> float:
260260
return math.degrees(self.angle)
261261

262262
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.
264264
265265
>>> '%.2f' % Vec2d(3, 0).get_angle_between(Vec2d(-1, 0))
266266
'3.14'
@@ -275,7 +275,7 @@ def get_angle_between(self, other: Tuple[float, float]) -> float:
275275
return math.atan2(cross, dot)
276276

277277
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.
279279
280280
>>> Vec2d(3, 0).get_angle_degrees_between(Vec2d(-1, 0))
281281
180.0
@@ -287,7 +287,7 @@ def get_angle_degrees_between(self, other: "Vec2d") -> float:
287287
return math.degrees(self.get_angle_between(other))
288288

289289
def normalized(self) -> "Vec2d":
290-
"""Get a normalized copy of the vector
290+
"""Get a normalized copy of the vector.
291291
Note: This function will return 0 if the length of the vector is 0.
292292
293293
>>> Vec2d(3, 0).normalized()
@@ -303,7 +303,7 @@ def normalized(self) -> "Vec2d":
303303
return Vec2d(0, 0)
304304

305305
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.
307307
308308
>>> Vec2d(3, 0).normalized_and_length()
309309
(Vec2d(1.0, 0.0), 3.0)
@@ -345,7 +345,7 @@ def perpendicular_normal(self) -> "Vec2d":
345345
return Vec2d(self.x, self.y)
346346

347347
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.
349349
v1.dot(v2) -> v1.x*v2.x + v1.y*v2.y
350350
351351
>>> Vec2d(5, 0).dot((0, 5))
@@ -357,7 +357,7 @@ def dot(self, other: Tuple[float, float]) -> float:
357357
return float(self.x * other[0] + self.y * other[1])
358358

359359
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.
361361
362362
>>> Vec2d(0, 2).get_distance((0, -3))
363363
5.0
@@ -369,11 +369,11 @@ def get_distance(self, other: Tuple[float, float]) -> float:
369369
return math.sqrt((self.x - other[0]) ** 2 + (self.y - other[1]) ** 2)
370370

371371
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.
373373
It is more efficent to use this method than to call get_distance()
374374
first and then do a square() on the result.
375375
376-
>>> Vec2d(1, 0).get_dist_sqrd((1,10))
376+
>>> Vec2d(1, 0).get_dist_sqrd((1, 10))
377377
100
378378
>>> Vec2d(1, 2).get_dist_sqrd((10, 11))
379379
162
@@ -384,7 +384,7 @@ def get_dist_sqrd(self, other: Tuple[float, float]) -> float:
384384
return (self.x - other[0]) ** 2 + (self.y - other[1]) ** 2
385385

386386
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.
388388
389389
>>> Vec2d(10, 1).projection((5.0, 0))
390390
Vec2d(10.0, 0.0)
@@ -397,12 +397,13 @@ def projection(self, other: Tuple[float, float]) -> "Vec2d":
397397
other_length_sqrd = other[0] * other[0] + other[1] * other[1]
398398
if other_length_sqrd == 0.0:
399399
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
402403
return Vec2d(other[0] * new_length, other[1] * new_length)
403404

404405
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.
406407
407408
v1.cross(v2) -> v1.x*v2.y - v1.y*v2.x
408409
@@ -426,7 +427,7 @@ def interpolate_to(self, other: Tuple[float, float], range: float) -> "Vec2d":
426427
def convert_to_basis(
427428
self, x_vector: Tuple[float, float], y_vector: Tuple[float, float]
428429
) -> "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.
430431
431432
>>> Vec2d(10, 1).convert_to_basis((5, 0), (0, 0.5))
432433
Vec2d(2.0, 2.0)
@@ -440,7 +441,7 @@ def convert_to_basis(
440441
@property
441442
def int_tuple(self) -> Tuple[int, int]:
442443
"""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.
444445
445446
>>> Vec2d(0.9, 2.4).int_tuple
446447
(1, 2)
@@ -458,7 +459,7 @@ def zero() -> "Vec2d":
458459

459460
@staticmethod
460461
def unit() -> "Vec2d":
461-
"""A unit vector pointing up
462+
"""A unit vector pointing up.
462463
463464
>>> Vec2d.unit()
464465
Vec2d(0, 1)
@@ -467,7 +468,7 @@ def unit() -> "Vec2d":
467468

468469
@staticmethod
469470
def ones() -> "Vec2d":
470-
"""A vector where both x and y is 1
471+
"""A vector where both x and y is 1.
471472
472473
>>> Vec2d.ones()
473474
Vec2d(1, 1)
@@ -490,14 +491,14 @@ def from_polar(length: float, angle: float) -> "Vec2d":
490491

491492
# Extra functions, mainly for chipmunk
492493
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."""
494495
assert len(other) == 2
495496
return Vec2d(
496497
self.x * other[0] - self.y * other[1], self.x * other[1] + self.y * other[0]
497498
)
498499

499500
def cpvunrotate(self, other: Tuple[float, float]) -> "Vec2d":
500-
"""The inverse of cpvrotate"""
501+
"""The inverse of cpvrotate."""
501502
assert len(other) == 2
502503
return Vec2d(
503504
self.x * other[0] + self.y * other[1], self.y * other[0] - self.x * other[1]

0 commit comments

Comments
 (0)