Ich hatte mal vor ca. 1,5 Jahren mir so eine Vektorklasse geschrieben.
Sind sicherlich einige Bugs drin und möglicherweise kann man das ein oder andere besser machen.
Verbesserungsvorschläge sind mir willkommen. Ich lerne jeden Tag.
Code: Alles auswählen
import random
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f'({self.x},{self.y})'
def __repr__(self):
return f'Vector({self.x}, {self.y})'
def __add__(self, other): # +
if isinstance(other, Vector):
return Vector(self.x + other.x, self.y + other.y)
elif isinstance(other, (int, float)):
return Vector(self.x + other, self.y + other)
else:
raise TypeError('Type mismatch')
def __iadd__(self, other): # +=
if isinstance(other, Vector):
self.x += other.x
self.y += other.y
elif isinstance(other, (int, float)):
self.x += other
self.y += other
else:
raise TypeError(f'Type mismatch <{type(other)}>')
return self
def __sub__(self, other): # -
if isinstance(other, Vector):
return Vector(self.x - other.x, self.y - other.y)
elif isinstance(other, (int, float)):
return Vector(self.x - other, self.y - other)
else:
raise TypeError('Type mismatch')
def __isub__(self, other): # -=
if isinstance(other, Vector):
self.x -= other.x
self.y -= other.y
elif isinstance(other, (int, float)):
self.x -= other
self.y -= other
else:
raise TypeError('Type mismatch')
return self
def __mul__(self, other): # *
if isinstance(other, Vector):
raise TypeError('Not yet defined')
elif isinstance(other, (int, float)):
return Vector(self.x * other, self.y * other)
else:
raise TypeError('Type mismatch')
def __rmul__(self, other): # *
return self * other
def __imul__(self, other): # *=
if isinstance(other, Vector):
raise TypeError('Not yet defined')
elif isinstance(other, (int, float)):
self.x *= other
self.y *= other
else:
raise TypeError('Type mismatch')
return self
def __truediv__(self, other): # /
if isinstance(other, Vector):
raise TypeError('Not yet defined')
elif isinstance(other, (int, float)):
return Vector(self.x / other, self.y / other)
else:
raise TypeError('Type mismatch')
def __idiv__(self, other): # /=
if isinstance(other, Vector):
raise TypeError('Not yet defined')
elif isinstance(other, (int, float)):
self.x /= other
self.y /= other
else:
raise TypeError('Type mismatch')
return self
def __neg__(self): # negation
return Vector(-self.x, -self.y)
def __pos__(self): # ?
raise TypeError('Not yet defined')
def __abs__(self): # abs()
return Vector(abs(self.x), abs(self.y))
def __invert__(self): # ~
raise TypeError('Not yet defined')
def __int__(self): # int()
return Vector(int(self.x), int(self.y))
def __float(self): # float()
return Vector(float(self.x), float(self.y))
def __lt__(self, other): # <
if isinstance(other, Vector):
return self.magnitude < other.magnitude
elif isinstance(other, (int, float)):
return self.magnitude < other
else:
raise TypeError('Type mismatch')
def __le__(self, other): # <=
if isinstance(other, Vector):
return self.magnitude <= other.magnitude
elif isinstance(other, (int, float)):
return self.magnitude <= other
else:
raise TypeError('Type mismatch')
def __eq__(self, other): # ==
if isinstance(other, Vector):
return self.x == other.x and self.y == other.y
else:
raise TypeError('Type mismatch')
def __ne__(self, other): # !=
if isinstance(other, Vector):
return self.x != other.x or self.y != other.y
else:
raise TypeError('Type mismatch')
return self.x != other.x or self.y != other.y
def __ge__(self, other): # >=
if isinstance(other, Vector):
return self.magnitude >= other.magnitude
elif isinstance(other, (int, float)):
return self.magnitude >= other
else:
raise TypeError('Type mismatch')
def __gt__(self, other): # >
if isinstance(other, Vector):
return self.magnitude > other.magnitude
elif isinstance(other, (int, float)):
return self.magnitude > other
else:
raise TypeError('Type mismatch')
@property
def magnitude(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
@magnitude.setter
def magnitude(self, factor):
mag = self.norm * factor
self.x = mag.x
self.y = mag.y
def normalize(self):
if self.magnitude != 0:
norm = self / self.magnitude
self.x = norm.x
self.y = norm.y
@property
def norm(self):
if self.magnitude != 0:
return self / self.magnitude
return self
@property
def position(self):
return (int(self.x), int(self.y))
def scale(self, factor):
return factor * self.norm
def randomize(self, factor=1):
self.x = factor * (2 * random.random() - 1)
self.y = factor * (2 * random.random() - 1)
@staticmethod
def random_2D():
vector = Vector(0,0)
vector.randomize()
return vector
def limit(self, value):
if self.magnitude > value:
self.magnitude = value
def copy(self):
return Vector(self.x, self.y)
point = Vector(2,2)