python_morsels/point/point.py

50 lines
1.4 KiB
Python
Raw Permalink Normal View History

2021-01-06 23:35:57 +01:00
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __eq__(self, other):
if type(other) != Point:
return False
return (self.x, self.y, self.z) == (other.x, other.y, other.z)
def __add__(self, other):
if not isinstance(other, Point):
raise TypeError(f"Can only add points together, type {type(other)} given.")
x = self.x + other.x
y = self.y + other.y
z = self.z + other.z
return Point(x, y, z)
def __sub__(self, other):
if not isinstance(other, Point):
raise TypeError(
f"Can only substract points together, type {type(other)} given."
)
x = self.x - other.x
y = self.y - other.y
z = self.z - other.z
return Point(x, y, z)
def __mul__(self, num):
if not (isinstance(num, int) or isinstance(num, float)):
raise TypeError(
f"Can only scale points with a number, type {type(num)} given."
)
x = num * self.x
y = num * self.y
z = num * self.z
return Point(x, y, z)
def __rmul__(self, num):
return Point.__mul__(self, num)
def __iter__(self):
yield self.x
yield self.y
yield self.z
def __repr__(self):
return f"Point(x={self.x}, y={self.y}, z={self.z})"