geometry.py

Thu, 21 Dec 2017 12:27:16 +0200

author
Santeri Piippo
date
Thu, 21 Dec 2017 12:27:16 +0200
changeset 8
303c51137cb2
parent 7
0ab0d61ccee8
child 9
fea8e9ae6f29
permissions
-rw-r--r--

Added ldconfig.ldr support

7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
1 def degree_rep(angle):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
2 from math import degrees
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
3 return '∠ %.2f°' % degrees(angle)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
4
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
5 def position_vector(vertex):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
6 assert isinstance(vertex, Vertex)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
7 return Vector(*vertex.coordinates)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
8
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
9 def angle_magnitude_key(angle):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
10 '''
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
11 Returns how great an angle is.
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
12 '''
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
13 from math import pi as π
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
14 normalized_angle = abs(angle) % (2 * π)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
15 if normalized_angle > π:
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
16 normalized_angle = (2 * π) - normalized_angle
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
17 return normalized_angle
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
18
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
19 class Vertex:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
20 def __init__(self, x, y, z):
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
21 if not all(is_real(coordinate) for coordinate in (x, y, z)):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
22 raise ValueError(str.format('Bad vertex coordinates: {!r}',
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
23 (x, y, z),
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
24 ))
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
25 self.x, self.y, self.z = x, y, z
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
26 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
27 return str.format('Vertex({!r}, {!r}, {!r})', self.x, self.y, self.z)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
28 def distance_to(self, other):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
29 # can't use hypot because of 3 arguments
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
30 from math import sqrt
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
31 return sqrt(
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
32 (self.x - other.x) ** 2 +
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
33 (self.y - other.y) ** 2 +
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
34 (self.z - other.z) ** 2
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
35 )
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
36 @property
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
37 def coordinates(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
38 return self.x, self.y, self.z
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
39 def __add__(self, other):
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
40 assert not (type(self) == type(other) == Vertex)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
41 if type(self) == Vector and type(other) == Vector:
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
42 result_type = Vector
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
43 else:
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
44 result_type = Vertex
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
45 return result_type(self.x + other.x, self.y + other.y, self.z + other.z)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
46 def __radd__(self, other):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
47 return self + other
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
48 def __neg__(self):
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
49 return type(self)(-self.x, -self.y, -self.z)
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
50 def __sub__(self, other):
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
51 result = self + (-position_vector(other))
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
52 if isinstance(other, Vertex):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
53 return Vector(*result.coordinates)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
54 else:
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
55 return result
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
56 def __mul__(self, scalar):
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
57 assert is_real(scalar)
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
58 return type(self)(self.x * scalar, self.y * scalar, self.z * scalar)
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
59 def __rmul__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
60 return self * other
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
61 def __truediv__(self, scalar):
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
62 assert is_real(scalar)
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
63 return type(self)(self.x / scalar, self.y / scalar, self.z / scalar)
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
64 def __floordiv__(self, scalar):
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
65 assert is_real(scalar)
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
66 return type(self)(self.x // scalar, self.y // scalar, self.z // scalar)
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
67 def __matmul__(self, transformation_matrix):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
68 return transform(self, transformation_matrix)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
69 def __eq__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
70 return self.coordinates == other.coordinates
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
71 def __lt__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
72 return self.coordinates < other.coordinates
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
73 def __hash__(self):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
74 return hash(self.coordinates)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
75
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
76 class VertexOp:
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
77 def __init__(self, callable):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
78 self.callable = callable
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
79 def __rmul__(self, coordinate):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
80 return self.callable(coordinate)
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
81
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
82 class LineSegment:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
83 def __init__(self, v1, v2):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
84 self.v1, self.v2 = v1, v2
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
85 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
86 return str.format('LineSegment({!r}, {!r})', self.v1, self.v2)
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
87 @property
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
88 def length(self):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
89 return self.v1.distance_to(self.v2)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
90 @property
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
91 def vertices(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
92 return self.v1, self.v2
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
93 def __len__(self):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
94 return 2
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
95 def __getitem__(self, index):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
96 return self.vertices[index]
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
97
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
98 class IndexRing:
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
99 def __init__(self, container):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
100 self.container = container
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
101 def __getitem__(self, index):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
102 return self.container[index % len(self.container)]
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
103
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
104 class Vector(Vertex):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
105 def __init__(self, x, y, z):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
106 super().__init__(x, y, z)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
107 def __repr__(self):
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
108 return str.format('Vector({!r}, {!r}, {!r})', self.x, self.y, self.z)
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
109 def length(self):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
110 from math import sqrt
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
111 return sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
112 def normalized(self):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
113 length = self.length()
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
114 if length > 0:
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
115 return Vector(self.x / length, self.y / length, self.z / length)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
116 else:
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
117 return Vector(0, 0, 0)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
118
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
119 def dot_product(v1, v2):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
120 ''' Returns the dot product v1 · v2. '''
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
121 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
122
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
123 def cross_product(v1, v2):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
124 ''' Returns the cross product v1 × v2. '''
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
125 return Vector(
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
126 x = v1.y * v2.z - v1.z * v2.y,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
127 y = v1.z * v2.x - v1.x * v2.z,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
128 z = v1.x * v2.y - v1.y * v2.x,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
129 )
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
130
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
131 def unit_normal(a, b, c):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
132 x = Matrix3x3([
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
133 1, a.y, a.z,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
134 1, b.y, b.z,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
135 1, c.y, c.z,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
136 ]).determinant()
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
137 y = Matrix3x3([
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
138 a.x, 1, a.z,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
139 b.x, 1, b.z,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
140 c.x, 1, c.z,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
141 ]).determinant()
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
142 z = Matrix3x3([
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
143 a.x, a.y, 1,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
144 b.x, b.y, 1,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
145 c.x, c.y, 1,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
146 ]).determinant()
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
147 return Vector(x, y, z).normalized()
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
148
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
149 def pairs(iterable, *, count = 2):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
150 '''
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
151 Iterates the given iterable and returns tuples containing `count`
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
152 sequential values in the iterable.
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
153
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
154 Formally, for a vector A with indices 0…n and pair count k, this
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
155 function yields:
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
156 (A₀, A₁, …, Aₖ),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
157 (A₁, A₂, …, Aₖ₊₁),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
158 …,
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
159 (Aₙ₋₂, Aₙ₋₁, …, Aₖ₋₂),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
160 (Aₙ₋₁, A₀, …, Aₖ₋₁).
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
161 '''
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
162 iterable_count = len(iterable)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
163 for index in range(iterable_count):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
164 yield tuple(
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
165 iterable[(index + offset) % iterable_count]
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
166 for offset in range(count)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
167 )
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
168
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
169 class Polygon:
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
170 def __init__(self, vertices):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
171 self.vertices = vertices
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
172 def __repr__(self):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
173 return str.format('Polygon({!r})', self.vertices)
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
174 def area(self):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
175 total = Vector(0, 0, 0)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
176 for v1, v2 in pairs(self.vertices):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
177 total += cross_product(v1, v2)
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
178 return dot_product(total, unit_normal(self.vertices[0], self.vertices[1], self.vertices[2]))
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
179 def centroid(self):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
180 ...
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
181 raise NotImplementedError
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
182 def __getitem__(self, index):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
183 return self.vertices[index % len(self.vertices)]
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
184 def __len__(self):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
185 return len(self.vertices)
7
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
186 @property
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
187 def perimeter_lines(self):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
188 for v1, v2 in pairs(self.vertices):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
189 yield LineSegment(v1, v2)
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
190 @property
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
191 def smallest_angle(self):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
192 from math import acos, inf
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
193 min_angle = inf
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
194 for v1, v2, v3 in pairs(self.vertices, count = 3):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
195 vec1 = (position_vector(v3) - position_vector(v2)).normalized()
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
196 vec2 = (position_vector(v1) - position_vector(v2)).normalized()
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
197 cosine = dot_product(vec1, vec2) / vec1.length() / vec2.length()
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
198 min_angle = min(min_angle, angle_magnitude_key(acos(cosine)))
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
199 return min_angle
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
200 @property
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
201 def hairline_ratio(self):
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
202 lengths = [line.length for line in self.perimeter_lines]
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
203 return max(lengths) / min(lengths)
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
204
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
205 def is_real(number):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
206 return isinstance(number, int) or isinstance(number, float)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
207
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
208 class Matrix3x3:
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
209 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
210 A 3×3 matrix forming the top-left portion of a full 4×4 transformation
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
211 matrix.
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
212 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
213 def __init__(self, values):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
214 assert(all(is_real(x) for x in values))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
215 assert len(values) == 9
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
216 self.values = values
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
217 def __repr__(self):
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
218 return str.format('Matrix3x3({!r})', self.values)
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
219 def __getitem__(self, index):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
220 return self.values[index]
6
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
221 def determinant(self):
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
222 v = self.values
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
223 return sum([
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
224 +(v[0] * v[4] * v[8]),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
225 +(v[1] * v[5] * v[6]),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
226 +(v[2] * v[3] * v[7]),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
227 -(v[2] * v[4] * v[6]),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
228 -(v[1] * v[3] * v[8]),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
229 -(v[0] * v[5] * v[7]),
6da1e81c5652 Added code to compute areas of triangles and quadrilaterals
Santeri Piippo
parents: 5
diff changeset
230 ])
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
231
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
232 def complete_matrix(matrix, anchor):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
233 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
234 Combines a 3×3 matrix and an anchor vertex into a full 4×4
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
235 transformation matrix.
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
236 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
237 return [
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
238 matrix[0], matrix[1], matrix[2], anchor.x,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
239 matrix[3], matrix[4], matrix[5], anchor.y,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
240 matrix[6], matrix[7], matrix[8], anchor.z,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
241 0, 0, 0, 1,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
242 ]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
243
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
244 def transform(vertex, transformation_matrix):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
245 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
246 Transforms a vertex by a 4×4 transformation matrix.
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
247 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
248 u = transformation_matrix[0] * vertex.x \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
249 + transformation_matrix[1] * vertex.y \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
250 + transformation_matrix[2] * vertex.z \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
251 + transformation_matrix[3]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
252 v = transformation_matrix[4] * vertex.x \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
253 + transformation_matrix[5] * vertex.y \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
254 + transformation_matrix[6] * vertex.z \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
255 + transformation_matrix[7]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
256 w = transformation_matrix[8] * vertex.x \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
257 + transformation_matrix[9] * vertex.y \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
258 + transformation_matrix[10] * vertex.z \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
259 + transformation_matrix[11]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
260 return Vertex(u, v, w)

mercurial