| 487 | ################################################################################ |
| 488 | |
| 489 | class Vector2: |
| 490 | |
| 491 | # See all the nice vector operations above? |
| 492 | # The following class implements those instructions. |
| 493 | |
| 494 | __slots__ = 'x', 'y' |
| 495 | |
| 496 | def __init__(self, x, y): |
| 497 | self.x = x |
| 498 | self.y = y |
| 499 | |
| 500 | def __repr__(self): |
| 501 | return 'Vector2({!r}, {!r})'.format(self.x, self.y) |
| 502 | |
| 503 | def polar_repr(self): |
| 504 | x, y = self.x, self.y |
| 505 | magnitude = hypot(x, y) |
| 506 | angle = degrees(atan2(x, y)) % 360 |
| 507 | return 'Polar2({!r}, {!r})'.format(magnitude, angle) |
| 508 | |
| 509 | # Rich Comparison Methods |
| 510 | |
| 511 | def __lt__(self, obj): |
| 512 | if isinstance(obj, Vector2): |
| 513 | x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y |
| 514 | return x1 * x1 + y1 * y1 < x2 * x2 + y2 * y2 |
| 515 | return hypot(self.x, self.y) < obj |
| 516 | |
| 517 | def __le__(self, obj): |
| 518 | if isinstance(obj, Vector2): |
| 519 | x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y |
| 520 | return x1 * x1 + y1 * y1 <= x2 * x2 + y2 * y2 |
| 521 | return hypot(self.x, self.y) <= obj |
| 522 | |
| 523 | def __eq__(self, obj): |
| 524 | if isinstance(obj, Vector2): |
| 525 | return self.x == obj.x and self.y == obj.y |
| 526 | return hypot(self.x, self.y) == obj |
| 527 | |
| 528 | def __ne__(self, obj): |
| 529 | if isinstance(obj, Vector2): |
| 530 | return self.x != obj.x or self.y != obj.y |
| 531 | return hypot(self.x, self.y) != obj |
| 532 | |
| 533 | def __gt__(self, obj): |
| 534 | if isinstance(obj, Vector2): |
| 535 | x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y |
| 536 | return x1 * x1 + y1 * y1 > x2 * x2 + y2 * y2 |
| 537 | return hypot(self.x, self.y) > obj |
| 538 | |
| 539 | def __ge__(self, obj): |
| 540 | if isinstance(obj, Vector2): |
| 541 | x1, y1, x2, y2 = self.x, self.y, obj.x, obj.y |
| 542 | return x1 * x1 + y1 * y1 >= x2 * x2 + y2 * y2 |
| 543 | return hypot(self.x, self.y) >= obj |
| 544 | |
| 545 | # Boolean Operation |
| 546 |
no outgoing calls
no test coverage detected