3 # Draw an SVG hexaflexagon 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.hexaflexagon_diagram import HexaflexagonDiagram
 
  26 class SvgwriteHexaflexagonDiagram(HexaflexagonDiagram):
 
  27     def __init__(self, *args, **kwargs):
 
  28         super(SvgwriteHexaflexagonDiagram, self).__init__(*args, **kwargs)
 
  30         svg = self.backend.svg
 
  32         # create some layers and groups
 
  34             "Hexagons": svg.layer(label="Hexagons"),
 
  35             "Backfaces": svg.layer(label="Backfaces"),
 
  36             "Hexaflexagon": svg.layer(label="Hexaflexagon"),
 
  37             "Folding guide": svg.layer(label="Folding guide"),
 
  38             "Template": svg.layer(label="Template")
 
  40         for layer in layers.values():
 
  45         for hexagon in self.hexaflexagon.hexagons:
 
  46             name = "hexagon%d-content" % hexagon.index
 
  47             layer = svg.layer(id=name, label="Hexagon %d" % (hexagon.index + 1))
 
  48             self.groups[name] = layer
 
  49             layers['Hexagons'].add(layer)
 
  51             for triangle in hexagon.triangles:
 
  52                 name = "hexagon%d-triangle%d" % (hexagon.index, triangle.index)
 
  53                 group = svg.g(id=name)
 
  54                 self.groups[name] = group
 
  55                 layers['Template'].add(group)
 
  58         for hexagon in self.hexaflexagon.hexagons:
 
  59             cx, cy = self.get_hexagon_center(hexagon)
 
  61             # Draw some default content
 
  62             old_active_group = self.backend.active_group
 
  63             self.backend.active_group = self.groups["hexagon%d-content" % hexagon.index]
 
  64             self.backend.draw_regular_polygon(cx, cy, 6, self.hexagon_radius,
 
  66                                               fill_color=(0.5, 0.5, 0.5, 0.2))
 
  67             self.backend.active_group = old_active_group
 
  70             old_active_group = self.backend.active_group
 
  71             self.backend.active_group = self.groups["Folding guide"]
 
  73             for triangle in hexagon.triangles:
 
  74                 cx, cy = self.get_triangle_center(triangle)
 
  75                 theta = triangle.get_angle_in_hexagon()
 
  76                 self.backend.draw_regular_polygon(cx, cy, 3, self.triangle_radius, theta,
 
  77                                                   stroke_color=(0, 0, 0, 0.2),
 
  79                 polygon = self.backend.active_group.elements[-1]
 
  80                 polygon['id'] = "hexagon%d-triangle%d-folding" % (triangle.hexagon.index,
 
  83             self.backend.active_group = old_active_group
 
  85         # Draw the normal template for hexagons
 
  86         for hexagon in self.hexaflexagon.hexagons:
 
  87             self.draw_hexagon_template(hexagon)
 
  89         # draw plan using references
 
  90         for hexagon in self.hexaflexagon.hexagons:
 
  91             for triangle in hexagon.triangles:
 
  92                 m = self.get_triangle_transform(triangle)
 
  93                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
 
  97                 # Reuse the hexagons triangle for the hexaflexagon template
 
  98                 group = self.groups["Template"]
 
  99                 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
 
 100                 ref = self.backend.svg.use(triangle_href)
 
 101                 ref['transform'] = svg_matrix
 
 104                 # Reuse the folding guides
 
 105                 group = self.groups["Folding guide"]
 
 106                 folding_href = "#hexagon%d-triangle%d-folding" % (hexagon.index, triangle.index)
 
 107                 ref = self.backend.svg.use(folding_href)
 
 108                 ref['transform'] = svg_matrix
 
 111                 # Reuse the content to draw the final hexaflexagon
 
 112                 group = self.groups["Hexaflexagon"]
 
 113                 content_href = "#hexagon%d-content" % hexagon.index
 
 114                 ref = self.backend.svg.use(content_href)
 
 115                 ref['transform'] = svg_matrix
 
 116                 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
 
 120         group = self.groups["Backfaces"]
 
 121         for hexagon in self.hexaflexagon.hexagons:
 
 122             for triangle in hexagon.triangles:
 
 123                 m = self.get_triangle_backfaces_transform(triangle)
 
 124                 svg_matrix = "matrix(%f, %f, %f, %f, %f, %f)" % (m[0], m[3],
 
 128                 triangle_href = "#hexagon%d-triangle%d" % (hexagon.index, triangle.index)
 
 130                 # Reuse the content to draw the backface
 
 131                 content_href = "#hexagon%d-content" % hexagon.index
 
 132                 ref = self.backend.svg.use(content_href)
 
 133                 ref['transform'] = svg_matrix
 
 134                 ref['clip-path'] = "url(%s)" % (triangle_href + '-clip-path')
 
 137     def draw_triangle_template(self, triangle, cx, cy, theta):
 
 138         old_active_group = self.backend.active_group
 
 139         group_name = "hexagon%d-triangle%d" % (triangle.hexagon.index, triangle.index)
 
 140         self.backend.active_group = self.groups[group_name]
 
 142         super(SvgwriteHexaflexagonDiagram, self).draw_triangle_template(triangle, cx, cy, theta)
 
 144         # The triangle outline in the active group's element is the only polygon
 
 145         # element, so get it and set its id so that it can be reused as
 
 147         for element in self.backend.active_group.elements:
 
 148             if isinstance(element, svgwrite.shapes.Polygon):
 
 149                 element['id'] = group_name + "-outline"
 
 152         clip_path = self.backend.svg.clipPath(id=group_name + '-clip-path')
 
 153         self.backend.svg.defs.add(clip_path)
 
 154         ref = self.backend.svg.use('#%s-outline' % group_name)
 
 157         self.backend.active_group = old_active_group
 
 164     x_border = width / 50
 
 165     font_size = width / 80
 
 166     stroke_width = width / 480
 
 168     svg_backend = SvgwriteDiagram(width, height, font_size=font_size, stroke_width=stroke_width)
 
 169     hexaflexagon = SvgwriteHexaflexagonDiagram(x_border, backend=svg_backend)
 
 171     svg_backend.save_svg("inkscape-hexaflexagon-editor.svg")
 
 174 if __name__ == "__main__":