Add support for tetraflexagons
[flexagon-toolkit.git] / src / flexagon / tetraflexagon_diagram.py
diff --git a/src/flexagon/tetraflexagon_diagram.py b/src/flexagon/tetraflexagon_diagram.py
new file mode 100755 (executable)
index 0000000..eb2c807
--- /dev/null
@@ -0,0 +1,168 @@
+#!/usr/bin/env python
+#
+# An class to draw tetraflexagons
+#
+# Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from math import sin, cos
+from .tritetraflexagon import TriTetraflexagon
+
+
+class TetraflexagonDiagram(object):
+    def __init__(self, x_border, backend=None):
+        self.x_border = x_border
+        self.backend = backend
+
+        self.tetraflexagon = TriTetraflexagon()
+
+        num_squares = len(self.tetraflexagon.squares)
+        self.square_side = (self.backend.height - (x_border * 3)) / (num_squares)
+        self.square_radius = self.square_side / 2
+        self.tile_side = self.square_radius
+        self.tile_radius = self.tile_side / 2
+
+        self._init_centers()
+
+        # draw the plan centered wrt. the squares
+        self.plan_origin = ((self.backend.width - self.tile_side * 5) / 2,
+                            self.x_border)
+
+        self.squares_color_map = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
+
+    def _init_centers(self):
+        # Preallocate the lists to be able to access them by indices in the
+        # loops below.
+        self.squares_centers = [None for h in self.tetraflexagon.squares]
+        self.tiles_centers = [[None for t in h.tiles] for h in self.tetraflexagon.squares]
+
+        cy = self.backend.height - (self.square_radius + self.x_border)
+        for square in self.tetraflexagon.squares:
+            cx = self.x_border + (2 * self.square_radius + self.x_border) * (square.index + 1)
+            self.squares_centers[square.index] = (cx, cy)
+
+            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)
+
+    def get_square_center(self, square):
+        return self.squares_centers[square.index]
+
+    def get_tile_center(self, tile):
+        return self.tiles_centers[tile.square.index][tile.index]
+
+    def get_tile_center_in_plan(self, tile):
+        x0, y0 = self.plan_origin
+        i, j = self.tetraflexagon.get_tile_plan_position(tile)
+        x, y = tile.calc_plan_coordinates(self.tile_side, i, j)
+        return x0 + x, y0 + y
+
+    def get_tile_transform(self, tile):
+        """Calculate the transformation matrix from a tile in an square to
+        the correspondent tile in the plan.
+
+        Return the matrix as a list of values sorted in row-major order."""
+
+        src_x, src_y = self.get_tile_center(tile)
+        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
+
+    def draw_square_template(self, square):
+        for tile in square.tiles:
+            cx, cy = self.get_tile_center(tile)
+            self.draw_tile_template(tile, cx, cy, 0)
+
+    def draw_tile_template(self, tile, cx, cy, theta):
+        side = self.tile_side
+        color = self.squares_color_map[tile.square.index]
+
+        self.backend.draw_rect_from_center(cx, cy, side, side, theta, color)
+
+        corners_labels = "ABC"
+        corner_text = corners_labels[tile.square.index] + str(tile.index + 1)
+        self.backend.draw_centered_text(cx, cy, corner_text, 0, color)
+
+    def draw_plan_template(self):
+        x0, y0 = self.plan_origin
+        for square in self.tetraflexagon.squares:
+            for tile in square.tiles:
+                i, j = self.tetraflexagon.get_tile_plan_position(tile)
+                x, y = tile.calc_plan_coordinates(self.tile_radius, i, j)
+                theta = tile.get_angle_in_plan(i, j)
+                self.draw_tile_template(tile, x0 + x, y0 + y, theta)
+
+    def draw_template(self):
+        for square in self.tetraflexagon.squares:
+            self.draw_square_template(square)
+
+        self.draw_plan_template()