Skip to content

Commit e4a315f

Browse files
committed
improve docs of vec2d
1 parent dec24e1 commit e4a315f

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

pymunk/tests/test_vec2d.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ def testLength(self) -> None:
6262
v = Vec2d(3, 4)
6363
self.assertTrue(v.length == 5)
6464
self.assertTrue(v.get_length_sqrd() == 25)
65-
normalized, length = v.normalized_and_length()
66-
self.assertEqual(normalized, Vec2d(0.6, 0.8))
67-
self.assertEqual(length, 5)
68-
normalized, length = Vec2d(0, 0).normalized_and_length()
69-
self.assertEqual(normalized, Vec2d(0, 0))
70-
self.assertEqual(length, 0)
7165
with self.assertRaises(AttributeError):
7266
v.length = 5 # type: ignore
7367

pymunk/vec2d.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,20 +237,37 @@ def rotated_degrees(self, angle_degrees: float) -> "Vec2d":
237237

238238
@property
239239
def angle(self) -> float:
240-
"""The angle (in radians) of the vector"""
240+
"""The angle (in radians) of the vector
241+
242+
>>> '%.2f' % Vec2d(-1, 0).angle
243+
'3.14'
244+
>>> Vec2d(0, 0).angle
245+
0
246+
"""
241247
if self.get_length_sqrd() == 0:
242248
return 0
243249
return math.atan2(self.y, self.x)
244250

245251
@property
246252
def angle_degrees(self) -> float:
247-
"""Gets the angle (in degrees) of a vector"""
253+
"""Gets the angle (in degrees) of a vector
254+
255+
>>> Vec2d(0, 1).angle_degrees
256+
90.0
257+
>>> Vec2d(0, 0).angle_degrees
258+
0.0
259+
"""
248260
return math.degrees(self.angle)
249261

250262
def get_angle_between(self, other: Tuple[float, float]) -> float:
251263
"""Get the angle between the vector and the other in radians
252264
253-
:return: The angle
265+
>>> '%.2f' % Vec2d(3, 0).get_angle_between(Vec2d(-1, 0))
266+
'3.14'
267+
>>> Vec2d(3, 0).get_angle_between(Vec2d(0, 0))
268+
0.0
269+
>>> Vec2d(0, 0).get_angle_between(Vec2d(0, 0))
270+
0.0
254271
"""
255272
assert len(other) == 2
256273
cross = self.x * other[1] - self.y * other[0]
@@ -260,15 +277,25 @@ def get_angle_between(self, other: Tuple[float, float]) -> float:
260277
def get_angle_degrees_between(self, other: "Vec2d") -> float:
261278
"""Get the angle between the vector and the other in degrees
262279
263-
:return: The angle (in degrees)
280+
>>> Vec2d(3, 0).get_angle_degrees_between(Vec2d(-1, 0))
281+
180.0
282+
>>> Vec2d(3, 0).get_angle_degrees_between(Vec2d(0, 0))
283+
0.0
284+
>>> Vec2d(0, 0).get_angle_degrees_between(Vec2d(0, 0))
285+
0.0
264286
"""
265287
return math.degrees(self.get_angle_between(other))
266288

267289
def normalized(self) -> "Vec2d":
268290
"""Get a normalized copy of the vector
269291
Note: This function will return 0 if the length of the vector is 0.
270292
271-
:return: A normalized vector
293+
>>> Vec2d(3, 0).normalized()
294+
Vec2d(1.0, 0.0)
295+
>>> Vec2d(3, 4).normalized()
296+
Vec2d(0.6, 0.8)
297+
>>> Vec2d(0, 0).normalized()
298+
Vec2d(0, 0)
272299
"""
273300
length = self.length
274301
if length != 0:
@@ -278,7 +305,12 @@ def normalized(self) -> "Vec2d":
278305
def normalized_and_length(self) -> Tuple["Vec2d", float]:
279306
"""Normalize the vector and return its length before the normalization
280307
281-
:return: The length before the normalization
308+
>>> Vec2d(3, 0).normalized_and_length()
309+
(Vec2d(1.0, 0.0), 3.0)
310+
>>> Vec2d(3, 4).normalized_and_length()
311+
(Vec2d(0.6, 0.8), 5.0)
312+
>>> Vec2d(0, 0).normalized_and_length()
313+
(Vec2d(0, 0), 0)
282314
"""
283315
length = self.length
284316
if length != 0:
@@ -288,14 +320,24 @@ def normalized_and_length(self) -> Tuple["Vec2d", float]:
288320
def perpendicular(self) -> "Vec2d":
289321
"""Get a vertical vector rotated 90 degrees counterclockwise from the original vector.
290322
291-
:return: A new vector perpendicular to this vector.
323+
>>> Vec2d(1, 2).perpendicular()
324+
Vec2d(-2, 1)
292325
"""
293326
return Vec2d(-self.y, self.x)
294327

295328
def perpendicular_normal(self) -> "Vec2d":
296329
"""Get a vertical normalized vector rotated 90 degrees counterclockwise from the original vector.
297330
298-
:return: A new normalized vector perpendicular to this vector.
331+
>>> Vec2d(1, 0).perpendicular_normal()
332+
Vec2d(0.0, 1.0)
333+
>>> Vec2d(2, 0).perpendicular_normal()
334+
Vec2d(0.0, 1.0)
335+
>>> Vec2d(1, 1).perpendicular_normal().angle_degrees
336+
135.0
337+
>>> Vec2d(1, 1).angle_degrees + 90
338+
135.0
339+
>>> Vec2d(0, 0).perpendicular_normal()
340+
Vec2d(0, 0)
299341
"""
300342
length = self.length
301343
if length != 0:
@@ -361,6 +403,7 @@ def projection(self, other: Tuple[float, float]) -> "Vec2d":
361403

362404
def cross(self, other: Tuple[float, float]) -> float:
363405
"""The cross product between the vector and other vector
406+
364407
v1.cross(v2) -> v1.x*v2.y - v1.y*v2.x
365408
366409
>>> Vec2d(1, 0.5).cross((4, 6))

0 commit comments

Comments
 (0)