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

Class Vector2

recipes/Python/576959_Boids_20_for_Python_31/recipe-576959.py:489–950  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

487################################################################################
488
489class 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

Callers 15

build_boidsMethod · 0.70
check_targetMethod · 0.70
__init__Method · 0.70
centerMethod · 0.70
vectorMethod · 0.70
__init__Method · 0.70
__rule_1Method · 0.70
__rule_2Method · 0.70
__rule_3Method · 0.70
Polar2Function · 0.70
__add__Method · 0.70
__sub__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected