MCPcopy Create free account
hub / github.com/ActiveState/code / Vector2

Class Vector2

recipes/Python/578006_vector/recipe-578006.py:24–506  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22################################################################################
23
24class Vector2:
25
26 __slots__ = 'x', 'y'
27
28 def __init__(self, x, y):
29 self.x = x
30 self.y = y
31
32 def __repr__(self):
33 return 'Vector2({!r}, {!r})'.format(self.x, self.y)
34
35 def polar_repr(self):
36 x, y = self.x, self.y
37 magnitude = hypot(x, y)
38 angle = degrees(atan2(x, y)) % 360
39 return 'Polar2({!r}, {!r})'.format(magnitude, angle)
40
41 # Rich Comparison Methods
42
43 def __lt__(self, obj):
44 if isinstance(obj, Vector2):
45 x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y
46 return x1 * x1 + y1 * y1 < x2 * x2 + y2 * y2
47 return hypot(self.x, self.y) < obj
48
49 def __le__(self, obj):
50 if isinstance(obj, Vector2):
51 x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y
52 return x1 * x1 + y1 * y1 <= x2 * x2 + y2 * y2
53 return hypot(self.x, self.y) <= obj
54
55 def __eq__(self, obj):
56 if isinstance(obj, Vector2):
57 return self.x == obj.x and self.y == obj.y
58 return hypot(self.x, self.y) == obj
59
60 def __ne__(self, obj):
61 if isinstance(obj, Vector2):
62 return self.x != obj.x or self.y != obj.y
63 return hypot(self.x, self.y) != obj
64
65 def __gt__(self, obj):
66 if isinstance(obj, Vector2):
67 x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y
68 return x1 * x1 + y1 * y1 > x2 * x2 + y2 * y2
69 return hypot(self.x, self.y) > obj
70
71 def __ge__(self, obj):
72 if isinstance(obj, Vector2):
73 x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y
74 return x1 * x1 + y1 * y1 >= x2 * x2 + y2 * y2
75 return hypot(self.x, self.y) >= obj
76
77 # Boolean Operation
78
79 def __bool__(self):
80 return self.x != 0 or self.y != 0
81

Callers 15

Polar2Function · 0.70
__add__Method · 0.70
__sub__Method · 0.70
__mul__Method · 0.70
__truediv__Method · 0.70
__floordiv__Method · 0.70
__mod__Method · 0.70
__divmod__Method · 0.70
__pow__Method · 0.70
__lshift__Method · 0.70
__rshift__Method · 0.70
__and__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected