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):
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)
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:
cx, cy = self.get_triangle_center(triangle)
return xoffset, yoffset
+ def get_backface_index(self):
+ # The backfaces have the triangles in the reverse rotational order
+ return 5 - self.index
+
def get_angle_in_plan(self):
"""The angle of a triangle in the hexaflexagon plan."""
return - ((self.index) % 2) * pi / 3.
# angle functions change this one can be left untouched.
return self.get_angle_in_hexagon() - self.get_angle_in_plan()
+ def get_angle_in_backface_relative_to_hexagon(self):
+
+ """"Get the angle of the triangle in the backface relative to the
+ rotation of the same triangle in the hexagon."""
+
+ backface_triangle_index = self.get_backface_index()
+ # group triangles in couples
+ group = (((backface_triangle_index + 1) % 6) // 2)
+ return pi + pi * 2 / 3 * group
+
def get_angle_in_hexagon(self):
"""Get the angle of the triangle in the hexagons.
# create some layers and groups
layers = {
"Hexagons": svg.layer(label="Hexagons"),
+ "Backfaces": svg.layer(label="Backfaces"),
"Hexaflexagon": svg.layer(label="Hexaflexagon"),
"Folding guide": svg.layer(label="Folding guide"),
"Template": svg.layer(label="Template")
ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
group.add(ref)
+ # draw the backfaces
+ group = self.groups["Backfaces"]
+ for hexagon in self.hexaflexagon.hexagons:
+ for triangle in hexagon.triangles:
+ m = self.get_triangle_backfaces_transform(triangle)
+ svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
+ m[1], m[4],
+ m[2], m[5])
+
+ triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
+
+ # Reuse the content to draw the backface
+ content_href = "#hexagon%d-content" % hexagon.index
+ ref = self.backend.svg.use(content_href)
+ ref['transform'] = svg_matrix
+ ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
+ group.add(ref)
+
def draw_triangle_template(self, triangle, cx, cy, theta):
old_active_group = self.backend.active_group
group_name = "hexagon%d-triangle%d" % (triangle.hexagon.index, triangle.index)