geometry.py

changeset 5
e340e35d6e4c
parent 3
1dc58f44d556
child 6
6da1e81c5652
--- 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):

mercurial