Manfred Moitzi avatar Manfred Moitzi committed 1d4698b

edit examples

Comments (0)

Files changed (16)

examples/ltattrie/circle_blur.py

+import svgwrite
+#
+#
+progname = 'test_circle_blur'
+
+
+def create_svg(name):
+    svg_size = 900
+    font_size = 20
+    y = 0
+    title = name + ': Test to see if feGaussianBlur filter can be used.'
+    dwg = svgwrite.Drawing(name, (svg_size, svg_size), debug=True)
+    myfilter = dwg.defs.add( dwg.filter(id="myfilter", filterUnits="userSpaceOnUse"))
+    newfilter = myfilter.feGaussianBlur(
+        in_='SourceGraphic',
+        stdDeviation =2)
+    defs_g_text = dwg.defs.add(dwg.g(id='defs_g_text'))
+    defs_g_text.add(dwg.text("SVGWRITE", insert=(0, 0), font_family="serif", font_size=font_size))
+    # background will be white.
+    dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), rx=None, ry=None, fill='white'))
+    # give the name of the example and a title.
+    y = font_size + 5
+    dwg.add(dwg.text(title, insert=(0, y),
+            font_family="serif", font_size=font_size, fill='black'))
+    y = y +50
+    circle_1 = dwg.circle(center=(100, y), r=30)
+    circle_1.fill('green', opacity=0.5).stroke('black', width=5).scale([2,2])
+    dwg.add(circle_1)
+    y = y + 30
+    circle_2 = dwg.circle(center=(100, y), r=30)
+    circle_2.fill('green', opacity=0.5).stroke('black', width=5).scale([2,2])
+    circle_2.filter('url(#myfilter)')
+    dwg.add(circle_2)
+    dwg.save()
+
+if __name__ == '__main__':
+    create_svg(progname + '.svg')
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 textwidth=99

examples/ltattrie/color_triangles_function.py

 # License: LGPL
 # Python version 2.7
 
-import math
+import math, sys
 import svgwrite
 from svgwrite import rgb
 #
 # http://www.w3.org/TR/SVG11/types.html#ColorKeywords
 # Note both grey and gray are valide but only color not colour is valid.
 #
-PROGNAME = 'color_triangles_function'
+
+PROGNAME = sys.argv[0].rstrip('.py')
 
 def create_svg(name):
     WIDTH = 900

examples/ltattrie/example_bezier.py

 # Copyright (C) 2012, L. Tattrie
 # License: LGPL
 # Python version 2.7
+import sys
 import svgwrite
 from itertools import *
-PROGNAME = 'example_bezier'
+
+PROGNAME = sys.argv[0].rstrip('.py')
 
 # http://code.activestate.com/recipes/66472-frange-a-range-function-with-float-increments/
 # but instead of parameter for float increments there is a parameter for n points.

examples/ltattrie/example_color_names.py

 # License: LGPL
 # Python version 2.7
 
+import sys
 import svgwrite
+
 #
 # http://www.w3.org/TR/SVG11/types.html#ColorKeywords
 # Note both grey and gray are valid but only color not colour is valid.
 #
-PROGNAME = 'example_color_names'
+
+
+PROGNAME = sys.argv[0].rstrip('.py')
 
 
 def create_svg(name):

examples/ltattrie/example_length_units.py

 # Copyright (C) 2012, L. Tattrie
 # License: LGPL
 # Python version 2.7
+
+import sys
 import svgwrite
-from svgwrite import cm, mm
+
 #
 # http://www.w3.org/TR/SVG/coords.html#Units
 # The supported length unit identifiers are: em, ex, px, pt, pc, cm, mm, in, and percentages.
 #
-PROGNAME = 'example_length_units'
+PROGNAME = sys.argv[0].rstrip('.py')
 
 def create_svg(name):
     SVG_SIZE = 900

examples/ltattrie/example_line_cap_join.py

 # Copyright (C) 2012, L. Tattrie
 # License: LGPL
 # Python version 2.7
+
+import sys
 import svgwrite
-PROGNAME = 'example_line_cap_join'
 
+PROGNAME = sys.argv[0].rstrip('.py')
 
 def create_svg(name):
     svg_size = 900

examples/ltattrie/example_text_font_generic_family.py

 # Copyright (C) 2012, L. Tattrie
 # License: LGPL
 # Python version 2.7
+
+import sys
 import svgwrite
-#
+
 # http://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#propdef-font-family
 # 'serif', 'sans-serif', 'cursive', 'fantasy', and 'monospace' from the CSS specification
-#
-PROGNAME = 'example_text_font_generic_family'
+
+PROGNAME = sys.argv[0].rstrip('.py')
 
 
 def create_svg(name):

examples/ltattrie/example_text_font_size.py

 # Copyright (C) 2012, L. Tattrie
 # License: LGPL
 # Python version 2.7
+
+import sys
 import svgwrite
-#
+
+PROGNAME = sys.argv[0].rstrip('.py')
+
 # http://www.w3.org/TR/2008/REC-CSS2-20080411/fonts.html#font-size-props
-#
-PROGNAME = 'example_text_font_size'
-
 
 def create_svg(name):
     svg_size = 900

examples/ltattrie/example_text_justify.py

 # License: LGPL
 # Python version 2.7
 
+import sys
 import svgwrite
 
-PROGNAME = 'example_text_justify'
+PROGNAME = sys.argv[0].rstrip('.py')
 
 def create_svg(name):
     svg_size = 900

examples/ltattrie/tenticles.py

 # Copyright (C) 2012, L. Tattrie
 # License: LGPL
 # Python version 2.7
-import math
+import math, sys
 import random
 import svgwrite
-from svgwrite import rgb
+
 # globals
-progname = 'tenticles'
+
+PROGNAME = sys.argv[0].rstrip('.py')
+
 file_log = ''
 dwg = ''
 
 
 
 if __name__ == '__main__':
-    create_svg(progname + '.svg')
+    create_svg(PROGNAME + '.svg')
 
 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 textwidth=99

examples/ltattrie/tiling_part_1.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# Author:  L. Tattrie
+# Purpose: svgwrite examples
+# Created: 2012/5/31
+# Copyright (C) 2012, L. Tattrie
+# License: LGPL
+# Python version 2.7
+
+import sys
+import svgwrite
+
+# http://www.w3.org/TR/SVG11/struct.html#UseElement
+#
+# For more information on tesselation / tiling see http://en.wikipedia.org/wiki/Wallpaper_group
+# The organization of these tilings are from the interesting book
+# Designing Testellations: The Secrets of Interlocking Patterns by Jinny Beyer.
+
+PROGNAME = sys.argv[0].rstrip('.py')
+
+
+def create_svg(name):
+    svg_size_width = 900
+    svg_size_height = 2800
+    font_size = 20
+    square_size = 30
+    title1 = name + ': Part 1 tiling with multiple def, groups, use, translate and scale.'
+    sample = ()
+    dwg = svgwrite.Drawing(name, (svg_size_width, svg_size_height), debug=True)
+
+    # clip path to prevent drawing outside of tile
+    # Having a clip path seems to increase the visibility of the lines between the tiles.
+    # A clipping path may be necessary if the shapes go outside the triangle.
+    # defs_g_kite = dwg.defs.add(dwg.g(id='defs_g_kite', clip_path='url(#clipkite)'))
+
+    clip_path = dwg.defs.add(dwg.clipPath(id='clipsq'))
+
+    # clip_path.add(dwg.circle((100, 100), 50))
+
+    clip_path.add(dwg.rect((0, 0), (square_size, square_size)))
+
+    # define the group (tile) which will be repeated.
+
+    defs_g = dwg.defs.add(dwg.g(id='defs_g', clip_path='url(#clipsq)'))
+    defs_g.add(dwg.rect((0, 0), (square_size, square_size)))
+    #defs_g.add(dwg.circle(center=(0, 5), r=6, stroke='blue', fill='green', stroke_width=1))
+    defs_g.add(dwg.ellipse(center=(5, 5), r=(5, 10), fill='green'))
+    defs_g.add(dwg.polygon([(square_size / 2.0, 0), 
+        (square_size, square_size), 
+        (0, square_size)], stroke='none', fill='navy'))
+    #defs_g.add(dwg.line(start=(0, 0), end=(square_size, square_size), stroke_width=2, stroke='black'))
+
+    # Background will be dark but not black so the background does not overwhelm the colors.
+
+    dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), rx=None, ry=None, fill='grey'))
+
+    # Give the name of the example and a title.
+
+    y = font_size + 5
+    dwg.add(dwg.text(title1, insert=(0, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size + 20
+
+    # show sample of tile
+
+    sq_created = dwg.use(defs_g, insert=(10, y), fill='yellow')
+    dwg.add(sq_created)
+    y += square_size
+    title2 = 'Translation, math name: p1'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            sq_created = dwg.use(defs_g, insert=(50 + j * square_size, y), fill='yellow')
+            dwg.add(sq_created)
+    y += square_size
+
+    # p2 (180 rotation to side)
+
+    title2 = '180 rotation to side, math name: p2'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, insert=(x, y), fill='yellow')
+
+            # rotate every second square
+
+            if j % 2:
+                sq_created.rotate(180, center=(x + square_size / 2.0, y + square_size / 2.0))
+            dwg.add(sq_created)
+    y += square_size
+
+    # p2 (180 rotation below)
+
+    title2 = '180 rotation below, math name: p2'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, insert=(x, y), fill='yellow')
+
+            # rotate every second row
+
+            if i % 2 == 1:
+                sq_created.rotate(180, center=(x + square_size / 2.0, y + square_size / 2.0))
+            dwg.add(sq_created)
+    y += square_size
+
+    # p2 (180 rotation side, below, and diagonally)
+
+    title2 = '180 rotation below, math name: p2'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, insert=(x, y), fill='yellow')
+
+            # rotate every second square
+
+            if i % 2 != j % 2:
+                sq_created.rotate(180, center=(x + square_size / 2.0, y + square_size / 2.0))
+            dwg.add(sq_created)
+    y += square_size
+
+    # p4 (90 rotation effectively rotating around the lower right corner.)
+
+    title2 = '90 degree rotation around lower right, math name: p4'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, insert=(x, y), fill='yellow')
+
+            # rotate every second square
+
+            if i % 2 == 1:
+
+                # odd row
+
+                if j % 2:
+                    sq_created.rotate(-90, center=(x + square_size / 2.0, y + square_size / 2.0))
+                else:
+                    sq_created.rotate(180, center=(x + square_size / 2.0, y + square_size / 2.0))
+            elif j % 2:
+                sq_created.rotate(90, center=(x + square_size / 2.0, y + square_size / 2.0))
+            dwg.add(sq_created)
+    y += square_size
+
+    # p4 (90 rotation effectively rotating around the upper left corner.)
+
+    title2 = '90 degree rotation around upper left, math name: p4'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, insert=(x, y), fill='yellow')
+
+            # rotate every second square
+
+            if i % 2:
+
+                # even row
+
+                if j % 2:
+                    sq_created.rotate(180, center=(x + square_size / 2.0, y + square_size / 2.0))
+                else:
+                    sq_created.rotate(-90, center=(x + square_size / 2.0, y + square_size / 2.0))
+            elif j % 2:
+                sq_created.rotate(90, center=(x + square_size / 2.0, y + square_size / 2.0))
+            dwg.add(sq_created)
+    y += square_size
+
+    # pg (moved horizontally then flipped vertically)
+    # scale(1,-1)
+
+    title2 = 'Glide: move horizontally then flip vertically, math name: pg'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+
+            # flip vertically every second square
+
+            if j % 2:
+
+                # odd column 1, 3, 5
+
+                sq_created.translate(x, y + square_size)
+                sq_created.scale(1, -1)
+            else:
+
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += square_size
+
+    # pg (moved vertically then flipped horizontally)
+
+    title2 = 'Glide: move vertically then flip horizontally, math name: pg'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+
+            # flip horizontally every second row
+
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                sq_created.translate(x + square_size, y)
+                sq_created.scale(-1, 1)
+            else:
+
+                # even row 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += square_size
+
+    # pgg (moved horizontally then flipped vertically, moved vertically then flipped horizontally)
+
+    title2 = 'Double Glide: Move vert, flip horiz. Move horiz flip vert. math name: pgg'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                if j % 2:
+
+                    # odd column 1, 3, 5
+                    # flip vertically and horizontally every second square
+
+                    sq_created.translate(x + square_size, y + square_size)
+                    sq_created.scale(-1, -1)
+                else:
+
+                    # even column 0, 2, 4
+                    # flip vertically every second square
+
+                    sq_created.translate(x + square_size, y)
+                    sq_created.scale(-1, 1)
+            elif j % 2:
+
+                # even row 0, 2, 4
+                # odd column 1, 3, 5
+                # flip vertically every second square but start with the first square
+
+                sq_created.translate(x, y + square_size)
+                sq_created.scale(1, -1)
+            else:
+
+                # even row 0, 2, 4
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    dwg.save()
+
+
+if __name__ == '__main__':
+    create_svg(PROGNAME + '.svg')
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 textwidth=99

examples/ltattrie/tiling_part_2.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import sys
+import svgwrite
+
+PROGNAME = sys.argv[0].rstrip('.py')
+
+#
+# http://www.w3.org/TR/SVG11/struct.html#UseElement
+#
+# For more information on tesselation / tiling see http://en.wikipedia.org/wiki/Wallpaper_group
+# The organization of these tilings are from the interesting book
+#   Designing Testellations: The Secrets of Interlocking Patterns by Jinny Beyer.
+
+def create_svg(name):
+    svg_size_width = 900
+    svg_size_height = 2200
+    font_size = 20
+    square_size = 30
+    title1 = name + ': Part 2 tiling with multiple def, groups, use, translate and scale.'
+    sample = ()
+    dwg = svgwrite.Drawing(name, (svg_size_width, svg_size_height), debug=True)
+
+    # Having a clip path seems to increase the visibility of the lines between the tiles.
+    # A clipping path may be necessary if the shapes go outside the triangle.
+    # clip path to prevent drawing outside of tile
+
+    clip_path = dwg.defs.add(dwg.clipPath(id='clipsq'))
+    clip_path.add(dwg.rect((0, 0), (square_size, square_size)))
+
+    # define the group (tile) which will be repeated.
+
+    defs_g = dwg.defs.add(dwg.g(id='defs_g', clip_path='url(#clipsq)'))
+    defs_g.add(dwg.rect((0, 0), (square_size, square_size)))
+    defs_g.add(dwg.circle(center=(0, 10), r=5, stroke='blue', fill='green', stroke_width=1))
+    defs_g.add(dwg.polygon([(square_size, 0), 
+        (square_size, square_size), 
+        (0, square_size)], stroke='none', fill='navy'))
+
+    # Background will be dark but not black so the background does not overwhelm the colors.
+
+    dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), fill='grey'))
+
+    # Give the name of the example and a title.
+
+    y = font_size + 5
+    dwg.add(dwg.text(title1, insert=(0, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size + 20
+
+    # Sample of tile.
+
+    y = y + 20
+    sq_created = dwg.use(defs_g, insert=(10, y), fill='yellow')
+    dwg.add(sq_created)
+    y += square_size
+
+    # pm (mirror horizontally)
+
+    title2 = 'Mirror horizontally, math name: pm'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+
+            # flip vertically every second square
+
+            if j % 2:
+
+                # odd column 1, 3, 5
+
+                sq_created.translate(x + square_size, y)
+                sq_created.scale(-1, 1)
+            else:
+
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y = y + square_size
+
+    # pm (mirror vertically)
+
+    title2 = 'Mirror vertically, math name: pm'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+
+            # flip vertically every second row
+
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                sq_created.translate(x, y + square_size)
+                sq_created.scale(1, -1)
+            else:
+
+                # even row 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += square_size
+
+    # cm (move horizontally then mirror. That pair is then moved vertically and staggered.)
+
+    title2 = 'Move horiz & mirror vert. Move vert & mirror vert, math name: cm'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                if j % 2:
+
+                    # odd row 1, 3, 5
+                    # odd column 1, 3, 5
+
+                    sq_created.translate(x, y)
+                else:
+
+                    # odd row 1, 3, 5
+                    # even column 0, 2, 4
+                    # flip vertically every second square
+
+                    sq_created.translate(x + square_size, y)
+                    sq_created.scale(-1, 1)
+            elif j % 2:
+
+                # even row 0, 2, 4
+                # odd column 1, 3, 5
+                # flip vertically every second square
+
+                sq_created.translate(x + square_size, y)
+                sq_created.scale(-1, 1)
+            else:
+
+                # even row 0, 2, 4
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += square_size
+
+    # cm (move vertically then mirror. That pair is then moved horizontally and staggered.)
+
+    title2 = 'Move horiz & mirror horiz, Move vert & mirror horiz, math name: cm'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                if j % 2:
+
+                    # odd row 1, 3, 5
+                    # odd column 1, 3, 5
+
+                    sq_created.translate(x, y)
+                else:
+
+                    # odd row 1, 3, 5
+                    # even column 0, 2, 4
+                    # flip horizontally every second square
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+            elif j % 2:
+
+                # even row 0, 2, 4
+                # odd column 1, 3, 5
+                # flip horizontally every second square
+
+                sq_created.translate(x, y + square_size)
+                sq_created.scale(1, -1)
+            else:
+
+                # even row 0, 2, 4
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += square_size
+
+    # pmg (moved horizontally flip vertically. That pair is then moved vertically, mirrored and
+    # staggered.)
+
+    title2 = 'Move horiz mirror horiz. Pair is glided horiz and mirrored horiz, math name: pmg'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                if j % 2:
+
+                    # odd row 1, 3, 5
+                    # odd column 1, 3, 5
+                    # flip horizontally every second square
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+                else:
+
+                    # odd row 1, 3, 5
+                    # even column 0, 2, 4
+                    # flip vertically and horizontally every second square
+
+                    sq_created.translate(x + square_size, y + square_size)
+                    sq_created.scale(-1, -1)
+            elif j % 2:
+
+                # even row 0, 2, 4
+                # odd column 1, 3, 5
+                # flip vertically every second square
+
+                sq_created.translate(x + square_size, y)
+                sq_created.scale(-1, 1)
+            else:
+
+                # even row 0, 2, 4
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += square_size
+
+    # pmg (moved vertically flip horizontally. That pair is then moved horizontally, mirrored and
+    # staggered.)
+
+    title2 = 'Move vert mirror vert. Pair is glided vert and mirrored horiz, math name: pmg'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                if j % 2:
+
+                    # odd row 1, 3, 5
+                    # odd column 1, 3, 5
+                    # flip vertically every second square
+
+                    sq_created.translate(x + square_size, y)
+                    sq_created.scale(-1, 1)
+                else:
+
+                    # odd row 1, 3, 5
+                    # even column 0, 2, 4
+                    # flip horizontally every second square
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+            elif j % 2:
+
+                # even row 0, 2, 4
+                # odd column 1, 3, 5
+                # flip vertically and horizontally every second square
+
+                sq_created.translate(x + square_size, y + square_size)
+                sq_created.scale(-1, -1)
+            else:
+
+                # even row 0, 2, 4
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += square_size
+
+    # pmm (as if mirrors were placed on all sides of square and diagonally.)
+
+    title2 = 'Mirror all sides, math name: pm'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # odd row 1, 3, 5
+
+                if j % 2:
+
+                    # odd row 1, 3, 5
+                    # odd column 1, 3, 5
+                    # flip vertically and horizontally every second square
+
+                    sq_created.translate(x + square_size, y + square_size)
+                    sq_created.scale(-1, -1)
+                else:
+
+                    # odd row 1, 3, 5
+                    # even column 0, 2, 4
+                    # flip horizontally every second square
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+            elif j % 2:
+
+                # even row 0, 2, 4
+                # odd column 1, 3, 5
+                # flip vertically every second square
+
+                sq_created.translate(x + square_size, y)
+                sq_created.scale(-1, 1)
+            else:
+
+                # even row 0, 2, 4
+                # even column 0, 2, 4
+
+                sq_created.translate(x, y)
+            dwg.add(sq_created)
+
+    # All items have been added so save the svg to a the file.
+
+    dwg.save()
+
+
+if __name__ == '__main__':
+    create_svg(PROGNAME + '.svg')
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 textwidth=99
+

examples/ltattrie/tiling_part_3.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import sys
+import svgwrite
+
+
+# http://www.w3.org/TR/SVG11/struct.html#UseElement
+#
+# For more information on tesselation / tiling see http://en.wikipedia.org/wiki/Wallpaper_group
+# The organization of these tilings are from the interesting book
+#   Designing Testellations: The Secrets of Interlocking Patterns by Jinny Beyer.
+
+PROGNAME = sys.argv[0].rstrip('.py')
+
+
+def create_svg(name):
+    svg_size_width = 900
+    svg_size_height = 2000
+    font_size = 20
+    square_size = 30
+    title1 = name + ': Part 3 tiling with multiple def, groups, use, translate and scale.'
+    sample = ()
+    dwg = svgwrite.Drawing(name, (svg_size_width, svg_size_height), debug=True)
+
+    # clip path to prevent drawing outside of tile
+
+    clip_path = dwg.defs.add(dwg.clipPath(id='clipsq'))
+
+    # clip_path.add(dwg.circle((100, 100), 50))
+
+    clip_path.add(dwg.rect((0, 0), (square_size, square_size)))
+
+    # define the group (tile) which will be repeated.
+
+    defs_g = dwg.defs.add(dwg.g(id='defs_g', clip_path='url(#clipsq)'))
+
+    # defs_g.add(dwg.rect((0,0), (square_size, square_size)))
+
+    defs_g.add(dwg.rect((0, 0), (square_size, square_size)))
+    defs_g.add(dwg.circle(center=(0, 10), r=5, stroke='blue', fill='green', stroke_width=1))
+    defs_g.add(dwg.polygon([(square_size, 0), 
+        (square_size, square_size), 
+        (0, square_size)], stroke='none', fill='navy'))
+
+    # Background will be dark but not black so the background does not overwhelm the colors.
+
+    dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), rx=None, ry=None, fill='grey'))
+
+    # Give the name of the example and a title.
+
+    y = font_size + 5
+    dwg.add(dwg.text(title1, insert=(0, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+
+    #
+    # Sample of tile.
+
+    y = y + 20
+    sq_created = dwg.use(defs_g, insert=(10, y), fill='yellow')
+    dwg.add(sq_created)
+    y += square_size
+
+    # cmm variation 1 (as if mirrors were placed on all sides of square and diagonally.)
+    #     similar to pmm but the group of four tiles will be mirrored, staggered, or centered.
+
+    title2 = 'Mirror all sides, then all are moved, variation 1, math name: cmm'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # row 1, 3, 5
+
+                if j % 4 == 0:
+
+                    # column 0, 4, 8
+                    # flip horizontally
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+                elif j % 4 == 1:
+
+                    # column 1, 5, 9
+                    # flip vertically and horizontally
+
+                    sq_created.translate(x + square_size, y + square_size)
+                    sq_created.scale(-1, -1)
+                elif j % 4 == 2:
+
+                    # column 2, 6, 10
+
+                    sq_created.translate(x, y)
+                else:
+
+                    # column 3, 7, 11
+                    # flip vertically
+
+                    sq_created.translate(x + square_size, y)
+                    sq_created.scale(-1, 1)
+            else:
+
+                # row 0, 2, 4
+
+                if j % 4 == 0:
+
+                    # column 0, 4, 8
+
+                    sq_created.translate(x, y)
+                elif j % 4 == 1:
+
+                    # column 1, 5, 9
+                    # flip vertically
+
+                    sq_created.translate(x + square_size, y)
+                    sq_created.scale(-1, 1)
+                elif j % 4 == 2:
+
+                    # column 2, 6, 10
+                    # flip horizontally
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+                else:
+
+                    # column 3, 7, 11
+                    # flip vertically and horizontally
+
+                    sq_created.translate(x + square_size, y + square_size)
+                    sq_created.scale(-1, -1)
+            dwg.add(sq_created)
+    y += square_size
+
+    # cmm variation 2 (as if mirrors were placed on all sides of square and diagonally.)
+    #     similar to pmm but the group of four tiles will be mirrored, staggered, or centered.
+
+    title2 = 'Mirror all sides, then all are moved, variation 2, math name: cmm'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    for i in range(8):
+        y += square_size
+        for j in range(16):
+            x = 50 + j * square_size
+            sq_created = dwg.use(defs_g, fill='yellow')
+            if i % 2:
+
+                # row 1, 3, 5
+
+                if j % 4 == 0:
+
+                    # column 0, 4, 8
+                    # flip horizontally
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+                elif j % 4 == 1:
+
+                    # column 1, 5, 9
+
+                    sq_created.translate(x, y)
+                elif j % 4 == 2:
+
+                    # column 2, 6, 10
+                    # flip vertically and horizontally
+
+                    sq_created.translate(x + square_size, y + square_size)
+                    sq_created.scale(-1, -1)
+                else:
+
+                    # column 3, 7, 11
+                    # flip horizontally
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+            else:
+
+                # row 0, 2, 4
+
+                if j % 4 == 0:
+
+                    # column 0, 4, 8
+                    # flip vertically and horizontally
+
+                    sq_created.translate(x + square_size, y + square_size)
+                    sq_created.scale(-1, -1)
+                elif j % 4 == 1:
+
+                    # column 1, 5, 9
+                    # flip horizontally
+
+                    sq_created.translate(x, y + square_size)
+                    sq_created.scale(1, -1)
+                elif j % 4 == 2:
+
+                    # column 2, 6, 10
+                    # flip vertically
+
+                    sq_created.translate(x + square_size, y)
+                    sq_created.scale(-1, 1)
+                else:
+
+                    # column 3, 7, 11
+
+                    sq_created.translate(x, y)
+            dwg.add(sq_created)
+
+    y += square_size
+
+    # Instead of writing the p4g four times this code would have been better if it had been
+    # put into a function and called indicating which corner is to be used for rotation.
+    # p4g variation 1 (mirrored pinwheel which depending on which corner is the rotation center.)
+    # lower right is the rotation corner
+
+    defs_g_p4g_v1_size = square_size * 2
+    defs_g_lr_90 = dwg.defs.add(dwg.g(id='defs_g_lr_90'))
+    defs_g_lr_90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_lr_90.rotate(90, center=(square_size, square_size))
+    defs_g_lr_m90 = dwg.defs.add(dwg.g(id='defs_g_lr_m90'))
+    defs_g_lr_m90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_lr_m90.rotate(-90, center=(square_size, square_size))
+    defs_g_lr_180 = dwg.defs.add(dwg.g(id='defs_g_lr_180'))
+    defs_g_lr_180.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_lr_180.rotate(180, center=(square_size, square_size))
+    defs_g_p4g_v1 = dwg.defs.add(dwg.g(id='defs_g_p4g_v1'))
+    defs_g_p4g_v1.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_p4g_v1.add(dwg.use(defs_g_lr_90, insert=(0, 0)))
+    defs_g_p4g_v1.add(dwg.use(defs_g_lr_m90, insert=(0, 0)))
+    defs_g_p4g_v1.add(dwg.use(defs_g_lr_180, insert=(0, 0)))
+    title2 = 'Mirrored pinwheel, rotate lower right, math name: p4g'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+
+    # y = y + font_size
+
+    for i in range(4):
+        y += defs_g_p4g_v1_size
+        for j in range(8):
+            x = 50 + j * defs_g_p4g_v1_size
+            sq_created = dwg.use(defs_g_p4g_v1, fill='yellow')
+            if i % 2:
+
+                # row 1, 3, 5
+
+                if j % 2:
+
+                    # column 1, 3, 5
+
+                    sq_created.translate(x, y)
+                else:
+
+                    # column 0, 2, 4
+                    # flip horizontally
+
+                    sq_created.translate(x, y + defs_g_p4g_v1_size)
+                    sq_created.scale(1, -1)
+            else:
+
+                # row 0, 2, 4
+
+                if j % 2:
+
+                    # column 1, 3, 5
+                    # flip vertically
+
+                    sq_created.translate(x + defs_g_p4g_v1_size, y)
+                    sq_created.scale(-1, 1)
+                else:
+
+                    # column 0, 2, 4
+
+                    sq_created.translate(x, y)
+            dwg.add(sq_created)
+
+    y += defs_g_p4g_v1_size
+
+    # p4g variation 2 (mirrored pinwheel which depending on which corner is the rotation center.)
+    # upper right is the rotation corner
+
+    title2 = 'Mirrored pinwheel, rotate upper right, math name: p4g'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+
+    # y = y + font_size
+
+    defs_g_p4g_v2_size = square_size * 2
+    defs_g_ur_90 = dwg.defs.add(dwg.g(id='defs_g_ur_90'))
+    defs_g_ur_90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ur_90.rotate(90, center=(square_size, 0))
+    defs_g_ur_m90 = dwg.defs.add(dwg.g(id='defs_g_ur_m90'))
+    defs_g_ur_m90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ur_m90.rotate(-90, center=(square_size, 0))
+    defs_g_ur_180 = dwg.defs.add(dwg.g(id='defs_g_ur_180'))
+    defs_g_ur_180.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ur_180.rotate(180, center=(square_size, 0))
+    defs_g_p4g_v2 = dwg.defs.add(dwg.g(id='defs_g_p4g_v2'))
+    defs_g_p4g_v2.add(dwg.use(defs_g, insert=(0, square_size)))
+    defs_g_p4g_v2.add(dwg.use(defs_g_ur_90, insert=(0, square_size)))
+    defs_g_p4g_v2.add(dwg.use(defs_g_ur_m90, insert=(0, square_size)))
+    defs_g_p4g_v2.add(dwg.use(defs_g_ur_180, insert=(0, square_size)))
+    for i in range(4):
+        y += defs_g_p4g_v2_size
+        for j in range(8):
+            x = 50 + j * defs_g_p4g_v2_size
+            sq_created = dwg.use(defs_g_p4g_v2, fill='yellow')
+            if i % 2:
+
+                # row 1, 3, 5
+
+                if j % 2:
+
+                    # column 1, 3, 5
+
+                    sq_created.translate(x, y)
+                else:
+
+                    # column 0, 2, 4
+                    # flip horizontally
+
+                    sq_created.translate(x, y + defs_g_p4g_v2_size)
+                    sq_created.scale(1, -1)
+            else:
+
+                # row 0, 2, 4
+
+                if j % 2:
+
+                    # column 1, 3, 5
+                    # flip vertically
+
+                    sq_created.translate(x + defs_g_p4g_v2_size, y)
+                    sq_created.scale(-1, 1)
+                else:
+
+                    # column 0, 2, 4
+
+                    sq_created.translate(x, y)
+            dwg.add(sq_created)
+
+    y += defs_g_p4g_v2_size
+
+    # p4g variation 3 (mirrored pinwheel which depending on which corner is the rotation center.)
+    # upper left is the rotation corner
+
+    defs_g_p4g_v3_size = square_size * 2
+    defs_g_ul_90 = dwg.defs.add(dwg.g(id='defs_g_ul_90'))
+    defs_g_ul_90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ul_90.rotate(90, center=(0, 0))
+    defs_g_ul_m90 = dwg.defs.add(dwg.g(id='defs_g_ul_m90'))
+    defs_g_ul_m90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ul_m90.rotate(-90, center=(0, 0))
+    defs_g_ul_180 = dwg.defs.add(dwg.g(id='defs_g_ul_180'))
+    defs_g_ul_180.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ul_180.rotate(180, center=(0, 0))
+    defs_g_p4g_v3 = dwg.defs.add(dwg.g(id='defs_g_p4g_v3'))
+    defs_g_p4g_v3.add(dwg.use(defs_g, insert=(square_size, square_size)))
+    defs_g_p4g_v3.add(dwg.use(defs_g_ul_90, insert=(square_size, square_size)))
+    defs_g_p4g_v3.add(dwg.use(defs_g_ul_m90, insert=(square_size, square_size)))
+    defs_g_p4g_v3.add(dwg.use(defs_g_ul_180, insert=(square_size, square_size)))
+    title2 = 'Mirrored pinwheel, rotate upper left, math name: p4g'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+
+    # y = y + font_size
+
+    for i in range(4):
+        y += defs_g_p4g_v3_size
+        for j in range(8):
+            x = 50 + j * defs_g_p4g_v3_size
+            sq_created = dwg.use(defs_g_p4g_v3, fill='yellow')
+            if i % 2:
+
+                # row 1, 3, 5
+
+                if j % 2:
+
+                    # column 1, 3, 5
+
+                    sq_created.translate(x, y)
+                else:
+
+                    # column 0, 2, 4
+                    # flip horizontally
+
+                    sq_created.translate(x, y + defs_g_p4g_v3_size)
+                    sq_created.scale(1, -1)
+            else:
+
+                # row 0, 2, 4
+
+                if j % 2:
+
+                    # column 1, 3, 5
+                    # flip vertically
+
+                    sq_created.translate(x + defs_g_p4g_v3_size, y)
+                    sq_created.scale(-1, 1)
+                else:
+
+                    # column 0, 2, 4
+
+                    sq_created.translate(x, y)
+            dwg.add(sq_created)
+
+    y += defs_g_p4g_v3_size
+
+    # p4g variation 4 (mirrored pinwheel which depending on which corner is the rotation center.)
+    # lower left is the rotation corner
+
+    defs_g_p4g_v4_size = square_size * 2
+    defs_g_ll_90 = dwg.defs.add(dwg.g(id='defs_g_ll_90'))
+    defs_g_ll_90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ll_90.rotate(90, center=(0, square_size))
+    defs_g_ll_m90 = dwg.defs.add(dwg.g(id='defs_g_ll_m90'))
+    defs_g_ll_m90.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ll_m90.rotate(-90, center=(0, square_size))
+    defs_g_ll_180 = dwg.defs.add(dwg.g(id='defs_g_ll_180'))
+    defs_g_ll_180.add(dwg.use(defs_g, insert=(0, 0)))
+    defs_g_ll_180.rotate(180, center=(0, square_size))
+    defs_g_p4g_v4 = dwg.defs.add(dwg.g(id='defs_g_p4g_v4'))
+    defs_g_p4g_v4.add(dwg.use(defs_g, insert=(square_size, 0)))
+    defs_g_p4g_v4.add(dwg.use(defs_g_ll_90, insert=(square_size, 0)))
+    defs_g_p4g_v4.add(dwg.use(defs_g_ll_m90, insert=(square_size, 0)))
+    defs_g_p4g_v4.add(dwg.use(defs_g_ll_180, insert=(square_size, 0)))
+    title2 = 'Mirrored pinwheel, rotate lower left, math name: p4g'
+    dwg.add(dwg.text(title2, insert=(50, y + square_size), font_family='serif', font_size=font_size, fill='white'))
+
+    # y = y + font_size
+
+    for i in range(4):
+        y += defs_g_p4g_v4_size
+        for j in range(8):
+            x = 50 + j * defs_g_p4g_v4_size
+            sq_created = dwg.use(defs_g_p4g_v4, fill='yellow')
+            if i % 2:
+
+                # row 1, 3, 5
+
+                if j % 2:
+
+                    # column 1, 3, 5
+
+                    sq_created.translate(x, y)
+                else:
+
+                    # column 0, 2, 4
+                    # flip horizontally
+
+                    sq_created.translate(x, y + defs_g_p4g_v4_size)
+                    sq_created.scale(1, -1)
+            else:
+
+                # row 0, 2, 4
+
+                if j % 2:
+
+                    # column 1, 3, 5
+                    # flip vertically
+
+                    sq_created.translate(x + defs_g_p4g_v4_size, y)
+                    sq_created.scale(-1, 1)
+                else:
+
+                    # column 0, 2, 4
+
+                    sq_created.translate(x, y)
+            dwg.add(sq_created)
+
+    # All items have been added so save the svg to a the file.
+
+    dwg.save()
+
+
+if __name__ == '__main__':
+    create_svg(PROGNAME + '.svg')
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 textwidth=99
+

examples/ltattrie/tiling_part_4.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import math, sys
+import svgwrite
+
+# http://www.w3.org/TR/SVG11/struct.html#UseElement
+#
+# For more information on tesselation / tiling see http://en.wikipedia.org/wiki/Wallpaper_group
+# The organization of these tilings are from the interesting book
+#   Designing Testellations: The Secrets of Interlocking Patterns by Jinny Beyer.
+
+PROGNAME = sys.argv[0].rstrip('.py')
+
+def create_svg(name):
+    svg_size_width = 900
+    svg_size_height = 2200
+    font_size = 20
+    triangle_size = 30
+    square_size = 30
+    title1 = name + ': Part 4 tiling with multiple def, groups, use, translate and scale.'
+    sqrt3 = math.sqrt(3)  # do this calc once instead of repeating the calc many times.
+
+    # sqrt2 = math.sqrt(2) # do this calc once instead of repeating the calc many times. 1.41421356237
+
+    dwg = svgwrite.Drawing(name, (svg_size_width, svg_size_height), debug=True)
+
+    # 45 degrees, 90 degrees, 45 degrees Right angle triangle
+
+    triright = dwg.defs.add(dwg.polygon([(0, 0), (triangle_size, 0), (triangle_size, triangle_size)], id='triangle', stroke='none'))
+
+    # clip path to prevent drawing outside of tile
+
+    clip_path_triright = dwg.defs.add(dwg.clipPath(id='cliptriright'))
+    clip_path_triright.add(dwg.polygon([(0, 0), (triangle_size, 0), (triangle_size, triangle_size)]))
+
+    # define the group (tile) which will be repeated.
+    # Having a clip path seems to increase the visibility of the lines which can occur between tiles and patterns.
+
+    defs_g_triright = dwg.defs.add(dwg.g(id='defs_g_triright', clip_path='url(#cliptriright)'))
+    defs_g_triright.add(dwg.polygon([(0, 0), (triangle_size, 0), (triangle_size, triangle_size)], stroke='none'))
+    defs_g_triright.add(dwg.polygon([(3 * triangle_size / 4.0, 0), 
+        (triangle_size, 0), 
+        (triangle_size, triangle_size)], stroke='none', fill='navy'))
+    defs_g_triright.add(dwg.circle(center=(triangle_size / 2.0, triangle_size / 2.0), r=5, stroke='blue', fill='green', stroke_width=1))
+
+    # define the mirror of  defs_g_triright
+
+    defs_g_tr_m = dwg.defs.add(dwg.g(id='defs_g_tr_m'))
+    defs_g_tr_m.add(dwg.use(defs_g_triright, insert=(0, 0)))
+    defs_g_tr_m.rotate(90, center=(triangle_size / 2.0, triangle_size / 2.0))
+    defs_g_tr_m.translate(0, triangle_size)
+    defs_g_tr_m.scale(1, -1)
+
+    # defs_g_tr_m.rotate(-90, center=(triangle_size, triangle_size))
+    # defs_g_tr_m.rotate(-90, center=(0, 0))
+    #                sq_created.translate(x, (y + defs_g_p4g_v1_size))
+    # create the cell by using one triangle and one mirrored triangle
+
+    defs_g_p4m_cell_size = triangle_size
+    defs_g_p4m_cell = dwg.defs.add(dwg.g(id='defs_g_p4m_cell'))
+
+    # defs_g_p4m_cell.add(dwg.use(defs_g_triright, insert=(0, 0), fill='aqua'))
+
+    defs_g_p4m_cell.add(dwg.use(defs_g_triright, insert=(0, 0)))
+    defs_g_p4m_cell.add(dwg.use(defs_g_tr_m, insert=(0, 0)))
+
+    # Create rotations of the cell.
+
+    defs_g_p4m_cell_lr_90 = dwg.defs.add(dwg.g(id='defs_g_p4m_cell_lr_90'))
+    defs_g_p4m_cell_lr_90.add(dwg.use(defs_g_p4m_cell, insert=(0, 0)))
+    defs_g_p4m_cell_lr_90.rotate(90, center=(square_size, square_size))
+    defs_g_p4m_cell_lr_m90 = dwg.defs.add(dwg.g(id='defs_g_p4m_cell_lr_m90'))
+    defs_g_p4m_cell_lr_m90.add(dwg.use(defs_g_p4m_cell, insert=(0, 0)))
+    defs_g_p4m_cell_lr_m90.rotate(-90, center=(square_size, square_size))
+    defs_g_p4m_cell_lr_180 = dwg.defs.add(dwg.g(id='defs_g_p4m_cell_lr_180'))
+    defs_g_p4m_cell_lr_180.add(dwg.use(defs_g_p4m_cell, insert=(0, 0)))
+    defs_g_p4m_cell_lr_180.rotate(180, center=(square_size, square_size))
+
+    # Now use the cell and three rotated cells to create the pattern.
+
+    defs_g_p4m_pattern_size = 2 * defs_g_p4m_cell_size
+    defs_g_p4m_pattern = dwg.defs.add(dwg.g(id='defs_g_p4m_pattern'))
+    defs_g_p4m_pattern.add(dwg.use(defs_g_p4m_cell, insert=(0, 0)))
+    defs_g_p4m_pattern.add(dwg.use(defs_g_p4m_cell_lr_90, insert=(0, 0)))
+    defs_g_p4m_pattern.add(dwg.use(defs_g_p4m_cell_lr_m90, insert=(0, 0)))
+    defs_g_p4m_pattern.add(dwg.use(defs_g_p4m_cell_lr_180, insert=(0, 0)))
+
+    # ####################
+    # p3 - rombus - For the purpose of these tessellations a rombus is 4 * 30-60-90 triangles.
+    # 30, 60, 90 angle triangle
+    #   The length of the sides are 1:sqrt(3):2   2 is the hypotenuse
+    # invsqrt2 = 1/sqrt2
+    # invsqrt2_2 = invsqrt2 * invsqrt2 = 1/2 = .5 by definition
+    # sin and cos(45 degrees) is 1/sqrt2 = 0.707106781187
+    # cos(30 degrees) is sqrt3/2
+    # sin(30 degrees) is 1/2
+
+    defs_g_rombus_size_x = square_size
+    defs_g_rombus_scale = defs_g_rombus_size_x / (sqrt3 * 2.0)
+    defs_g_rombus_size_y = 2 * defs_g_rombus_scale
+
+    # Having a clip path seems to increase the visibility of the lines between the tiles.
+    # A clipping path may be necessary if the shapes go outside the triangle.
+    # defs_g_rombus = dwg.defs.add(dwg.g(id='defs_g_rombus', clip_path='url(#cliprombus)'))
+
+    defs_g_rombus = dwg.defs.add(dwg.g(id='defs_g_rombus'))
+    defs_g_rombus.add(dwg.polygon([(0, defs_g_rombus_size_y / 2.0), (defs_g_rombus_size_x / 2.0, 0), (0, -defs_g_rombus_size_y / 2),
+                      (-defs_g_rombus_size_x / 2.0, 0)], stroke='none'))
+    defs_g_rombus.add(dwg.polygon([(0, -defs_g_rombus_size_y / 2.0), (defs_g_rombus_size_x / 4.0, defs_g_rombus_size_y / 4.0), (0,
+                      defs_g_rombus_size_y / 2.0)], stroke='none', fill='darkolivegreen'))
+
+    #     (0, defs_g_rombus_size_y/2.0) ], stroke='none', fill='aqua'))
+    #     (0, defs_g_rombus_size_y/2.0) ], stroke='none'))
+    #     (defs_g_rombus_size_x/4.0, defs_g_rombus_size_y/4.0),
+    #     (defs_g_rombus_size_x*invsqrt2_2/2.0, defs_g_rombus_size_y*invsqrt2_2/2.0),
+
+    defs_g_rombus.add(dwg.polygon([(0, -defs_g_rombus_size_y / 2.0), (-defs_g_rombus_size_x / 2.0, 0), (0, 0)], stroke='none', fill='palegreen'))
+
+    # Create rotations of the rombus.
+
+    defs_g_rombus_120 = dwg.defs.add(dwg.g(id='defs_g_rombus_120'))
+    defs_g_rombus_120.add(dwg.use(defs_g_rombus, insert=(0, 0)))
+    defs_g_rombus_120.rotate(120, center=(0, 0))
+    defs_g_rombus_m120 = dwg.defs.add(dwg.g(id='defs_g_rombus_m120'))
+    defs_g_rombus_m120.add(dwg.use(defs_g_rombus, insert=(0, 0)))
+    defs_g_rombus_m120.rotate(-120, center=(0, 0))
+
+    # Create the pattern by using the cell and two rotated cells
+
+    defs_g_rombus_pattern_size_x = defs_g_rombus_size_x
+    defs_g_rombus_pattern_size_y = 2.0 * defs_g_rombus_size_y
+    defs_g_rombus_pattern = dwg.defs.add(dwg.g(id='defs_g_rombus_pattern'))
+    defs_g_rombus_pattern.add(dwg.use(defs_g_rombus, insert=(defs_g_rombus_size_x / 2.0, 3 * defs_g_rombus_size_y / 2.0)))
+    defs_g_rombus_pattern.add(dwg.use(defs_g_rombus_120, insert=(defs_g_rombus_size_x / 4.0, 3 * defs_g_rombus_size_y / 4.0)))
+    defs_g_rombus_pattern.add(dwg.use(defs_g_rombus_m120, insert=(.75 * defs_g_rombus_size_x, 3 * defs_g_rombus_size_y / 4.0)))
+
+    # ####################
+    # p6 - Equilateral triangle
+    # All three sides are the same length, all three angles are 60 degrees.
+    # The height of the triangle h = sqrt(3)/2.0 * length of a side
+    # The centre of the triangle is  sqrt(3)/6.0 * length of a side
+
+    defs_g_trieq_size_x = square_size
+    defs_g_trieq_size_y = defs_g_trieq_size_x * sqrt3 / 2.0
+    defs_g_trieq_centre = sqrt3 / 6.0 * defs_g_trieq_size_x
+
+    # width of equilateral triangle at the centre
+
+    defs_g_trieq_centre_size_x = defs_g_trieq_size_x - defs_g_trieq_size_x * defs_g_trieq_centre / defs_g_trieq_size_y
+
+    # defs_g_trieq = dwg.defs.add(dwg.g(id='defs_g_trieq', clip_path='url(#cliptrieq)'))
+
+    defs_g_trieq = dwg.defs.add(dwg.g(id='defs_g_trieq'))
+    defs_g_trieq.add(dwg.polygon([(0, -defs_g_trieq_size_y + defs_g_trieq_centre), (defs_g_trieq_size_x / 2.0, defs_g_trieq_centre),
+                     (-defs_g_trieq_size_x / 2.0, defs_g_trieq_centre)], stroke='none'))
+    defs_g_trieq.add(dwg.polygon([(-defs_g_trieq_size_x / 2.0, defs_g_trieq_centre), (-defs_g_trieq_centre_size_x / 2.0, 0),
+                     (defs_g_trieq_centre_size_x / 2.0, 0), (0, defs_g_trieq_centre)], stroke='none', fill='yellow'))
+
+    # Create rotations of the equilateral triangle.
+
+    defs_g_trieq_60 = dwg.defs.add(dwg.g(id='defs_g_trieq_60'))
+    defs_g_trieq_60.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_60.rotate(60, center=(defs_g_trieq_size_x / 2.0, defs_g_trieq_centre))
+    defs_g_trieq_120 = dwg.defs.add(dwg.g(id='defs_g_trieq_120'))
+    defs_g_trieq_120.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_120.rotate(120, center=(defs_g_trieq_size_x / 2.0, defs_g_trieq_centre))
+    defs_g_trieq_180 = dwg.defs.add(dwg.g(id='defs_g_trieq_180'))
+    defs_g_trieq_180.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_180.rotate(180, center=(defs_g_trieq_size_x / 2.0, defs_g_trieq_centre))
+    defs_g_trieq_m60 = dwg.defs.add(dwg.g(id='defs_g_trieq_m60'))
+    defs_g_trieq_m60.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_m60.rotate(-60, center=(defs_g_trieq_size_x / 2.0, defs_g_trieq_centre))
+    defs_g_trieq_m120 = dwg.defs.add(dwg.g(id='defs_g_trieq_m120'))
+    defs_g_trieq_m120.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_m120.rotate(-120, center=(defs_g_trieq_size_x / 2.0, defs_g_trieq_centre))
+
+    # Now use the cell and five rotated cells to create the pattern.
+
+    defs_g_trieq_pattern_size_x = 2 * defs_g_trieq_size_x
+    defs_g_trieq_pattern_size_y = 2 * defs_g_trieq_size_y
+    defs_g_trieq_pattern = dwg.defs.add(dwg.g(id='defs_g_trieq_pattern'))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_60, insert=(0, 0)))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_120, insert=(0, 0)))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_180, insert=(0, 0)))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_m60, insert=(0, 0)))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_m120, insert=(0, 0)))
+
+    # Create centered rotations of the equilateral triangle.
+
+    defs_g_trieq_c60 = dwg.defs.add(dwg.g(id='defs_g_trieq_c60'))
+    defs_g_trieq_c60.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_c60.rotate(60, center=(0, 0))
+    defs_g_trieq_c120 = dwg.defs.add(dwg.g(id='defs_g_trieq_c120'))
+    defs_g_trieq_c120.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_c120.rotate(120, center=(0, 0))
+    defs_g_trieq_c180 = dwg.defs.add(dwg.g(id='defs_g_trieq_c180'))
+    defs_g_trieq_c180.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_c180.rotate(180, center=(0, 0))
+    defs_g_trieq_cm60 = dwg.defs.add(dwg.g(id='defs_g_trieq_cm60'))
+    defs_g_trieq_cm60.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_cm60.rotate(-60, center=(0, 0))
+    defs_g_trieq_cm120 = dwg.defs.add(dwg.g(id='defs_g_trieq_cm120'))
+    defs_g_trieq_cm120.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_cm120.rotate(-120, center=(0, 0))
+
+    # Now use the cell and five rotated cells to create the pattern variation 2.
+    # Variation 2 is the equilateral triangle rotated around a the upper corner.
+
+    defs_g_trieq_pattern_v2_size_x = 2 * defs_g_trieq_size_x
+    defs_g_trieq_pattern_v2_size_y = 2 * defs_g_trieq_size_y
+    defs_g_trieq_pattern_v2 = dwg.defs.add(dwg.g(id='defs_g_trieq_pattern_v2'))
+    defs_g_trieq_pattern_v2.add(dwg.use(defs_g_trieq, insert=(0, defs_g_trieq_size_y - defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v2.add(dwg.use(defs_g_trieq_c60, insert=(-defs_g_trieq_size_x / 2.0, defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v2.add(dwg.use(defs_g_trieq_c120, insert=(-defs_g_trieq_size_x / 2.0, -defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v2.add(dwg.use(defs_g_trieq_c180, insert=(0, defs_g_trieq_centre - defs_g_trieq_size_y)))
+    defs_g_trieq_pattern_v2.add(dwg.use(defs_g_trieq_cm60, insert=(defs_g_trieq_size_x / 2.0, defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v2.add(dwg.use(defs_g_trieq_cm120, insert=(defs_g_trieq_size_x / 2.0, -defs_g_trieq_centre)))
+
+    # Now use the cell and five rotated cells to create the pattern variation 3.
+    # Variation 3 is the equilateral triangle rotated around a the lower left corner.
+
+    defs_g_trieq_pattern_v3_size_x = 2 * defs_g_trieq_size_x
+    defs_g_trieq_pattern_v3_size_y = 2 * defs_g_trieq_size_y
+    defs_g_trieq_pattern_v3 = dwg.defs.add(dwg.g(id='defs_g_trieq_pattern_v3'))
+    defs_g_trieq_pattern_v3.add(dwg.use(defs_g_trieq, insert=(defs_g_trieq_size_x / 2.0, -defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v3.add(dwg.use(defs_g_trieq_c60, insert=(defs_g_trieq_size_x / 2.0, defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v3.add(dwg.use(defs_g_trieq_c120, insert=(0, defs_g_trieq_size_y - defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v3.add(dwg.use(defs_g_trieq_c180, insert=(-defs_g_trieq_size_x / 2.0, defs_g_trieq_centre)))
+    defs_g_trieq_pattern_v3.add(dwg.use(defs_g_trieq_cm60, insert=(0, defs_g_trieq_centre - defs_g_trieq_size_y)))
+    defs_g_trieq_pattern_v3.add(dwg.use(defs_g_trieq_cm120, insert=(-defs_g_trieq_size_x / 2.0, -defs_g_trieq_centre)))
+
+    # ########################
+    # Background will be dark but not black so the background does not overwhelm the colors.
+
+    dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), rx=None, ry=None, fill='grey'))
+
+    # Give the name of the example and a title.
+
+    y = font_size + 5
+    dwg.add(dwg.text(title1, insert=(0, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+
+    #
+    # p4m 45 45 90 triangle
+
+    title2 = '45 45 90 triangle mirrored and rotated, math name: p4m'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+
+    # Sample of tile.
+
+    y = y + triangle_size
+    tri_created = dwg.use(defs_g_triright, insert=(50, y), fill='yellow')
+    dwg.add(tri_created)
+    tri_created = dwg.use(defs_g_p4m_cell, insert=(150, y), fill='yellow')
+    dwg.add(tri_created)
+    tri_created = dwg.use(defs_g_p4m_pattern, insert=(250, y), fill='yellow')
+    dwg.add(tri_created)
+    y = y + defs_g_p4m_pattern_size
+
+    for i in range(4):
+        y += defs_g_p4m_pattern_size
+        for j in range(8):
+            x = 50 + j * defs_g_p4m_pattern_size
+            sq_created = dwg.use(defs_g_p4m_pattern, fill='yellow')
+            sq_created.translate(x, y)
+            dwg.add(sq_created)
+    y += 2 * defs_g_p4m_pattern_size
+
+    # p4m rombus
+
+    title2 = 'Rombus rotated, math name: p3'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+
+    # Sample of tile.
+    # rombus_created = dwg.use(defs_g_rombus, insert=(100, y ), fill='yellow')
+
+    rombus_created = dwg.use(defs_g_rombus, insert=(75, y), fill='black')
+    dwg.add(rombus_created)
+    tile_created = dwg.use(defs_g_rombus_120, insert=(150, y), fill='black')
+    dwg.add(tile_created)
+    pattern_created = dwg.use(defs_g_rombus_pattern, insert=(250, y), fill='black')
+    dwg.add(pattern_created)
+    y = y + defs_g_rombus_size_y
+
+    y_offset = defs_g_rombus_size_y / 2.0
+    for i in range(12):
+        y += defs_g_rombus_pattern_size_y - y_offset
+        for j in range(16):
+            if i % 2:
+                x = 50 + defs_g_rombus_pattern_size_x / 2.0 + j * defs_g_rombus_pattern_size_x
+            else:
+                x = 50 + j * defs_g_rombus_pattern_size_x
+            pattern_created = dwg.use(defs_g_rombus_pattern, fill='black')
+            pattern_created.translate(x, y)
+            dwg.add(pattern_created)
+    y += 2 * defs_g_rombus_pattern_size_y
+
+    # p6 Equilateral triangle
+
+    title2 = 'Equilateral triangle rotated lower right point, math name: p6'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+
+    # Sample of tile.
+
+    y = y + triangle_size
+    tri_created = dwg.use(defs_g_trieq, insert=(50 + defs_g_trieq_size_x / 2.0, y), fill='navy')
+    dwg.add(tri_created)
+    dwg.add(dwg.circle(center=(50 + defs_g_trieq_size_x / 2.0, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    tile_created = dwg.use(defs_g_trieq_pattern, insert=(150 + defs_g_trieq_pattern_size_x / 2, y), fill='navy')
+    dwg.add(tile_created)
+    dwg.add(dwg.circle(center=(150 + defs_g_trieq_pattern_size_x / 2, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    y += defs_g_trieq_pattern_size_y
+    for i in range(9):
+        y += defs_g_trieq_pattern_size_y / 2.0
+        for j in range(6):
+            if i % 2:
+                x = 50 + j * 1.5 * defs_g_trieq_pattern_size_x
+            else:
+                x = 50 + 1.5 * defs_g_trieq_size_x + j * 1.5 * defs_g_trieq_pattern_size_x
+            pattern_created = dwg.use(defs_g_trieq_pattern, fill='navy')
+            pattern_created.translate(x, y)
+            dwg.add(pattern_created)
+    y += defs_g_trieq_pattern_size_y
+
+    title2 = 'Equilateral triangle rotated upper point, math name: p6'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size + defs_g_trieq_pattern_size_y
+
+    # sample centered rotated eqilateral triangle variation 2
+
+    tri_created = dwg.use(defs_g_trieq, insert=(50 + defs_g_trieq_size_x / 2.0, y), fill='navy')
+    dwg.add(tri_created)
+    dwg.add(dwg.circle(center=(50 + defs_g_trieq_size_x / 2.0, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    tile_created = dwg.use(defs_g_trieq_c60, insert=(150 + defs_g_trieq_size_x / 2, y), fill='navy')
+    dwg.add(tile_created)
+    dwg.add(dwg.circle(center=(150 + defs_g_trieq_size_x / 2, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    tile_created = dwg.use(defs_g_trieq_pattern_v2, insert=(250 + defs_g_trieq_pattern_size_x / 2, y), fill='navy')
+    dwg.add(tile_created)
+    dwg.add(dwg.circle(center=(250 + defs_g_trieq_pattern_size_x / 2, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    y += defs_g_trieq_pattern_size_y
+    for i in range(9):
+        y += defs_g_trieq_pattern_v2_size_y / 2.0
+        for j in range(6):
+            if i % 2:
+                x = 50 + j * 1.5 * defs_g_trieq_pattern_v2_size_x
+            else:
+                x = 50 + 1.5 * defs_g_trieq_size_x + j * 1.5 * defs_g_trieq_pattern_v2_size_x
+            pattern_created = dwg.use(defs_g_trieq_pattern_v2, fill='navy')
+            pattern_created.translate(x, y)
+            dwg.add(pattern_created)
+    y += defs_g_trieq_pattern_size_y
+
+    title2 = 'Equilateral triangle rotated lower left, math name: p6'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size + defs_g_trieq_pattern_size_y
+
+    # sample centered rotated eqilateral triangle variation 3
+
+    tri_created = dwg.use(defs_g_trieq, insert=(50 + defs_g_trieq_size_x / 2.0, y), fill='navy')
+    dwg.add(tri_created)
+    dwg.add(dwg.circle(center=(50 + defs_g_trieq_size_x / 2.0, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    tile_created = dwg.use(defs_g_trieq_c60, insert=(150 + defs_g_trieq_size_x / 2, y), fill='navy')
+    dwg.add(tile_created)
+    dwg.add(dwg.circle(center=(150 + defs_g_trieq_size_x / 2, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    tile_created = dwg.use(defs_g_trieq_pattern_v3, insert=(250 + defs_g_trieq_pattern_size_x / 2, y), fill='navy')
+    dwg.add(tile_created)
+    dwg.add(dwg.circle(center=(250 + defs_g_trieq_pattern_size_x / 2, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    y += defs_g_trieq_pattern_size_y
+    for i in range(9):
+        y += defs_g_trieq_pattern_v3_size_y / 2.0
+        for j in range(6):
+            if i % 2:
+                x = 50 + j * 1.5 * defs_g_trieq_pattern_v3_size_x
+            else:
+                x = 50 + 1.5 * defs_g_trieq_size_x + j * 1.5 * defs_g_trieq_pattern_v3_size_x
+            pattern_created = dwg.use(defs_g_trieq_pattern_v3, fill='navy')
+            pattern_created.translate(x, y)
+            dwg.add(pattern_created)
+    y += defs_g_trieq_pattern_size_y
+
+    # All items have been added so save the svg to a the file.# Sample of tile.
+
+    dwg.save()
+
+
+if __name__ == '__main__':
+    create_svg(PROGNAME + '.svg')
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 textwidth=99
+

examples/ltattrie/tiling_part_5.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+import math, sys
+import svgwrite
+
+#
+# http://www.w3.org/TR/SVG11/struct.html#UseElement
+#
+# For more information on tesselation / tiling see http://en.wikipedia.org/wiki/Wallpaper_group
+# The organization of these tilings are from the interesting book
+#   Designing Testellations: The Secrets of Interlocking Patterns by Jinny Beyer.
+
+PROGNAME = sys.argv[0].rstrip('.py')
+
+def create_svg(name):
+    svg_size_width = 900
+    svg_size_height = 1600
+    font_size = 20
+    square_size = 30
+    title1 = name + ': Part 5 tiling with multiple def, groups, use, translate and scale.'
+    sqrt3 = math.sqrt(3)  # do this calc once instead of repeating the calc many times.
+    dwg = svgwrite.Drawing(name, (svg_size_width, svg_size_height), debug=True)
+
+    # ####################
+    # p3m1 - Mirror and Three rotations
+    #      - Equilateral triangle mirrored, rotated
+    # All three sides are the same length, all three angles are 60 degrees.
+    # The height of the triangle h = sqrt(3)/2.0 * length of a side
+    # The centre of the triangle is  sqrt(3)/6.0 * length of a side
+
+    defs_g_trieq_size_x = square_size
+    defs_g_trieq_size_y = defs_g_trieq_size_x * sqrt3 / 2.0
+    defs_g_trieq_centre = sqrt3 / 6.0 * defs_g_trieq_size_x
+
+    # width of equilateral triangle at the centre
+
+    defs_g_trieq_centre_size_x = defs_g_trieq_size_x - defs_g_trieq_size_x * defs_g_trieq_centre / defs_g_trieq_size_y
+
+    # defs_g_trieq = dwg.defs.add(dwg.g(id='defs_g_trieq', clip_path='url(#cliptrieq)'))
+
+    defs_g_trieq = dwg.defs.add(dwg.g(id='defs_g_trieq'))
+    defs_g_trieq.add(dwg.polygon([(0, -defs_g_trieq_size_y + defs_g_trieq_centre), (defs_g_trieq_size_x / 2.0, defs_g_trieq_centre),
+                     (-defs_g_trieq_size_x / 2.0, defs_g_trieq_centre)], stroke='none'))
+    defs_g_trieq.add(dwg.polygon([(-defs_g_trieq_size_x / 2.0, defs_g_trieq_centre), (-defs_g_trieq_centre_size_x / 2.0, 0),
+                     (defs_g_trieq_centre_size_x / 2.0, 0), (0, defs_g_trieq_centre)], stroke='none', fill='yellow'))
+
+    # Create mirror of the equilateral triangle.
+
+    defs_g_trieq_m = dwg.defs.add(dwg.g(id='defs_g_trieq_m'))
+    defs_g_trieq_m.add(dwg.use(defs_g_trieq, insert=(0, 0)))
+    defs_g_trieq_m.scale(-1, -1)
+
+    # Create combined cell
+
+    defs_g_trieq_cc_size_x = 1.5 * defs_g_trieq_size_x
+    defs_g_trieq_cc_size_y = defs_g_trieq_size_y
+    defs_g_trieq_cc = dwg.defs.add(dwg.g(id='defs_g_trieq_cc'))
+    defs_g_trieq_cc.add(dwg.use(defs_g_trieq, insert=(-defs_g_trieq_size_x / 4.0, defs_g_trieq_size_y / 2.0 - defs_g_trieq_centre)))
+    defs_g_trieq_cc.add(dwg.use(defs_g_trieq_m, insert=(defs_g_trieq_size_x / 4.0, -(defs_g_trieq_size_y / 2.0 - defs_g_trieq_centre))))
+
+    # Create rotations of combined cell
+
+    defs_g_trieq_cc_120 = dwg.defs.add(dwg.g(id='defs_g_trieq_cc_120'))
+    defs_g_trieq_cc_120.add(dwg.use(defs_g_trieq_cc, insert=(0, 0), fill='mediumblue'))
+    defs_g_trieq_cc_120.rotate(120, center=(0, 0))
+    defs_g_trieq_cc_m120 = dwg.defs.add(dwg.g(id='defs_g_trieq_cc_m120'))
+    defs_g_trieq_cc_m120.add(dwg.use(defs_g_trieq_cc, insert=(0, 0), fill='navy'))
+    defs_g_trieq_cc_m120.rotate(-120, center=(0, 0))
+
+    # Create pattern from rotations of combined cell
+
+    defs_g_trieq_pattern_size_x = 2 * defs_g_trieq_size_x
+    defs_g_trieq_pattern_size_y = 2 * defs_g_trieq_size_y
+    defs_g_trieq_pattern = dwg.defs.add(dwg.g(id='defs_g_trieq_pattern'))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_cc, insert=(-defs_g_trieq_size_x / 4.0, -defs_g_trieq_cc_size_y / 2.0)))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_cc_120, insert=(defs_g_trieq_size_x / 2.0, 0)))
+    defs_g_trieq_pattern.add(dwg.use(defs_g_trieq_cc_m120, insert=(-defs_g_trieq_size_x / 4.0, defs_g_trieq_cc_size_y / 2.0)))
+
+    # ####################
+    # p31m - Three rotations and a mirror
+    #      - A Kite shape, half hexagon, and half of a 60 degree diamond will all work for this
+    #      symmetry.  This one will use a kite.
+    # 30, 60, 90 angle triangle
+    #   The length of the sides are 1:sqrt(3):2   2 is the hypotenuse
+    # invsqrt2 = 1/sqrt2
+    # invsqrt2_2 = invsqrt2 * invsqrt2 = 1/2 = .5 by definition
+    # sin and cos(45 degrees) is 1/sqrt2 = 0.707106781187
+    # cos(30 degrees) is sqrt3/2
+    # sin(30 degrees) is 1/2
+    # tan(30) = 1/sqrt(3)
+    # The height of equilateral triangle h = sqrt(3)/2.0 * length of a side
+    # The centre of equilateral triangle is  sqrt(3)/6.0 * length of a side
+
+    defs_g_kite_size_x = square_size
+    defs_g_kite_size_y = defs_g_kite_size_x * sqrt3 / 2.0 + defs_g_kite_size_x * sqrt3 / 6.0
+
+    # Having a clip path seems to increase the visibility of the lines between the tiles.
+    # A clipping path may be necessary if the shapes go outside the triangle.
+    # defs_g_kite = dwg.defs.add(dwg.g(id='defs_g_kite', clip_path='url(#clipkite)'))
+
+    defs_g_kite = dwg.defs.add(dwg.g(id='defs_g_kite'))
+    defs_g_kite.add(dwg.polygon([(0, 0), 
+        (defs_g_kite_size_x / 2.0, defs_g_kite_size_x / (sqrt3 * 2.0)), 
+        (0, defs_g_kite_size_y), 
+        (-defs_g_kite_size_x / 2.0, defs_g_kite_size_x / (sqrt3 * 2.0))], stroke='none'))
+    #defs_g_kite.add(dwg.polygon([(0, 0), 
+    #    (defs_g_kite_size_x / 4.0, (defs_g_kite_size_y + defs_g_kite_size_x / (sqrt3 * 2.0)) / 2.0),
+    #    (-defs_g_kite_size_x / 2.0, defs_g_kite_size_x / (sqrt3 * 2.0))], stroke='none', fill='yellow'))
+    defs_g_kite.add(dwg.polygon([(0, 0), 
+        (defs_g_kite_size_x / 2.0, defs_g_kite_size_x / (sqrt3 * 2.0)),
+        (0, defs_g_kite_size_y / 12.0),
+        (-defs_g_kite_size_x / 2.0, defs_g_kite_size_x / (sqrt3 * 2.0))], stroke='none',
+        fill='black'))
+    defs_g_kite.add(dwg.polygon([(0, defs_g_kite_size_y), 
+        (defs_g_kite_size_x / 2.0, defs_g_kite_size_x / (sqrt3 * 2.0)),
+        (0, defs_g_kite_size_y * 8.0 / 12.0),
+        (-defs_g_kite_size_x / 2.0, defs_g_kite_size_x / (sqrt3 * 2.0))], stroke='none',
+        fill='green'))
+
+    # Create rotations of the kite.
+
+    defs_g_kite_120 = dwg.defs.add(dwg.g(id='defs_g_kite_120'))
+    defs_g_kite_120.add(dwg.use(defs_g_kite, insert=(0, 0)))
+    defs_g_kite_120.rotate(120, center=(0, 0))
+    defs_g_kite_m120 = dwg.defs.add(dwg.g(id='defs_g_kite_m120'))
+    defs_g_kite_m120.add(dwg.use(defs_g_kite, insert=(0, 0)))
+    defs_g_kite_m120.rotate(-120, center=(0, 0))
+
+    # Now use the cell, rotated cells to create the combined cell.
+    # The height of equilateral triangle h = sqrt(3) / 2.0 * length of a side
+
+    defs_g_kite_cc_size_x = 2 * defs_g_kite_size_x
+    defs_g_kite_cc_size_y = defs_g_kite_size_x * sqrt3  # 2*(sqrt(3)/2.0)
+    defs_g_kite_cc = dwg.defs.add(dwg.g(id='defs_g_kite_cc'))
+    defs_g_kite_cc.add(dwg.use(defs_g_kite, insert=(0, 0)))
+    defs_g_kite_cc.add(dwg.use(defs_g_kite_120, insert=(0, 0)))
+    defs_g_kite_cc.add(dwg.use(defs_g_kite_m120, insert=(0, 0)))
+
+    # Now use the combined cell to create a mirrored combined cell
+
+    defs_g_kite_mcc = dwg.defs.add(dwg.g(id='defs_g_kite_mcc'))
+    defs_g_kite_mcc.add(dwg.use(defs_g_kite_cc, insert=(0, 0)))
+    defs_g_kite_mcc.scale(-1, -1)
+
+    # Now use the combined cell, and mirrored combined cell to create a pattern
+
+    defs_g_kite_pattern_size_x = 1.5 * defs_g_kite_cc_size_x
+    defs_g_kite_pattern_size_y = defs_g_kite_cc_size_y
+    defs_g_kite_pattern = dwg.defs.add(dwg.g(id='defs_g_kite_pattern'))
+    defs_g_kite_pattern.add(dwg.use(defs_g_kite_cc, insert=(-defs_g_kite_cc_size_x / 4.0, -sqrt3 / 12.0 * defs_g_kite_cc_size_x)))
+    defs_g_kite_pattern.add(dwg.use(defs_g_kite_mcc, insert=(defs_g_kite_cc_size_x / 4.0, sqrt3 / 12.0 * defs_g_kite_cc_size_x)))
+
+    # ####################
+    # p6m - Kaleidoscope Either of the two long sides of the primary triangle is mirrored.  The
+    # resulting shape is rotated six times.
+    # 30, 60, 90 angle triangle
+    #   The length of the sides are 1:sqrt(3):2   2 is the hypotenuse
+    # invsqrt2 = 1/sqrt2
+    # invsqrt2_2 = invsqrt2 * invsqrt2 = 1/2 = .5 by definition
+    # sin and cos(45 degrees) is 1/sqrt2 = 0.707106781187
+    # cos(30 degrees) is sqrt3/2
+    # sin(30 degrees) is 1/2
+    # tan(30) = 1/sqrt(3)
+    # # The height of equilateral triangle h = sqrt(3) / 2.0 * length of a side
+    # # The centre of equilateral triangle is  sqrt(3) / 6.0 * length of a side
+
+    defs_g_kale_tri_size_x = square_size
+    defs_g_kale_tri_size_y = defs_g_kale_tri_size_x * 4.0 / sqrt3
+
+    # Having a clip path seems to increase the visibility of the lines between the tiles.
+    # A clipping path may be necessary if the shapes go outside the triangle.
+    # defs_g_kale_tri = dwg.defs.add(dwg.g(id='defs_g_kale_tri', clip_path='url(#clipkale)'))
+
+    defs_g_kale_tri = dwg.defs.add(dwg.g(id='defs_g_kale_tri'))
+    defs_g_kale_tri.add(dwg.polygon([(0, -defs_g_kale_tri_size_y), (0, 0), (-defs_g_kale_tri_size_x, defs_g_kale_tri_size_x / sqrt3
+                        - defs_g_kale_tri_size_y)], stroke='none'))
+    defs_g_kale_tri.add(dwg.polygon([(-defs_g_kale_tri_size_x, defs_g_kale_tri_size_x / sqrt3 - defs_g_kale_tri_size_y), (0, 2.0
+                        * defs_g_kale_tri_size_x / sqrt3 - defs_g_kale_tri_size_y), (0, 3.0 * defs_g_kale_tri_size_x / sqrt3
+                        - defs_g_kale_tri_size_y)], stroke='none', fill='yellow'))
+
+    # Create mirror of the kale.
+
+    defs_g_kale_tri_m = dwg.defs.add(dwg.g(id='defs_g_kale_tri_m'))
+    defs_g_kale_tri_m.add(dwg.use(defs_g_kale_tri, insert=(0, 0)))
+    defs_g_kale_tri_m.scale(-1, 1)
+
+    # Now use the tri, rotated tri to create the combined cell.
+
+    defs_g_kale_cc_size_x = 2 * defs_g_kale_tri_size_x
+    defs_g_kale_cc_size_y = defs_g_kale_tri_size_y
+    defs_g_kale_cc = dwg.defs.add(dwg.g(id='defs_g_kale_cc'))
+    defs_g_kale_cc.add(dwg.use(defs_g_kale_tri, insert=(0, 0)))
+    defs_g_kale_cc.add(dwg.use(defs_g_kale_tri_m, insert=(0, 0)))
+
+    # Now rotate the combined cell.
+
+    defs_g_kale_cc_60 = dwg.defs.add(dwg.g(id='defs_g_kale_cc_60'))
+    defs_g_kale_cc_60.add(dwg.use(defs_g_kale_cc, insert=(0, 0)))
+    defs_g_kale_cc_60.rotate(60, center=(0, 0))
+    defs_g_kale_cc_120 = dwg.defs.add(dwg.g(id='defs_g_kale_cc_120'))
+    defs_g_kale_cc_120.add(dwg.use(defs_g_kale_cc, insert=(0, 0)))
+    defs_g_kale_cc_120.rotate(120, center=(0, 0))
+    defs_g_kale_cc_180 = dwg.defs.add(dwg.g(id='defs_g_kale_cc_180'))
+    defs_g_kale_cc_180.add(dwg.use(defs_g_kale_cc, insert=(0, 0)))
+    defs_g_kale_cc_180.rotate(180, center=(0, 0))
+    defs_g_kale_cc_m60 = dwg.defs.add(dwg.g(id='defs_g_kale_cc_m60'))
+    defs_g_kale_cc_m60.add(dwg.use(defs_g_kale_cc, insert=(0, 0)))
+    defs_g_kale_cc_m60.rotate(-60, center=(0, 0))
+    defs_g_kale_cc_m120 = dwg.defs.add(dwg.g(id='defs_g_kale_cc_m120'))
+    defs_g_kale_cc_m120.add(dwg.use(defs_g_kale_cc, insert=(0, 0)))
+    defs_g_kale_cc_m120.rotate(-120, center=(0, 0))
+
+    # Now use the cell and five rotated cells to create the pattern.
+
+    defs_g_kale_pattern_size_x = 2 * defs_g_kale_cc_size_x
+    defs_g_kale_pattern_size_y = 2 * defs_g_kale_cc_size_y
+    defs_g_kale_pattern = dwg.defs.add(dwg.g(id='defs_g_kale_pattern'))
+    defs_g_kale_pattern.add(dwg.use(defs_g_kale_cc, insert=(0, 0)))
+    defs_g_kale_pattern.add(dwg.use(defs_g_kale_cc_60, insert=(0, 0)))
+    defs_g_kale_pattern.add(dwg.use(defs_g_kale_cc_120, insert=(0, 0)))
+    defs_g_kale_pattern.add(dwg.use(defs_g_kale_cc_180, insert=(0, 0)))
+    defs_g_kale_pattern.add(dwg.use(defs_g_kale_cc_m60, insert=(0, 0)))
+    defs_g_kale_pattern.add(dwg.use(defs_g_kale_cc_m120, insert=(0, 0)))
+
+    # ########################
+    # Background will be dark but not black so the background does not overwhelm the colors.
+
+    dwg.add(dwg.rect(insert=(0, 0), size=('100%', '100%'), rx=None, ry=None, fill='grey'))
+
+    # Give the name of the example and a title.
+
+    y = font_size + 5
+    dwg.add(dwg.text(title1, insert=(0, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+
+    # p3m1 - Mirror and three rotations
+
+    title2 = 'Mirror and three rotations, math name: p3m1'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size + defs_g_trieq_size_x
+    cell_created = dwg.use(defs_g_trieq, insert=(50 + defs_g_trieq_size_x, y), fill='lightblue')
+    dwg.add(cell_created)
+    dwg.add(dwg.circle(center=(50 + defs_g_trieq_size_x, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    cc_created = dwg.use(defs_g_trieq_cc, insert=(150 + defs_g_trieq_cc_size_x, y), fill='lightblue')
+    dwg.add(cc_created)
+    dwg.add(dwg.circle(center=(150 + defs_g_trieq_cc_size_x, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    pattern_created = dwg.use(defs_g_trieq_pattern, insert=(250 + defs_g_trieq_cc_size_x, y), fill='lightblue')
+    dwg.add(pattern_created)
+    dwg.add(dwg.circle(center=(250 + defs_g_trieq_cc_size_x, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    y += defs_g_trieq_pattern_size_y
+    for i in range(8):
+        y += defs_g_trieq_pattern_size_y / 2.0
+        for j in range(6):
+            if i % 2:
+                x = 50 + j * 1.5 * defs_g_trieq_pattern_size_x
+            else:
+                x = 50 + 1.5 * defs_g_trieq_size_x + j * 1.5 * defs_g_trieq_pattern_size_x
+            pattern_created = dwg.use(defs_g_trieq_pattern, fill='lightblue')
+            pattern_created.translate(x, y)
+            dwg.add(pattern_created)
+    y += defs_g_trieq_pattern_size_y
+
+    #
+    # p31m sample cell, combined cell and tile
+
+    title2 = 'Kite rotated and mirrored, math name: p31m'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size + defs_g_kite_size_y
+    cell_created = dwg.use(defs_g_kite, insert=(50 + defs_g_kite_size_x / 2.0, y), fill='navy')
+    dwg.add(cell_created)
+    dwg.add(dwg.circle(center=(50 + defs_g_kite_size_x / 2.0, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    cc_created = dwg.use(defs_g_kite_cc, insert=(150 + defs_g_kite_size_x / 2.0, y), fill='navy')
+    dwg.add(cc_created)
+    dwg.add(dwg.circle(center=(150 + defs_g_kite_size_x / 2.0, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    mcc_created = dwg.use(defs_g_kite_mcc, insert=(250 + defs_g_kite_cc_size_x / 2, y), fill='navy')
+    dwg.add(mcc_created)
+    dwg.add(dwg.circle(center=(250 + defs_g_kite_cc_size_x / 2, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    pattern_created = dwg.use(defs_g_kite_pattern, insert=(350 + defs_g_kite_cc_size_x, y), fill='navy')
+    dwg.add(pattern_created)
+    dwg.add(dwg.circle(center=(350 + defs_g_kite_cc_size_x, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    y += defs_g_kite_pattern_size_y
+    for i in range(6):
+        y += defs_g_kite_pattern_size_y
+        for j in range(8):
+            if i % 2:
+                x = 100 + (j + 0.5) * defs_g_kite_cc_size_x
+            else:
+                x = 100 + j * defs_g_kite_cc_size_x
+            pattern_created = dwg.use(defs_g_kite_pattern, fill='navy')
+            pattern_created.translate(x, y)
+            dwg.add(pattern_created)
+    y += defs_g_kite_pattern_size_y
+
+    # ##
+    # p6m kaleidoscope
+
+    title2 = 'Kaleidoscope 30, 60, 90 triangle mirrored and rotated, math name: p6m'
+    dwg.add(dwg.text(title2, insert=(50, y), font_family='serif', font_size=font_size, fill='white'))
+    y = y + font_size
+    y += defs_g_kale_tri_size_y
+    cell_created = dwg.use(defs_g_kale_tri, insert=(50 + defs_g_kale_tri_size_x, y), fill='navy')
+    dwg.add(cell_created)
+    dwg.add(dwg.circle(center=(50 + defs_g_kale_tri_size_x, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    cc_created = dwg.use(defs_g_kale_cc, insert=(150 + defs_g_kale_cc_size_x / 2.0, y), fill='navy')
+    dwg.add(cc_created)
+    dwg.add(dwg.circle(center=(150 + defs_g_kale_cc_size_x / 2.0, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    pattern_created = dwg.use(defs_g_kale_pattern, insert=(250 + defs_g_kale_pattern_size_x / 2.0, y), fill='navy')
+    dwg.add(pattern_created)
+    dwg.add(dwg.circle(center=(250 + defs_g_kale_pattern_size_x / 2.0, y), r=3, stroke='none', fill='purple', opacity='0.5'))
+    y += defs_g_kale_pattern_size_y / 2.0
+    for i in range(4):
+        y += defs_g_kale_pattern_size_y - defs_g_kale_pattern_size_x / (sqrt3 * 2)
+        for j in range(6):
+            if i % 2:
+                x = 100 + j * defs_g_kale_pattern_size_x
+            else:
+                x = 100 + defs_g_kale_cc_size_x + j * defs_g_kale_pattern_size_x
+            pattern_created = dwg.use(defs_g_kale_pattern, fill='navy')
+            pattern_created.translate(x, y)
+            dwg.add(pattern_created)
+    y += defs_g_kale_pattern_size_y
+
+    # All items have been added so save the svg to a the file.
+
+    dwg.save()
+
+
+if __name__ == '__main__':
+    create_svg(PROGNAME + '.svg')
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 textwidth=99
+

examples/ltattrie/xkcd_colour_data_svgwrite_3.py

 
 import sys
 import os
-import string
 import colorsys
 import svgwrite
 
-PROGNAME = 'xkcd_colour_data_svgwrite_3'
+PROGNAME = sys.argv[0].rstrip('.py')
 RGB_TXT = 'rgb.txt'
 
 print("program %s started." % PROGNAME)
     dwg.save()
 
 if __name__ == '__main__':
-    create_svg(PROGNAME+'_hl.svg')
+    create_svg(PROGNAME + '_hl.svg')
 print  ('program %s ended. ' % PROGNAME)