Advertisement
Korotkodul

Vector2D

Nov 15th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. from numbers import Real
  2. import math
  3.  
  4. class Vector2D:
  5.     _abscissa: float
  6.     _ordinate: float
  7.     def __init__(self, abscissa = 0.0, ordinate = 0.0):
  8.         self._abscissa = abscissa
  9.         self._ordinate = ordinate
  10.  
  11.     @property
  12.     def abscissa(self):
  13.         return self._abscissa
  14.  
  15.     @property
  16.     def ordinate(self):
  17.         return self._ordinate
  18.  
  19.     def __str__(self):
  20.         return f"Vector2D(abscissa={self._abscissa}, ordinate={self._ordinate})"
  21.  
  22.     def __eq__(self, other):
  23.         return self._abscissa == other.abscissa and self._ordinate == other.ordinate
  24.  
  25.     def __lt__(self, other):
  26.         return self._abscissa < other.abscissa or self._abscissa == other.abscissa and self.ordinate < other.ordinate
  27.  
  28.     def __le__(self, other):
  29.         return self._abscissa <= other.abscissa or self._abscissa == other.abscissa and self.ordinate <= other.ordinate
  30.  
  31.     def __abs__(self):
  32.         return math.sqrt(self._abscissa**2 + self._ordinate**2)
  33.  
  34.     def __bool__(self):
  35.         return abs(self) > 0
  36.  
  37.     def __mul__(self, k):
  38.         res = Vector2D(self._abscissa, self._ordinate)
  39.         res.abscissa *= k
  40.         res.ordinate *= k
  41.  
  42.     def __rmul__(self, k):
  43.         res = Vector2D(self._abscissa, self._ordinate)
  44.         res.abscissa *= k
  45.         res.ordinate *= k
  46.  
  47.     def __truediv__(self, k):
  48.         res = Vector2D(self._abscissa, self._ordinate)
  49.         res.abscissa /= k
  50.         res.ordinate /= k
  51.  
  52. a = Vector2D(1, 2)
  53. b = Vector2D(2, 2)
  54. print(a >= b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement