From: Antonio Ospite Date: Wed, 27 Jun 2018 18:00:05 +0000 (+0200) Subject: diagram: share the rotate-translate calculation in the generic diagram class X-Git-Url: https://git.ao2.it/flexagon-toolkit.git/commitdiff_plain/23f35964b301e77eaa007aa75fe9f768f2761a5c diagram: share the rotate-translate calculation in the generic diagram class Move the calculation of the rotate-translate transformation matrix into the generic diagram class, so that it can be shared. --- diff --git a/src/diagram/diagram.py b/src/diagram/diagram.py index 43e3a93..ec45e5c 100755 --- a/src/diagram/diagram.py +++ b/src/diagram/diagram.py @@ -127,6 +127,66 @@ class Diagram(object): return fmod(theta, 2 * pi) / (2 * pi) @staticmethod + def calc_rotate_translate_transform(src_x, src_y, dest_x, dest_y, theta): + """Calculate the transformation matrix resulting from a rotation and + a translation. + + Return the matrix as a list of values sorted in row-major order.""" + + # A rotate-translate transformation 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 + + @staticmethod def get_regular_polygon(x, y, sides, r, theta0=0.0): """Calc the coordinates of the regular polygon. diff --git a/src/flexagon/hexaflexagon_diagram.py b/src/flexagon/hexaflexagon_diagram.py index 1a1cdff..4bb029b 100755 --- a/src/flexagon/hexaflexagon_diagram.py +++ b/src/flexagon/hexaflexagon_diagram.py @@ -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 @@ -88,59 +88,8 @@ 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 draw_hexagon_template(self, hexagon): for triangle in hexagon.triangles: diff --git a/src/flexagon/tetraflexagon_diagram.py b/src/flexagon/tetraflexagon_diagram.py index acf5706..d64ce32 100755 --- a/src/flexagon/tetraflexagon_diagram.py +++ b/src/flexagon/tetraflexagon_diagram.py @@ -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 @@ -83,59 +82,8 @@ class TetraflexagonDiagram(object): i, j = self.tetraflexagon.get_tile_plan_position(tile) theta = tile.calc_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 + 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: