geometry.py

changeset 5
e340e35d6e4c
parent 3
1dc58f44d556
child 6
6da1e81c5652
equal deleted inserted replaced
4:8eb83f200486 5:e340e35d6e4c
1 class Vertex: 1 class Vertex:
2 def __init__(self, x, y, z): 2 def __init__(self, x, y, z):
3 if not all(is_real(coordinate) for coordinate in (x, y, z)):
4 raise ValueError(str.format('Bad vertex coordinates: {!r}',
5 (x, y, z),
6 ))
3 self.x, self.y, self.z = x, y, z 7 self.x, self.y, self.z = x, y, z
4 def __repr__(self): 8 def __repr__(self):
5 return str.format('Vertex({!r}, {!r}, {!r})', self.x, self.y, self.z) 9 return str.format('Vertex({!r}, {!r}, {!r})', self.x, self.y, self.z)
6 def distance_to(self, other): 10 def distance_to(self, other):
7 # can't use hypot because of 3 arguments 11 # can't use hypot because of 3 arguments
12 (self.z - other.z) ** 2 16 (self.z - other.z) ** 2
13 ) 17 )
14 @property 18 @property
15 def coordinates(self): 19 def coordinates(self):
16 return self.x, self.y, self.z 20 return self.x, self.y, self.z
21 def __add__(self, other):
22 return Vertex(self.x + other.x, self.y + other.y, self.z + other.z)
23 def __neg__(self):
24 return Vertex(-self.x, -self.y, -self.z)
25 def __sub__(self, other):
26 return self + (-other)
27 def __mul__(self, scalar):
28 return Vertex(self.x * scalar, self.y * scalar, self.z * scalar)
29 def __rmul__(self, other):
30 return self * other
31 def __truediv__(self, scalar):
32 return Vertex(self.x / scalar, self.y / scalar, self.z / scalar)
33 def __floordiv__(self, scalar):
34 return Vertex(self.x // scalar, self.y // scalar, self.z // scalar)
35 def __matmul__(self, transformation_matrix):
36 return transform(self, transformation_matrix)
37 def __eq__(self, other):
38 return self.coordinates == other.coordinates
39 def __lt__(self, other):
40 return self.coordinates < other.coordinates
41 def __hash__(self):
42 return hash(self.coordinates)
43
44 class VertexOp:
45 def __init__(self, callable):
46 self.callable = callable
47 def __rmul__(self, coordinate):
48 return self.callable(coordinate)
17 49
18 class LineSegment: 50 class LineSegment:
19 def __init__(self, v1, v2): 51 def __init__(self, v1, v2):
20 self.v1, self.v2 = v1, v2 52 self.v1, self.v2 = v1, v2
21 def __repr__(self): 53 def __repr__(self):

mercurial