# HG changeset patch # User Santeri Piippo # Date 1513079177 -7200 # Node ID e340e35d6e4c84877e8a76127c0ad1ece70df6b8 # Parent 8eb83f200486dca3da385b14b0f920cd3260b703 Added vertex operators diff -r 8eb83f200486 -r e340e35d6e4c geometry.py --- a/geometry.py Tue Dec 12 11:50:18 2017 +0200 +++ b/geometry.py Tue Dec 12 13:46:17 2017 +0200 @@ -1,5 +1,9 @@ class Vertex: def __init__(self, x, y, z): + if not all(is_real(coordinate) for coordinate in (x, y, z)): + raise ValueError(str.format('Bad vertex coordinates: {!r}', + (x, y, z), + )) self.x, self.y, self.z = x, y, z def __repr__(self): return str.format('Vertex({!r}, {!r}, {!r})', self.x, self.y, self.z) @@ -14,6 +18,34 @@ @property def coordinates(self): return self.x, self.y, self.z + def __add__(self, other): + return Vertex(self.x + other.x, self.y + other.y, self.z + other.z) + def __neg__(self): + return Vertex(-self.x, -self.y, -self.z) + def __sub__(self, other): + return self + (-other) + def __mul__(self, scalar): + return Vertex(self.x * scalar, self.y * scalar, self.z * scalar) + def __rmul__(self, other): + return self * other + def __truediv__(self, scalar): + return Vertex(self.x / scalar, self.y / scalar, self.z / scalar) + def __floordiv__(self, scalar): + return Vertex(self.x // scalar, self.y // scalar, self.z // scalar) + def __matmul__(self, transformation_matrix): + return transform(self, transformation_matrix) + def __eq__(self, other): + return self.coordinates == other.coordinates + def __lt__(self, other): + return self.coordinates < other.coordinates + def __hash__(self): + return hash(self.coordinates) + +class VertexOp: + def __init__(self, callable): + self.callable = callable + def __rmul__(self, coordinate): + return self.callable(coordinate) class LineSegment: def __init__(self, v1, v2):