| 22 | ################################################################################ |
| 23 | |
| 24 | class 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 |
no outgoing calls
no test coverage detected