X-Git-Url: https://git.ao2.it/flexagon-toolkit.git/blobdiff_plain/d7db3a7d7112d2bf77c96d0f027bf0e00ab15215..aabf42f895331f171651e971661d4d7ace18e0d8:/src/flexagon/tetraflexagon_diagram.py?ds=sidebyside diff --git a/src/flexagon/tetraflexagon_diagram.py b/src/flexagon/tetraflexagon_diagram.py index eb2c807..a79a7a0 100755 --- a/src/flexagon/tetraflexagon_diagram.py +++ b/src/flexagon/tetraflexagon_diagram.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# An class to draw tetraflexagons +# A class to draw tetraflexagons # # Copyright (C) 2018 Antonio Ospite # @@ -17,7 +17,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from math import sin, cos from .tritetraflexagon import TriTetraflexagon @@ -40,6 +39,9 @@ class TetraflexagonDiagram(object): self.plan_origin = ((self.backend.width - self.tile_side * 5) / 2, self.x_border) + self.backfaces_origin = (self.squares_centers[0][0] - self.tile_side, + self.squares_centers[0][1] + self.x_border * 2 + self.tile_side) + self.squares_color_map = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] def _init_centers(self): @@ -55,9 +57,8 @@ class TetraflexagonDiagram(object): for tile in square.tiles: # offset by 1 or -1 times the tile radius - tile_cx = cx + self.tile_radius * ((tile.index % 2) * 2 - 1) - tile_cy = cy + self.tile_radius * ((tile.index > 1) * 2 - 1) - self.tiles_centers[square.index][tile.index] = (tile_cx, tile_cy) + tile_xoffset, tile_yoffset = tile.calc_offset_in_square(self.tile_side) + self.tiles_centers[square.index][tile.index] = (cx + tile_xoffset, cy + tile_yoffset) def get_square_center(self, square): return self.squares_centers[square.index] @@ -71,6 +72,16 @@ class TetraflexagonDiagram(object): x, y = tile.calc_plan_coordinates(self.tile_side, i, j) return x0 + x, y0 + y + def get_backface_tile_transform(self, tile): + src_x, src_y = self.get_tile_center(tile) + tile_xoffset, tile_yoffset = tile.calc_offset_in_square(self.tile_side) + # When calculating dest_x the minus in the formula switches the columns. + dest_x = self.backfaces_origin[0] + self.tile_side - tile_xoffset + dest_y = self.backfaces_origin[1] + self.tile_side + tile_yoffset + + return self.backend.calc_rotate_translate_transform(src_x, src_y, + dest_x, dest_y, 0) + def get_tile_transform(self, tile): """Calculate the transformation matrix from a tile in an square to the correspondent tile in the plan. @@ -81,61 +92,10 @@ class TetraflexagonDiagram(object): dest_x, dest_y = self.get_tile_center_in_plan(tile) i, j = self.tetraflexagon.get_tile_plan_position(tile) - theta = tile.get_angle_in_plan(i, j) - - # The transformation from a tile in the square to the correspondent - # tile in the plan is composed by these steps: - # - # 1. rotate by 'theta' around (src_x, src_y); - # 2. move to (dest_x, dest_y). - # - # Step 1 can be expressed by these sub-steps: - # - # 1a. translate by (-src_x, -src_y) - # 1b. rotate by 'theta' - # 1c. translate by (src_x, src_y) - # - # Step 2. can be expressed by a translation like: - # - # 2a. translate by (dest_x - src_x, dest_y - src_y) - # - # The consecutive translations 1c and 2a can be easily combined, so - # the final steps are: - # - # T1 -> translate by (-src_x, -src_y) - # R -> rotate by 'theta' - # T2 -> translate by (dest_x, dest_y) - # - # Using affine transformations these are expressed as: - # - # | 1 0 -src_x | - # T1 = | 0 1 -src_y | - # | 0 0 1 | - # - # | cos(theta) -sin(theta) 0 | - # R = | sin(theta) con(theta) 0 | - # | 0 0 1 | - # - # | 1 0 dest_x | - # T2 = | 0 1 dest_y | - # | 0 0 1 | - # - # Composing these transformations into one is achieved by multiplying - # the matrices from right to left: - # - # T = T2 * R * T1 - # - # NOTE: To remember this think about composing functions: T2(R(T1())), - # the inner one is performed first. - # - # The resulting T matrix is the one below. - matrix = [ - cos(theta), -sin(theta), -src_x * cos(theta) + src_y * sin(theta) + dest_x, - sin(theta), cos(theta), -src_x * sin(theta) - src_y * cos(theta) + dest_y, - 0, 0, 1 - ] - - return matrix + theta = tile.calc_angle_in_plan(i, j) + + return self.backend.calc_rotate_translate_transform(src_x, src_y, + dest_x, dest_y, theta) def draw_square_template(self, square): for tile in square.tiles: @@ -146,7 +106,9 @@ class TetraflexagonDiagram(object): side = self.tile_side color = self.squares_color_map[tile.square.index] - self.backend.draw_rect_from_center(cx, cy, side, side, theta, color) + self.backend.draw_rect_from_center(cx, cy, side, side, theta, + stroke_color=color, + fill_color=None) corners_labels = "ABC" corner_text = corners_labels[tile.square.index] + str(tile.index + 1)