3 # A class to draw tetraflexagons
5 # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from math import sin, cos
21 from .tritetraflexagon import TriTetraflexagon
24 class TetraflexagonDiagram(object):
25 def __init__(self, x_border, backend=None):
26 self.x_border = x_border
27 self.backend = backend
29 self.tetraflexagon = TriTetraflexagon()
31 num_squares = len(self.tetraflexagon.squares)
32 self.square_side = (self.backend.height - (x_border * 3)) / (num_squares)
33 self.square_radius = self.square_side / 2
34 self.tile_side = self.square_radius
35 self.tile_radius = self.tile_side / 2
39 # draw the plan centered wrt. the squares
40 self.plan_origin = ((self.backend.width - self.tile_side * 5) / 2,
43 self.squares_color_map = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
45 def _init_centers(self):
46 # Preallocate the lists to be able to access them by indices in the
48 self.squares_centers = [None for h in self.tetraflexagon.squares]
49 self.tiles_centers = [[None for t in h.tiles] for h in self.tetraflexagon.squares]
51 cy = self.backend.height - (self.square_radius + self.x_border)
52 for square in self.tetraflexagon.squares:
53 cx = self.x_border + (2 * self.square_radius + self.x_border) * (square.index + 1)
54 self.squares_centers[square.index] = (cx, cy)
56 for tile in square.tiles:
57 # offset by 1 or -1 times the tile radius
58 tile_cx = cx + self.tile_radius * ((tile.index % 2) * 2 - 1)
59 tile_cy = cy + self.tile_radius * ((tile.index > 1) * 2 - 1)
60 self.tiles_centers[square.index][tile.index] = (tile_cx, tile_cy)
62 def get_square_center(self, square):
63 return self.squares_centers[square.index]
65 def get_tile_center(self, tile):
66 return self.tiles_centers[tile.square.index][tile.index]
68 def get_tile_center_in_plan(self, tile):
69 x0, y0 = self.plan_origin
70 i, j = self.tetraflexagon.get_tile_plan_position(tile)
71 x, y = tile.calc_plan_coordinates(self.tile_side, i, j)
74 def get_tile_transform(self, tile):
75 """Calculate the transformation matrix from a tile in an square to
76 the correspondent tile in the plan.
78 Return the matrix as a list of values sorted in row-major order."""
80 src_x, src_y = self.get_tile_center(tile)
81 dest_x, dest_y = self.get_tile_center_in_plan(tile)
83 i, j = self.tetraflexagon.get_tile_plan_position(tile)
84 theta = tile.calc_angle_in_plan(i, j)
86 # The transformation from a tile in the square to the correspondent
87 # tile in the plan is composed by these steps:
89 # 1. rotate by 'theta' around (src_x, src_y);
90 # 2. move to (dest_x, dest_y).
92 # Step 1 can be expressed by these sub-steps:
94 # 1a. translate by (-src_x, -src_y)
95 # 1b. rotate by 'theta'
96 # 1c. translate by (src_x, src_y)
98 # Step 2. can be expressed by a translation like:
100 # 2a. translate by (dest_x - src_x, dest_y - src_y)
102 # The consecutive translations 1c and 2a can be easily combined, so
103 # the final steps are:
105 # T1 -> translate by (-src_x, -src_y)
106 # R -> rotate by 'theta'
107 # T2 -> translate by (dest_x, dest_y)
109 # Using affine transformations these are expressed as:
112 # T1 = | 0 1 -src_y |
115 # | cos(theta) -sin(theta) 0 |
116 # R = | sin(theta) con(theta) 0 |
120 # T2 = | 0 1 dest_y |
123 # Composing these transformations into one is achieved by multiplying
124 # the matrices from right to left:
128 # NOTE: To remember this think about composing functions: T2(R(T1())),
129 # the inner one is performed first.
131 # The resulting T matrix is the one below.
133 cos(theta), -sin(theta), -src_x * cos(theta) + src_y * sin(theta) + dest_x,
134 sin(theta), cos(theta), -src_x * sin(theta) - src_y * cos(theta) + dest_y,
140 def draw_square_template(self, square):
141 for tile in square.tiles:
142 cx, cy = self.get_tile_center(tile)
143 self.draw_tile_template(tile, cx, cy, 0)
145 def draw_tile_template(self, tile, cx, cy, theta):
146 side = self.tile_side
147 color = self.squares_color_map[tile.square.index]
149 self.backend.draw_rect_from_center(cx, cy, side, side, theta, color)
151 corners_labels = "ABC"
152 corner_text = corners_labels[tile.square.index] + str(tile.index + 1)
153 self.backend.draw_centered_text(cx, cy, corner_text, 0, color)
155 def draw_plan_template(self):
156 x0, y0 = self.plan_origin
157 for square in self.tetraflexagon.squares:
158 for tile in square.tiles:
159 i, j = self.tetraflexagon.get_tile_plan_position(tile)
160 x, y = tile.calc_plan_coordinates(self.tile_radius, i, j)
161 theta = tile.get_angle_in_plan(i, j)
162 self.draw_tile_template(tile, x0 + x, y0 + y, theta)
164 def draw_template(self):
165 for square in self.tetraflexagon.squares:
166 self.draw_square_template(square)
168 self.draw_plan_template()