You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.7 KiB
81 lines
1.7 KiB
2 years ago
|
class Vec2:
|
||
|
class NotComparable(Exception):
|
||
|
...
|
||
|
|
||
|
def __init__(self, x, y):
|
||
|
self.x = x
|
||
|
self.y = y
|
||
|
|
||
|
def __iter__(self):
|
||
|
yield self.x
|
||
|
yield self.y
|
||
|
|
||
|
def __repr__(self):
|
||
|
return str(self)
|
||
|
|
||
|
def __str__(self):
|
||
|
return f'Vec2({self.x}, {self.y})'
|
||
|
|
||
|
def __add__(self, other):
|
||
|
x, y = other
|
||
|
return Vec2(self.x + x, self.y + y)
|
||
|
|
||
|
def __sub__(self, other):
|
||
|
x, y = other
|
||
|
return Vec2(self.x - x, self.y - y)
|
||
|
|
||
|
def __mul__(self, other):
|
||
|
if isinstance(other, int | float):
|
||
|
return Vec2(self.x * other, self.y * other)
|
||
|
else:
|
||
|
return NotImplemented
|
||
|
|
||
|
def __eq__(self, other):
|
||
|
x, y = other
|
||
|
return self.x == x and self.y == y
|
||
|
|
||
|
def __ne__(self, other):
|
||
|
x, y = other
|
||
|
return self.x != x or self.y != y
|
||
|
|
||
|
def __gt__(self, other):
|
||
|
x, y = other
|
||
|
return self.x > x and self.y > y
|
||
|
|
||
|
def __ge__(self, other):
|
||
|
x, y = other
|
||
|
return self.x >= x and self.y >= y
|
||
|
|
||
|
def __lt__(self, other):
|
||
|
x, y = other
|
||
|
return self.x < x and self.y < y
|
||
|
|
||
|
def __le__(self, other):
|
||
|
x, y = other
|
||
|
return self.x <= x and self.y <= y
|
||
|
|
||
|
def __abs__(self):
|
||
|
return Vec2(abs(self.x), abs(self.y))
|
||
|
|
||
|
def __iadd__(self, other):
|
||
|
x, y = other
|
||
|
self.x += x
|
||
|
self.y += y
|
||
|
return self
|
||
|
|
||
|
def __isub__(self, other):
|
||
|
x, y = other
|
||
|
self.x -= x
|
||
|
self.y -= y
|
||
|
return self
|
||
|
|
||
|
def __imul__(self, other):
|
||
|
self.x *= other
|
||
|
self.y *= other
|
||
|
|
||
|
def __complex__(self):
|
||
|
return complex(self.x, self.y)
|
||
|
|
||
|
def __len__(self):
|
||
|
return (self.x ** 2 + self.y ** 2) ** 0.5
|