3 # Draw an SVG tetraflexagon which can be edited live in Inkscape.
5 # Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from diagram.svgwrite_diagram import SvgwriteDiagram
23 from flexagon.tetraflexagon_diagram import TetraflexagonDiagram
26 class SvgwriteTetraflexagonDiagram(TetraflexagonDiagram):
27 def __init__(self, *args, **kwargs):
28 super(SvgwriteTetraflexagonDiagram, self).__init__(*args, **kwargs)
30 svg = self.backend.svg
32 # create some layers and groups
34 "Squares": svg.layer(label="Squares"),
35 "Backfaces": svg.layer(label="Backfaces"),
36 "Tetraflexagon": svg.layer(label="Tetraflexagon"),
37 "Template": svg.layer(label="Template")
39 for layer in layers.values():
44 for square in self.tetraflexagon.squares:
45 name = "square%d-content" % square.index
46 layer = svg.layer(id=name, label="Square %d" % (square.index + 1))
47 self.groups[name] = layer
48 layers['Squares'].add(layer)
50 for tile in square.tiles:
51 name = "square%d-tile%d" % (square.index, tile.index)
52 group = svg.g(id=name)
53 self.groups[name] = group
54 layers['Template'].add(group)
57 for square in self.tetraflexagon.squares:
58 cx, cy = self.get_square_center(square)
60 # Draw some default content
61 old_active_group = self.backend.active_group
62 self.backend.active_group = self.groups["square%d-content" % square.index]
63 self.backend.draw_rect_from_center(cx, cy, self.square_side, self.square_side, 0,
64 fill_color=(0.5, 0.5, 0.5, 0.2))
65 self.backend.active_group = old_active_group
67 # Draw the normal template for squares
68 for square in self.tetraflexagon.squares:
69 self.draw_square_template(square)
71 # draw plan using references
72 for square in self.tetraflexagon.squares:
73 for tile in square.tiles:
74 m = self.get_tile_transform(tile)
75 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
79 # Reuse the squares tile for the tetraflexagon template
80 group = self.groups["Template"]
81 tile_href = "#square%d-tile%d" % (square.index, tile.index)
82 ref = self.backend.svg.use(tile_href)
83 ref['transform'] = svg_matrix
86 # Reuse the content to draw the final tetraflexagon
87 group = self.groups["Tetraflexagon"]
88 content_href = "#square%d-content" % square.index
89 ref = self.backend.svg.use(content_href)
90 ref['transform'] = svg_matrix
91 ref['clip-path'] = "url(%s)" % (tile_href + '-clip-path')
94 # A tri-tetraflexagon only have one visible backface, the first one.
95 group = self.groups["Backfaces"]
96 for tile in self.tetraflexagon.squares[0].tiles:
97 m = self.get_backface_tile_transform(tile)
98 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
101 tile_href = "#square0-tile%d" % tile.index
102 ref = self.backend.svg.use("#square0-content")
103 ref['transform'] = svg_matrix
104 ref['clip-path'] = "url(%s)" % (tile_href + '-clip-path')
107 def draw_tile_template(self, tile, cx, cy, theta):
108 old_active_group = self.backend.active_group
109 group_name = "square%d-tile%d" % (tile.square.index, tile.index)
110 self.backend.active_group = self.groups[group_name]
112 super(SvgwriteTetraflexagonDiagram, self).draw_tile_template(tile, cx, cy, theta)
114 # The tile outline in the active group's element is the only rectangle
115 # element, so get it and set its id so that it can be reused as
117 for element in self.backend.active_group.elements:
118 if isinstance(element, svgwrite.shapes.Rect):
119 element['id'] = group_name + "-outline"
122 clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
123 self.backend.svg.defs.add(clip_path)
124 ref = self.backend.svg.use('#%s-outline' % group_name)
127 self.backend.active_group = old_active_group
134 x_border = width / 50
135 font_size = width / 80
136 stroke_width = width / 480
138 svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
139 tetraflexagon = SvgwriteTetraflexagonDiagram(x_border, backend=svg_backend)
141 svg_backend.save_svg("inkscape-tetraflexagon-editor.svg")
144 if __name__ == "__main__":