Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from numbers import Real
- import math
- from math import acos, degrees
- class Vector2D:
- _abscissa: float
- _ordinate: float
- def __init__(self, abscissa = 0.0, ordinate = 0.0):
- self._abscissa = abscissa
- self._ordinate = ordinate
- @property
- def abscissa(self):
- return self._abscissa
- @property
- def ordinate(self):
- return self._ordinate
- def __str__(self):
- return f"Vector2D(abscissa={self._abscissa}, ordinate={self._ordinate})"
- def __eq__(self, other):
- return self._abscissa == other.abscissa and self._ordinate == other.ordinate
- def __lt__(self, other):
- return self._abscissa < other.abscissa or self._abscissa == other.abscissa and self._ordinate < other.ordinate
- def __le__(self, other):
- return self._abscissa < other.abscissa or self._abscissa == other.abscissa and self._ordinate <= other.ordinate
- def __abs__(self):
- return math.sqrt(self._abscissa**2 + self._ordinate**2)
- def __bool__(self):
- return abs(self) > 0
- def __mul__(self, k):
- res = Vector2D(self._abscissa, self._ordinate)
- res._abscissa *= k
- res._ordinate *= k
- return res
- def __rmul__(self, k):
- res = Vector2D(self._abscissa, self._ordinate)
- res._abscissa *= k
- res._ordinate *= k
- return res
- def __truediv__(self, k):
- if k == 0:
- raise ZeroDivisionError
- res = Vector2D(self._abscissa, self._ordinate)
- res._abscissa /= k
- res._ordinate /= k
- return res
- def __add__(self, k):
- res = Vector2D(self._abscissa, self._ordinate)
- if isinstance(k, Real):
- res._abscissa += k
- res._ordinate += k
- elif isinstance(k, Vector2D):
- res._abscissa += k._abscissa
- res._ordinate += k._ordinate
- return res
- def __radd__(self, k):
- res = Vector2D(self._abscissa, self._ordinate)
- if isinstance(k, Real):
- res._abscissa += k
- res._ordinate += k
- elif isinstance(k, Vector2D):
- res._abscissa += k._abscissa
- res._ordinate += k._ordinate
- return res
- def __sub__(self, k):
- res = Vector2D(self._abscissa, self._ordinate)
- if isinstance(k, Real):
- res._abscissa -= k
- res._ordinate -= k
- elif isinstance(k, Vector2D):
- res._abscissa -= k._abscissa
- res._ordinate -= k._ordinate
- return res
- def __neg__(self):
- res = Vector2D(-self._abscissa, -self._ordinate)
- return res
- def __bool__(self):
- return abs(self) != 0
- def __complex__(self):
- return complex(real=self._abscissa, imag=self._ordinate)
- def __float__(self):
- return float(abs(self))
- def __int__(self):
- return int(abs(self))
- def __matmul__(self, other):
- return self._abscissa * other._abscissa + self._ordinate * other._ordinate
- def get_angle(self, other):
- if abs(self) == 0 or abs(other) == 0:
- raise ValueError("Impossible to find angle if some vector is null")
- return degrees(acos(round((self @ other) / (abs(self) * abs(other)), 1)))
- def conjugate(self):
- return Vector2D(self._abscissa, -self._ordinate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement