Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Пример того, как можно делать запись вектороного произведения точек в виде cp[v, w], а не cp(v, w)
- class Point:
- """Класс Точка"""
- __slots__ = ['x', 'y']
- def __init__(self, x=0, y=0):
- self.x = x
- self.y = y
- def __sub__(v, w):
- return Point(v.x - w.x, v.y - w.y)
- def __repr__(self):
- return f'Point({self.x}, {self.y})'
- def __str__(self):
- return f'({self.x}, {self.y})'
- def dp(v, w):
- """Dot product — скалярное произведение"""
- return v.x * w.x + v.y * w.y
- class _CrossProductHelper:
- """Специальный класс, который позволит обрабатывать cp[key]"""
- @staticmethod # Ибо самого объекта класса никому не нужно
- def __getitem__(key): # key в данном случае — это кортеж из пары точек
- v, w = key
- return v.x * w.x - v.y * w.y
- cp = _CrossProductHelper()
- # Берём две точки
- v = Point(1, 1)
- w = Point(1, -1)
- print('Dot (scalar) product of', v, 'and', w, 'is', dp(v, w))
- print('Cross product of', v, 'and', w, 'is', cp[v, w])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement