Advertisement
Korotkodul

Vector2D

Nov 15th, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. from numbers import Real
  2. import math
  3. from math import acos, degrees
  4.  
  5. class Vector2D:
  6.     _abscissa: float
  7.     _ordinate: float
  8.     def __init__(self, abscissa = 0.0, ordinate = 0.0):
  9.         self._abscissa = abscissa
  10.         self._ordinate = ordinate
  11.  
  12.     @property
  13.     def abscissa(self):
  14.         return self._abscissa
  15.  
  16.     @property
  17.     def ordinate(self):
  18.         return self._ordinate
  19.  
  20.     def __str__(self):
  21.         return f"Vector2D(abscissa={self._abscissa}, ordinate={self._ordinate})"
  22.  
  23.     def __eq__(self, other):
  24.         return self._abscissa == other.abscissa and self._ordinate == other.ordinate
  25.  
  26.     def __lt__(self, other):
  27.         return self._abscissa < other.abscissa or self._abscissa == other.abscissa and self._ordinate < other.ordinate
  28.  
  29.     def __le__(self, other):
  30.         return self._abscissa < other.abscissa or self._abscissa == other.abscissa and self._ordinate <= other.ordinate
  31.  
  32.     def __abs__(self):
  33.         return math.sqrt(self._abscissa**2 + self._ordinate**2)
  34.  
  35.     def __bool__(self):
  36.         return abs(self) > 0
  37.  
  38.     def __mul__(self, k):
  39.         res = Vector2D(self._abscissa, self._ordinate)
  40.         res._abscissa *= k
  41.         res._ordinate *= k
  42.         return res
  43.  
  44.     def __rmul__(self, k):
  45.         res = Vector2D(self._abscissa, self._ordinate)
  46.         res._abscissa *= k
  47.         res._ordinate *= k
  48.         return res
  49.  
  50.     def __truediv__(self, k):
  51.         if k == 0:
  52.             raise ZeroDivisionError
  53.         res = Vector2D(self._abscissa, self._ordinate)
  54.         res._abscissa /= k
  55.         res._ordinate /= k
  56.         return res
  57.  
  58.     def __add__(self, k):
  59.         res = Vector2D(self._abscissa, self._ordinate)
  60.         if isinstance(k, Real):
  61.             res._abscissa += k
  62.             res._ordinate += k
  63.         elif isinstance(k, Vector2D):
  64.             res._abscissa += k._abscissa
  65.             res._ordinate += k._ordinate
  66.         return res
  67.  
  68.     def __radd__(self, k):
  69.         res = Vector2D(self._abscissa, self._ordinate)
  70.         if isinstance(k, Real):
  71.             res._abscissa += k
  72.             res._ordinate += k
  73.         elif isinstance(k, Vector2D):
  74.             res._abscissa += k._abscissa
  75.             res._ordinate += k._ordinate
  76.         return res
  77.  
  78.     def __sub__(self, k):
  79.         res = Vector2D(self._abscissa, self._ordinate)
  80.         if isinstance(k, Real):
  81.             res._abscissa -= k
  82.             res._ordinate -= k
  83.         elif isinstance(k, Vector2D):
  84.             res._abscissa -= k._abscissa
  85.             res._ordinate -= k._ordinate
  86.         return res
  87.  
  88.     def __neg__(self):
  89.         res = Vector2D(-self._abscissa, -self._ordinate)
  90.         return res
  91.  
  92.     def __bool__(self):
  93.         return abs(self) != 0
  94.  
  95.  
  96.     def __complex__(self):
  97.         return complex(real=self._abscissa, imag=self._ordinate)
  98.  
  99.     def __float__(self):
  100.         return float(abs(self))
  101.  
  102.     def __int__(self):
  103.         return int(abs(self))
  104.  
  105.     def __matmul__(self, other):
  106.         return self._abscissa * other._abscissa + self._ordinate * other._ordinate
  107.  
  108.     def  get_angle(self, other):
  109.         if abs(self) == 0 or abs(other) == 0:
  110.             raise ValueError("Impossible to find angle if some vector is null")
  111.         return degrees(acos(round((self @ other) / (abs(self) * abs(other)), 1)))
  112.  
  113.     def conjugate(self):
  114.         return Vector2D(self._abscissa, -self._ordinate)
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement