X-Git-Url: https://git.ao2.it/flexagon-toolkit.git/blobdiff_plain/5f2e900515a64fe483569855b89f26abcc89a441..refs/heads/master:/src/flexagon/hexaflexagon_diagram.py?ds=sidebyside diff --git a/src/flexagon/hexaflexagon_diagram.py b/src/flexagon/hexaflexagon_diagram.py index cdd636b..7b09c94 100755 --- a/src/flexagon/hexaflexagon_diagram.py +++ b/src/flexagon/hexaflexagon_diagram.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -# An class to draw hexaflexagons +# A class to draw hexaflexagons # # Copyright (C) 2018 Antonio Ospite # @@ -17,7 +17,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from math import sin, cos, pi +from math import cos, pi from .trihexaflexagon import TriHexaflexagon @@ -43,6 +43,10 @@ class HexaflexagonDiagram(object): self.plan_origin = (self.x_border * 2. + self.hexagon_radius / 2., self.x_border + self.triangle_radius / 3.) + # The offset of the backfaces relative to the first hexagon + self.backfaces_offsets = (0, + (self.hexagon_radius + self.x_border) * 2) + self.hexagons_color_map = [(1, 0, 0), (0, 1, 0), (0, 0, 1)] def _init_centers(self): @@ -66,6 +70,15 @@ class HexaflexagonDiagram(object): def get_triangle_center(self, triangle): return self.triangles_centers[triangle.hexagon.index][triangle.index] + def get_triangle_center_in_backfaces(self, triangle): + """Get the center of this triangle but in the backface""" + x0, y0 = self.backfaces_offsets + + backface_triangle_index = triangle.get_backface_index() + x, y = self.triangles_centers[triangle.hexagon.index][backface_triangle_index] + + return x0 + x, y0 + y + def get_triangle_center_in_plan(self, triangle): x0, y0 = self.plan_origin i, j = self.hexaflexagon.get_triangle_plan_position(triangle) @@ -88,59 +101,20 @@ class HexaflexagonDiagram(object): dest_x, dest_y = self.get_triangle_center_in_plan(triangle) theta = triangle.get_angle_in_plan_relative_to_hexagon() - # The transformation from a triangle in the hexagon to the correspondent - # triangle 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 + return self.backend.calc_rotate_translate_transform(src_x, src_y, + dest_x, dest_y, theta) + + def get_triangle_backfaces_transform(self, triangle): + """Calculate the transformation matrix from a triangle in an hexagon to + the correspondent triangle in a backface. + + Return the matrix as a list of values sorted in row-major order.""" + src_x, src_y = self.get_triangle_center(triangle) + dest_x, dest_y = self.get_triangle_center_in_backfaces(triangle) + theta = triangle.get_angle_in_backface_relative_to_hexagon() + + return self.backend.calc_rotate_translate_transform(src_x, src_y, + dest_x, dest_y, theta) def draw_hexagon_template(self, hexagon): for triangle in hexagon.triangles: