% alhambra-two-sisters-mosaic.tex
% Figure of a geometrical mosaic of intertwining loops from the Hall of the Two Sisters in the Alhambra.
%
% Copyright 2025 Alan J. Cain
%
% This work may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version
% 1.3c of this license or (at your option) any later version. The latest version of this license is in
%   https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX version 2008 or later.

% Although it is not required, if you use this work, a link to the author's book "Form & Number: A History of
% Mathematical Beauty" (for which this image was originally drawn) would be appreciated:
%   https://archive.org/details/cain_formandnumber_ebook_large

% This image is based on a geometrical mosaic from the Alhambra, redrawn to be mathematically exact. The mosaic appears
% in Goury, Jones & Gayangos's book of the architectural and artistic details of the Alhambra:
%   https://archive.org/details/gri_33125012280661/page/n290/mode/1up

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[
  ]

  % *** Configuration ***

  % Grid unit
  \pgfmathsetlengthmacro{\gridunit}{1mm}
  % Line width for loops. (Should be close to \gridunit but can be changed slightly without problems)
  \pgfmathsetlengthmacro{\looplinewidth}{1mm}
  % Extra space to leave when one line passes under another
  \pgfmathsetlengthmacro{\undergap}{.25pt}

  % "Background" colour
  \definecolor{cream}{rgb}{.994,.990,.949}
  % Colours for loops
  \definecolor{purple}{rgb}{.35,.16,.35}
  \definecolor{navy}{rgb}{.00,.00,.25}
  \definecolor{orange}{rgb}{.50,.30,.00}
  \definecolor{green}{rgb}{.24,.42,.32}
  \definecolor{cyan}{rgb}{.16,.30,.60}


  % *** Coordinate guides ***
  % \begin{scope}[
  %   x=\gridunit,
  %   y=\gridunit,
  %   every node/.style={
  %     text=gray,
  %     node font=\footnotesize\ttfamily,
  %   }
  %   ]

  %   \foreach \x in {0,1,...,131} {
  %     \pgfmathsetmacro{\c}{ifthenelse(mod(\x,5)==0,"gray","lightgray")}
  %     \pgfmathsetmacro{\extra}{ifthenelse(mod(\x,5)==0,4,2)}
  %     \draw[\c] (\x,{0-\extra}) -- (\x,{118+\extra});
  %   }
  %   \foreach \x in {0,5,...,131} {
  %     \node[anchor=west,rotate=-90] at (\x,-4) {\x};
  %     \node[anchor=east,rotate=-90] at (\x,122) {\x};
  %   }

  %   \foreach \y in {0,1,...,118} {
  %     \pgfmathsetmacro{\c}{ifthenelse(mod(\y,5)==0,"gray","lightgray")}
  %     \pgfmathsetmacro{\extra}{ifthenelse(mod(\y,5)==0,4,2)}
  %     \draw[\c] ({0-\extra},\y) -- ({131+\extra},\y);
  %   }
  %   \foreach \y in {0,5,...,118} {
  %     \node[anchor=east] at (-4,\y) {\y};
  %     \node[anchor=west] at (135,\y) {\y};
  %   }

  % \end{scope}


  % *** Mosaic ***

  \begin{scope}[
    x=\gridunit,
    y=\gridunit,
    line width=\looplinewidth,
    ]

    % Clip to size and fill background
    \clip ($ (0,0) + ({-.5*\looplinewidth},{-.5*\looplinewidth}) $) rectangle ($ (131,118) + (0,{.5*\looplinewidth}) $);
    \fill[cream] ($ (current bounding box.south west) + (-1,-1) $) rectangle ($ (current bounding box.south west) + (1,1) $);


    % All the loops are drawn as a sequence of segments from one point where the loop passes under another to the next.
    % The command \mline draws such a segment, where the endpoints are specified as the midpoint of the crossing of
    % loops (which will be a grid point), and applies the appropriate shortening to show the current loop as passing
    % under.
    %
    % Usage: \mline[<colour>] <path in TikZ syntax>;
    \pgfmathsetlengthmacro{\adjustlength}{.5*\looplinewidth+\undergap}
    \newcommand*{\mline}[1][]{\draw[shorten <=\adjustlength,shorten >=\adjustlength,#1]}


    % In the remainder of the drawing code, macros are defined and used to draw for particular loop shapes. Translations
    % and rotations are used to draw loops of the same shape. Only the first one is commented fully. Macros are defined
    % with (0,0) being in the lower-left corner of the shape defined by the macro. A "shift" transformation will mode it
    % to the correct location.


    % Macros for the the small purple seven-sided loop in the lower and upper left. It makes three passes under other
    % loops, so uses three applications of \mline.

    % An auxiliary macro is defined first:
    % Usage: \medcorneraux{<other transformation>}{<colour>}{<main shift>}
    \newcommand*{\medcorneraux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,7) -- (0,6) -- (0,0) -- (6,0) -- (9,3);
        \mline[#2] (9,3) -- (10,4) -- (10,6) -- (7,9);
        \mline[#2] (7,9) -- (6,10) -- (4,10) -- (1,7);
      \end{scope}
    }
    % Now different macros are used for four possible orientatiosn of this loop. The only different in these macros is
    % in the <other transformation> in the call to the auxiliary macro. Note that the <other transformation> includes
    % both a rotation and a shift, so (0,0) remains at the lower-left corner of the shape defined by the macro. The
    % <main shift> places the loop in the correct place.
    \newcommand*{\medcornersw}[2]{
      \medcorneraux{}{#1}{#2}
    }
    \newcommand*{\medcornernw}[2]{
      \medcorneraux{shift={(0,10)},rotate=-90}{#1}{#2}
    }
    \newcommand*{\medcornerne}[2]{
      \medcorneraux{shift={(10,10)},rotate=180}{#1}{#2}
    }
    \newcommand*{\medcornerse}[2]{
      \medcorneraux{shift={(10,0)},rotate=90}{#1}{#2}
    }
    % Draw the loop in the lower and upper left. The two commented-out commands could be used for a variant image that
    % does not extend to the right
    \medcornersw{purple}{2,2}
    \medcornernw{purple}{2,106}
    % \medcornerne{purple}{106,106}
    % \medcornerse{purple}{106,2}


    \newcommand*{\bigcorneraux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (3,25) -- (0,22) --(0,0) -- (22,0) -- (23,1);
        \mline[#2] (23,1) -- (27,5);
        \mline[#2] (27,5) -- (28,6) -- (28,8) -- (25,11);
        \mline[#2] (25,11) -- (21,15);
        \mline[#2] (21,15) -- (20,16) -- (16,16) -- (16,20) -- (13,23);
        \mline[#2] (13,23) -- (9,27);
        \mline[#2] (9,27) -- (8,28) -- (6,28) -- (3,25);
      \end{scope}
    }
    \newcommand*{\bigcornersw}[2]{
      \bigcorneraux{}{#1}{#2}
    }
    \newcommand*{\bigcornernw}[2]{
      \bigcorneraux{shift={(0,27)},rotate=-90}{#1}{#2}
    }
    \newcommand*{\bigcornerne}[2]{
      \bigcorneraux{shift={(27,27)},rotate=180}{#1}{#2}
    }
    \newcommand*{\bigcornerse}[2]{
      \bigcorneraux{shift={(27,0)},rotate=90}{#1}{#2}
    }

    \bigcornersw{green}{0,0}
    \bigcornernw{green}{0,91}
    % \bigcornerne{green}{91,91}
    % \bigcornerse{green}{91,0}


    \newcommand*{\hexagonaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (3,1) -- (4,2) -- (4,4) -- (2,4) -- (0,2) -- (0,0) -- (2,0) -- (3,1);
      \end{scope}
    }
    \newcommand*{\hexagonse}[2]{
      \hexagonaux{}{#1}{#2}
    }
    \newcommand*{\hexagonne}[2]{
      \hexagonaux{shift={(4,0)},rotate=90}{#1}{#2}
    }
    \newcommand*{\hexagonnw}[2]{
      \hexagonaux{shift={(4,4)},rotate=180}{#1}{#2}
    }
    \newcommand*{\hexagonsw}[2]{
      \hexagonaux{shift={(0,4)},rotate=-90}{#1}{#2}
    }

    \hexagonse{navy}{4,4}
    % \hexagonne{navy}{110,4}
    % \hexagonnw{navy}{110,110}
    \hexagonsw{navy}{4,110}

    \hexagonsw{purple}{44,14}
    \hexagonse{purple}{44,28}
    \hexagonne{purple}{70,28}
    \hexagonnw{purple}{70,14}

    \hexagonne{purple}{14,44}
    \hexagonse{purple}{28,44}
    \hexagonsw{purple}{28,70}
    \hexagonnw{purple}{14,70}

    \hexagonsw{purple}{44,86}
    \hexagonse{purple}{44,100}
    \hexagonne{purple}{70,100}
    \hexagonnw{purple}{70,86}

    \hexagonne{purple}{86,44}
    \hexagonse{purple}{100,44}
    \hexagonsw{purple}{100,70}
    \hexagonnw{purple}{86,70}

    \hexagonse{purple}{36,36}
    \hexagonne{purple}{78,36}
    \hexagonnw{purple}{78,78}
    \hexagonsw{purple}{36,78}

    \hexagonse{purple}{52,52}
    \hexagonne{purple}{62,52}
    \hexagonnw{purple}{62,62}
    \hexagonsw{purple}{52,62}

    \hexagonsw{purple}{116,86}
    \hexagonse{purple}{116,100}
    \hexagonsw{purple}{116,14}
    \hexagonse{purple}{116,28}
    \hexagonse{purple}{108,36}
    \hexagonse{purple}{124,52}
    \hexagonsw{purple}{108,78}
    \hexagonsw{purple}{124,62}


    \newcommand*{\rectangleaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (3,5) -- (7,1);
        \mline[#2] (7,1) -- (8,0) -- (10,0) -- (13,3);
        \mline[#2] (13,3) -- (14,4) -- (14,6) -- (11,9);
        \mline[#2] (11,9) -- (7,13);
        \mline[#2] (7,13) -- (6,14) -- (4,14) -- (1,11);
        \mline[#2] (1,11) -- (0,10) -- (0,8) -- (3,5);
      \end{scope}
    }
    \newcommand*{\rectanglenwse}[2]{
      \rectangleaux{}{#1}{#2}
    }
    \newcommand*{\rectanglenesw}[2]{
      \rectangleaux{shift={(0,14)},rotate=-90}{#1}{#2}
    }

    \rectanglenwse{cyan}{2,2}
    % \rectanglenesw{cyan}{102,2}
    % \rectanglenwse{cyan}{102,102}
    \rectanglenesw{cyan}{2,102}



    \newcommand*{\octagonaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,5) -- (0,4) -- (0,2) -- (2,0) -- (4,0) -- (5,1);
        \mline[#2] (5,1) -- (6,2) -- (6,4) -- (4,6) -- (2,6) -- (1,5);
      \end{scope}
    }
    \newcommand*{\octagonnwse}[2]{
      \octagonaux{}{#1}{#2}
    }
    \newcommand*{\octagonnesw}[2]{
      \octagonaux{shift={(6,0)},rotate=90}{#1}{#2}
    }

    \octagonnwse{navy}{14,4}
    \octagonnesw{navy}{4,14}
    % \octagonnwse{navy}{98,4}
    % \octagonnesw{navy}{108,14}
    % \octagonnwse{navy}{98,108}
    % \octagonnesw{navy}{108,98}
    \octagonnwse{navy}{14,108}
    \octagonnesw{navy}{4,98}

    \octagonnesw{cyan}{36,42}
    \octagonnwse{cyan}{42,36}
    \octagonnwse{cyan}{70,36}
    \octagonnesw{cyan}{76,42}
    \octagonnesw{cyan}{76,70}
    \octagonnwse{cyan}{70,76}
    \octagonnesw{cyan}{36,70}
    \octagonnwse{cyan}{42,76}

    \octagonnesw{cyan}{108,42}
    \octagonnwse{cyan}{114,36}
    \octagonnesw{cyan}{108,70}
    \octagonnwse{cyan}{114,76}


    \newcommand*{\medtwolinkaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,7) -- (0,6) -- (0,4) -- (3,1);
        \mline[#2] (3,1) -- (4,0) -- (10,0) -- (12,2) -- (12,4) -- (13,5);
        \mline[#2] (13,5) -- (14,6) -- (14,8) -- (16,10) -- (22,10) -- (23,9);
        \mline[#2] (23,9) -- (26,6) -- (26,4) -- (25,3);
        \mline[#2] (25,3) -- (22,0) -- (16,0) -- (15,1);
        \mline[#2] (15,1) -- (14,2) -- (14,4) -- (12,6) -- (12,8) -- (11,9);
        \mline[#2] (11,9) -- (10,10) -- (4,10) -- (1,7);
      \end{scope}
    }
    \newcommand*{\medtwolinkhor}[2]{
      \medtwolinkaux{}{#1}{#2}
    }
    \newcommand*{\medtwolinkver}[2]{
      \medtwolinkaux{shift={(0,26)},rotate=-90}{#1}{#2}
    }

    \medtwolinkhor{orange}{10,2}
    \medtwolinkhor{orange}{82,2}
    \medtwolinkhor{orange}{10,106}
    \medtwolinkhor{orange}{82,106}
    \medtwolinkver{orange}{2,10}
    \medtwolinkver{orange}{2,82}
    % \medtwolinkver{orange}{106,10}
    % \medtwolinkver{orange}{106,82}


    \newcommand*{\medthreelinkaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,7) -- (0,6) -- (0,4) -- (3,1);
        \mline[#2] (3,1) -- (4,0) -- (10,0) -- (12,2) -- (12,4) -- (13,5);
        \mline[#2] (13,5) -- (14,6) -- (14,8) -- (16,10) -- (18,10) -- (19,9);
        \mline[#2] (19,9) -- (20,8) -- (22,8) -- (24,10) -- (26,10) -- (27,9);
        \mline[#2] (27,9) -- (28,8) -- (28,6) -- (30,4) -- (30,2) -- (31,1);
        \mline[#2] (31,1) -- (32,0) -- (38,0) -- (41,3);
        \mline[#2] (41,3) -- (42,4) -- (42,6) -- (39,9);
        \mline[#2] (39,9) -- (38,10) -- (32,10) -- (30,8) -- (30,6) -- (29,5);
        \mline[#2] (29,5) -- (28,4) -- (28,2) -- (26,0) -- (24,0) -- (23,1);
        \mline[#2] (23,1) -- (22,2) -- (20,2) -- (18,0) -- (16,0) -- (15,1);
        \mline[#2] (15,1) -- (14,2) -- (14,4) -- (12,6) -- (12,8) -- (11,9);
        \mline[#2] (11,9) -- (10,10) -- (4,10) -- (1,7);
      \end{scope}
    }
    \newcommand*{\medthreelinkhor}[2]{
      \medthreelinkaux{}{#1}{#2}
    }
    \newcommand*{\medthreelinkver}[2]{
      \medthreelinkaux{shift={(10,0)},rotate=90}{#1}{#2}
    }

    \medthreelinkhor{orange}{38,2}
    \medthreelinkhor{orange}{38,106}
    \medthreelinkver{orange}{2,38}
    % \medthreelinkver{orange}{106,38}

    \medthreelinkhor{orange}{110,2}
    \medthreelinkhor{orange}{110,106}


    \newcommand*{\smallthreelinkaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,5) -- (0,4) -- (0,2) -- (2,0) -- (4,0) -- (5,1);
        \mline[#2] (5,1) -- (6,2) -- (8,2) -- (10,0) --(12,0) -- (13,1);
        \mline[#2] (13,1) -- (14,2) -- (16,2) -- (18,0) -- (20,0) -- (21,1);
        \mline[#2] (21,1) -- (22,2) -- (22,4) -- (20,6) -- (18,6) -- (17,5);
        \mline[#2] (17,5) -- (16,4) -- (14,4) -- (12,6) -- (10,6) -- (9,5);
        \mline[#2] (9,5) -- (8,4) -- (6,4) -- (4,6) -- (2,6) -- (1,5);
      \end{scope}
    }
    \newcommand*{\smallthreelinkhor}[2]{
      \smallthreelinkaux{}{#1}{#2}
    }
    \newcommand*{\smallthreelinkver}[2]{
      \smallthreelinkaux{shift={(6,0)},rotate=90}{#1}{#2}
    }

    \smallthreelinkhor{navy}{26,4}
    \smallthreelinkhor{navy}{70,4}
    \smallthreelinkhor{navy}{26,108}
    \smallthreelinkhor{navy}{70,108}
    \smallthreelinkver{navy}{4,26}
    \smallthreelinkver{navy}{4,70}
    % \smallthreelinkver{navy}{108,26}
    % \smallthreelinkver{navy}{108,70}

    \smallthreelinkhor{navy}{98,4}
    \smallthreelinkhor{navy}{98,108}


    \newcommand*{\medsidepieceaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,7) -- (0,6) -- (0,4) -- (3,1);
        \mline[#2] (3,1) -- (4,0) -- (10,0) -- (13,3);
        \mline[#2] (13,3) -- (14,4) -- (14,6) -- (11,9);
        \mline[#2] (11,9) -- (8,12) -- (6,12) -- (5,11);
        \mline[#2] (5,11) -- (1,7);
      \end{scope}
    }
    \newcommand*{\medsidepiecen}[2]{
      \medsidepieceaux{}{#1}{#2}
    }
    \newcommand*{\medsidepiecee}[2]{
      \medsidepieceaux{shift={(0,14)},rotate=-90}{#1}{#2}
    }
    \newcommand*{\medsidepieces}[2]{
      \medsidepieceaux{shift={(14,12)},rotate=180}{#1}{#2}
    }
    \newcommand*{\medsidepiecew}[2]{
      \medsidepieceaux{shift={(12,0)},rotate=90}{#1}{#2}
    }

    \medsidepiecen{purple}{30,2}
    \medsidepiecen{purple}{74,2}
    \medsidepiecee{purple}{2,30}
    \medsidepiecee{purple}{2,74}
    \medsidepieces{purple}{30,104}
    \medsidepieces{purple}{74,104}
    % \medsidepiecew{purple}{104,30}
    % \medsidepiecew{purple}{104,74}

    \medsidepiecen{purple}{102,2}
    \medsidepieces{purple}{102,104}


    \newcommand*{\bigsidepieceleftaux}[3]{
      \begin{scope}[shift={(#3)},#1]

        \pgfmathsetlengthmacro{\offset}{((.5*\pgflinewidth+\undergap)/cos(45))-.5*\gridunit}

        \mline[#2] (1,9) -- (0,8) -- (0,6) -- (3,3);
        \mline[#2] (3,3) -- (6,0) -- (40,0) -- (41,1);
        \mline[#2] (41,1) -- (45,5);
        \mline[#2] (45,5) -- (46,6) -- (46,8) -- (43,11);
        \begin{scope}
          \clip[shift={(0,{-.5*\pgflinewidth-\undergap})}] ($ (41,14) + (0,\offset) $) -- ++(-2,0) |- ++(5,-4) |- cycle;
          \mline[#2] (43,11) -- (38,16) -- (34,16) -- (31,19);
        \end{scope}
        \begin{scope}
          \clip[shift={(0,{.5*\pgflinewidth+\undergap})}] ($ (41,14) + (0,\offset) $) |- ++(-10,5) |- cycle;
          \mline[#2] (43,11) -- (38,16) -- (34,16) -- (31,19);
        \end{scope}
        \mline[#2] (31,19) -- (27,23);
        \mline[#2] (27,23) -- (23,27);
        \mline[#2] (23,27) -- (22,28) -- (20,28) -- (17,25);
        \mline[#2] (17,25) -- (13,21);
        \mline[#2] (13,21) -- (12,20) -- (12,16) -- (8,16) -- (5,13);
        \mline[#2] (5,13) -- (1,9);
      \end{scope}
    }
    \newcommand*{\bigsidepiecelefts}[2]{
      \bigsidepieceleftaux{}{#1}{#2}
    }
    \newcommand*{\bigsidepieceleftw}[2]{
      \bigsidepieceleftaux{shift={(0,46)},rotate=-90,}{#1}{#2}
    }
    \newcommand*{\bigsidepieceleftn}[2]{
      \bigsidepieceleftaux{shift={(46,28)},rotate=180,}{#1}{#2}
    }
    \newcommand*{\bigsidepiecelefte}[2]{
      \bigsidepieceleftaux{shift={(28,0)},rotate=90,}{#1}{#2}
    }

    \bigsidepiecelefts{green}{18,0}
    \bigsidepieceleftw{green}{0,54}
    \bigsidepieceleftn{green}{54,90}
    % \bigsidepiecelefte{green}{90,18}

    \bigsidepiecelefts{green}{90,0}
    \bigsidepieceleftn{green}{126,90}


    \newcommand*{\bigsidepiecerightaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,9) -- (0,8) -- (0,6) -- (3,3);
        \mline[#2] (3,3) -- (6,0) -- (40,0) -- (41,1);
        \mline[#2] (41,1) -- (45,5);
        \mline[#2] (45,5) -- (46,6) -- (46,8) -- (43,11);
        \mline[#2] (43,11)-- (39,15);
        \mline[#2] (39,15) -- (38,16) -- (34,16) -- (34,20) -- (31,23);
        \mline[#2] (31,23) -- (27,27);
        \mline[#2] (27,27) -- (26,28) -- (24,28) -- (21,25);
        \mline[#2] (21,25) -- (17,21);
        \mline[#2] (17,21) -- (13,17);
        \mline[#2] (13,17) -- (12,16) -- (8,16) -- (5,13);
        \mline[#2] (5,13) -- (1,9);
      \end{scope}
    }
    \newcommand*{\bigsidepiecerights}[2]{
      \bigsidepiecerightaux{}{#1}{#2}
    }
    \newcommand*{\bigsidepiecerightw}[2]{
      \bigsidepiecerightaux{shift={(0,46)},rotate=-90,}{#1}{#2}
    }
    \newcommand*{\bigsidepiecerightn}[2]{
      \bigsidepiecerightaux{shift={(46,28)},rotate=180,}{#1}{#2}
    }
    \newcommand*{\bigsidepiecerighte}[2]{
      \bigsidepiecerightaux{shift={(28,0)},rotate=90,}{#1}{#2}
    }

    \bigsidepiecerights{green}{54,0}
    \bigsidepiecerightw{green}{0,18}
    \bigsidepiecerightn{green}{18,90}
    % \bigsidepiecerighte{green}{90,54}

    \bigsidepiecerightn{green}{90,90}
    \bigsidepiecerights{green}{126,0}


    \newcommand*{\knotcorneraux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,7) -- (0,6) -- (0,0) -- (6,0) -- (8,2) -- (8,4) -- (7,5);
        \mline[#2] (7,5) -- (4,8) -- (2,8) -- (1,7);
      \end{scope}
    }
    \newcommand*{\knotcornersw}[2]{
      \knotcorneraux{}{#1}{#2}
    }
    \newcommand*{\knotcornerse}[2]{
      \knotcorneraux{shift={(8,0)},rotate=90}{#1}{#2}
    }
    \newcommand*{\knotcornerne}[2]{
      \knotcorneraux{shift={(8,8)},rotate=180}{#1}{#2}
    }
    \newcommand*{\knotcornernw}[2]{
      \knotcorneraux{shift={(0,8)},rotate=-90}{#1}{#2}
    }

    \knotcornersw{purple}{14,14}
    \knotcornerse{purple}{24,14}
    \knotcornerne{purple}{24,24}
    \knotcornernw{purple}{14,24}

    \knotcornersw{purple}{14,86}
    \knotcornerse{purple}{24,86}
    \knotcornerne{purple}{24,96}
    \knotcornernw{purple}{14,96}

    % \knotcornersw{purple}{86,86}
    % \knotcornerse{purple}{96,86}
    % \knotcornerne{purple}{96,96}
    % \knotcornernw{purple}{86,96}
    % %
    % \knotcornersw{purple}{86,14}
    % \knotcornerse{purple}{96,14}
    % \knotcornerne{purple}{96,24}
    % \knotcornernw{purple}{86,24}


    \newcommand*{\bigknotquarter}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,7) -- (0,6) -- (0,0) -- (6,0) -- (8,2) -- (8,4) -- (9,5);
        \mline[#2] (9,5) -- (10,6) -- (14,6) -- (14,4) -- (12,4) -- (12,6);
        \mline[#2] (12,6) -- (12,8) -- (14,10) -- (16,10) -- (17,11);
      \end{scope}
    }
    \newcommand*{\bigknot}[2]{
      \bigknotquarter{}{#1}{#2}
      \bigknotquarter{shift={(0,18)},rotate=-90}{#1}{#2}
      \bigknotquarter{shift={(18,18)},rotate=180}{#1}{#2}
      \bigknotquarter{shift={(18,0)},rotate=90}{#1}{#2}

      \begin{scope}[shift={(#2)}]
        \fill[orange] (7.5,10.5) -- (10.5,10.5) -- (10.5,8) -- (9,6.5) -- (7.5,8) -- cycle;
        \draw[line width={.75*\gridunit},white] (7.5,10.5) -- (10.5,7.5);
      \end{scope}
    }
    % \newcommand*{\bigknot}[2]{
    %   \bigknotcorneraux{shift={(8,0)},rotate=90}{#1}{#2}
    % }
    % \newcommand*{\bigknot}[2]{
    %   \bigknotcorneraux{shift={(8,8)},rotate=180}{#1}{#2}
    % }
    % \newcommand*{\bigknot}[2]{
    %   \bigknotcorneraux{shift={(0,8)},rotate=-90}{#1}{#2}
    % }

    \bigknot{purple}{86,14}
    \bigknot{purple}{86,86}


    \newcommand*{\knot}[2]{
      \begin{scope}[shift={(#2)}]
        \mline[#1] (1,3) -- (0,2) -- (0,0) -- (2,0) -- (4,2) -- (4,4);
        \mline[#1] (4,4) -- (4,8) -- (3,9);
        \mline[#1] (3,9) -- (2,10) -- (0,10) -- (0,8) -- (2,6) -- (4,6);
        \mline[#1] (4,6) -- (8,6) -- (9,7);
        \mline[#1] (9,7) -- (10,8) -- (10,10) -- (8,10) -- (6,8) -- (6,6);
        \mline[#1] (6,6) -- (6,2) -- (7,1);
        \mline[#1] (7,1) -- (8,0) -- (10,0) -- (10,2) -- (8,4) -- (6,4);
        \mline[#1] (6,4) -- (2,4) -- (1,3);
      \end{scope}
    }
    % %
    \knot{orange}{18,18}
    \knot{orange}{18,90}
    % \knot{orange}{90,18}
    % \knot{orange}{90,90}


    \newcommand*{\triangleaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (3,11) -- (2,10) -- (2,6) -- (0,4) -- (0,2) -- (1,1);
        \mline[#2] (1,1) -- (2,0) -- (8,0) -- (10,2) -- (12,2) -- (13,1);
        \mline[#2] (13,1) -- (14,0) -- (20,0) -- (22,2) -- (22,4) -- (21,5);
        \mline[#2] (21,5) -- (20,6) -- (20,10) -- (17,13);
        \mline[#2] (17,13) -- (13,17);
        \mline[#2] (13,17) -- (12,18) -- (10,18) -- (7,15);
        \mline[#2] (7,15) -- (3,11);
      \end{scope}
    }
    \newcommand*{\trianglen}[2]{
      \triangleaux{}{#1}{#2}
    }
    \newcommand*{\trianglee}[2]{
      \triangleaux{shift={(0,22)},rotate=-90}{#1}{#2}
    }
    \newcommand*{\triangles}[2]{
      \triangleaux{shift={(22,18)},rotate=180}{#1}{#2}
    }
    \newcommand*{\trianglew}[2]{
      \triangleaux{shift={(18,0)},rotate=90}{#1}{#2}
    }

    \trianglen{orange}{12,34}
    \trianglen{orange}{84,34}
    \trianglee{orange}{34,12}
    \trianglee{orange}{34,84}
    \triangles{orange}{12,66}
    \triangles{orange}{84,66}
    \trianglew{orange}{66,12}
    \trianglew{orange}{66,84}

    \trianglee{orange}{106,12}
    \trianglee{orange}{106,84}


    \newcommand*{\bowtieaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (7,3) -- (6,4) -- (4,4) -- (2,6) -- (0,6) -- (0,0) -- (2,0) -- (3,1);
        \mline[#2] (3,1) -- (4,2) -- (6,2) -- (8,4) -- (10,4) -- (11,5);
        \mline[#2] (11,5) -- (12,6) -- (14,6) -- (14,0) -- (12,0) -- (10,2) -- (8,2) -- (7,3);
      \end{scope}
    }
    \newcommand*{\bowtiehor}[2]{
      \bowtieaux{}{#1}{#2}
    }
    \newcommand*{\bowtiever}[2]{
      \bowtieaux{shift={(0,14)},rotate=-90}{#1}{#2}
    }

    \bowtiehor{cyan}{16,36}
    \bowtiehor{cyan}{88,36}
    \bowtiehor{cyan}{16,76}
    \bowtiehor{cyan}{88,76}
    \bowtiever{cyan}{36,16}
    \bowtiever{cyan}{36,88}
    \bowtiever{cyan}{76,16}
    \bowtiever{cyan}{76,88}

    \bowtiever{cyan}{108,16}
    \bowtiever{cyan}{108,88}


    \newcommand*{\squareaux}[3]{
      \begin{scope}[shift={(#3)},#1]

        \pgfmathsetlengthmacro{\offset}{((.5*\pgflinewidth+\undergap)/cos(45))-.5*\gridunit}

        \mline[#2] (1,23) -- (0,22) -- (0,20) -- (3,17);
        \mline[#2] (3,17) -- (7,13);
        \mline[#2] (7,13) -- (8,12) -- (12,12) -- (12,8) -- (15,5);
        \mline[#2] (15,5) -- (19,1);
        \mline[#2] (19,1) -- (20,0) -- (22,0) -- (25,3);
        \mline[#2] (25,3) -- (29,7);
        \mline[#2] (29,7) -- (33,11);
        \mline[#2] (33,11) -- (34,12) -- (38,12) -- (41,15);
        \mline[#2] (41,15) -- (45,19);
        \mline[#2] (45,19) -- (46,20) -- (46,22) -- (43,25);
        \mline[#2] (43,25) -- (39,29);
        \mline[#2] (39,29) -- (36,32) -- (32,32) -- (32,36) -- (31,37);
        \mline[#2] (31,37) -- (27,41);
        \mline[#2] (27,41) -- (23,45);
        \mline[#2] (23,45) -- (22,46) -- (20,46) -- (17,43);
        \begin{scope}
          \clip[shift={({-.5*\pgflinewidth-\undergap},0)}] ($ (14,40) + (-\offset,0) $) -| ++(-6,-10) -| cycle;
          \mline[#2] (17,43) -- (12,38) -- (12,34) -- (9,31);
        \end{scope}
        \begin{scope}
          \clip[shift={({.5*\pgflinewidth+\undergap},0)}] ($ (14,40) + (-\offset,0) $) -- ++(0,-1) -| ++(3,4) -| cycle;
          \mline[#2] (17,43) -- (12,38) -- (12,34) -- (9,31);
        \end{scope}
        \mline[#2] (9,31) -- (5,27);
        \mline[#2] (5,27) -- (1,23);
      \end{scope}
    }
    \newcommand*{\squaresw}[2]{
      \squareaux{}{#1}{#2}
    }
    \newcommand*{\squarenw}[2]{
      \squareaux{shift={(0,46)},rotate=-90}{#1}{#2}
    }
    \newcommand*{\squarene}[2]{
      \squareaux{shift={(46,46)},rotate=180}{#1}{#2}
    }
    \newcommand*{\squarese}[2]{
      \squareaux{shift={(46,0)},rotate=90}{#1}{#2}
    }

    \squaresw{green}{18,18}
    \squarenw{green}{18,54}
    \squarene{green}{54,54}
    \squarese{green}{54,18}

    \squaresw{green}{90,18}
    \squarenw{green}{90,54}
    \squarene{green}{126,54}
    \squarese{green}{126,18}


    \newcommand*{\crosslinkaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,7) -- (0,6) -- (0,4) -- (2,2) -- (4,2) -- (5,3);
        \mline[#2] (5,3) -- (6,4) -- (8,4) -- (11,1);
        \mline[#2] (11,1) -- (12,0) -- (14,0) -- (16,2) -- (16,4) -- (17,5);
        \mline[#2] (17,5) -- (18,6) -- (18,8) -- (20,10) -- (22,10) -- (23,9);
        \mline[#2] (23,9) -- (26,6) -- (28,6) -- (29,7);
        \mline[#2] (29,7) -- (30,8) -- (32,8) -- (34,6) -- (34,4) -- (33,3);
        \mline[#2] (33,3) -- (32,2) -- (30,2) -- (28,4) -- (26,4) --(25,3);
        \mline[#2] (25,3) -- (22,0) -- (20,0) -- (19,1);
        \mline[#2] (19,1) -- (18,2) -- (18,4) -- (16,6) -- (16,8) -- (15,9);
        \mline[#2] (15,9) -- (14,10) -- (12,10) -- (9,7);
        \mline[#2] (9,7) -- (8,6) -- (6,6) -- (4,8) -- (2,8) -- (1,7);
      \end{scope}
    }
    \newcommand*{\crosslinkhor}[2]{
      \crosslinkaux{}{#1}{#2}
    }
    \newcommand*{\crosslinkver}[2]{
      \crosslinkaux{shift={(10,0)},rotate=90}{#1}{#2}
    }

    \crosslinkhor{navy}{42,18}
    \crosslinkhor{navy}{42,90}
    \crosslinkver{navy}{18,42}
    \crosslinkver{navy}{90,42}

    \crosslinkhor{navy}{114,18}
    \crosslinkhor{navy}{114,90}


    \newcommand*{\lozengeaux}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,8) -- (0,7) -- (0,5) -- (3,2);
        \mline[#2] (3,2) -- (5,0) -- (9,4);
        \mline[#2] (9,4) -- (10,5) -- (10,7) -- (7,10);
        \mline[#2] (7,10) -- (5,12) -- (1,8);
      \end{scope}
    }
    \newcommand*{\lozengever}[2]{
      \lozengeaux{}{#1}{#2}
    }
    \newcommand*{\lozengehor}[2]{
      \lozengeaux{shift={(12,0)},rotate=90}{#1}{#2}
    }

    \lozengever{purple}{54,17}
    \lozengever{purple}{54,89}
    \lozengehor{purple}{17,54}
    \lozengehor{purple}{89,54}

    \lozengever{purple}{126,17}
    \lozengever{purple}{126,89}


    \newcommand*{\giantcrossarmaux}[3]{
      \begin{scope}[shift={(#3)},#1]

        \pgfmathsetlengthmacro{\offset}{((.5*\pgflinewidth+\undergap)/cos(45))-.5*\gridunit}

        % \draw[thin,->] (13,12) -- +(0,\offset);
        % \pgfmathsetlengthmacro{\offset}{0}
        \mline[#2] (5,15) -- (9,19);
        \mline[#2] (7,9) -- (4,12) -- (4,14) -- (5,15);
        \mline[#2] (13,5) -- (12,6) -- (10,6) -- (7,9);
        \mline[#2] (19,1) -- (16,4) -- (14,4) -- (13,5);
        \mline[#2] (25,3) -- (22,0) -- (20,0) -- (19,1);
        \mline[#2] (23,9) -- (26,6) -- (26,4) -- (25,3);
        \begin{scope}
          \clip[shift={({(+.5*\pgflinewidth+\undergap)/cos(45)},0)}] (13,11) -- ++(2,2) -| ++ (8,-4) -| ++(-4,2) -- cycle;
          \mline[#2] (23,9) -- ($ (20,12) + (-\offset,\offset) $) -- ($ (6,12) + (\offset,\offset) $) -- (5,11);
        \end{scope}
        \begin{scope}
          % \draw[shift={({(-.5*\pgflinewidth-\undergap)/cos(45)},0)},thin] (13,11) -- ++(2,2) -| ++ (-10,-2) -- cycle;
          \clip[shift={({(-.5*\pgflinewidth-\undergap)/cos(45)},0)}] (13,11) -- ++(2,2) -| ++ (-10,-2) -- cycle;
          \mline[#2] (23,9) -- ($ (20,12) + (-\offset,\offset) $) -- ($ (6,12) + (\offset,\offset) $) -- (5,11);
        \end{scope}
        \mline[#2] (5,11) -- (1,7);
        \mline[#2] (1,7) -- (0,6) -- (0,4) -- (3,1);
        \mline[#2] (3,1) -- (4,0) -- (6,0) -- (9,3);
        \mline[#2] (9,3) -- (10,4) -- (12,4) -- (14,6) -- (16,6) -- (17,7);
        \mline[#2] (17,7) -- (21,11);
        \mline[#2] (21,11) -- (22,12) -- (22,14) -- (19,17);
        \mline[#2] (19,17) -- (16,20) -- (16,22) -- (17,23);
      \end{scope}
    }
    \newcommand*\giantcrossarms[2]{
      \giantcrossarmaux{}{#1}{#2}
    }
    \newcommand*\giantcrossarmw[2]{
      \giantcrossarmaux{shift={(0,26)},rotate=-90}{#1}{#2}
    }
    \newcommand*\giantcrossarmn[2]{
      \giantcrossarmaux{shift={(26,23)},rotate=180}{#1}{#2}
    }
    \newcommand*\giantcrossarme[2]{
      \giantcrossarmaux{shift={(23,0)},rotate=90}{#1}{#2}
    }

    \giantcrossarms{cyan}{46,2}
    \giantcrossarmw{cyan}{2,46}
    \giantcrossarmn{cyan}{46,93}
    % \giantcrossarme{cyan}{93,46}

    \giantcrossarms{cyan}{118,2}
    \giantcrossarmn{cyan}{118,93}


    \newcommand*{\giantcrossquarter}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (25,32) -- (28,35) -- (28,37) -- (29,38);
        \mline[#2] (21,28) -- (25,32);
        \mline[#2] (17,24) -- (21,28);
        \mline[#2] (13,20) -- (17,24);
        \mline[#2] (5,16) -- (6,17) -- (8,17) -- (10,19) -- (12,19) -- (13,20);
        \mline[#2] (1,12) -- (5,16);
        \mline[#2] (3,6) -- (0,9) -- (0,11) -- (1,12);
        \mline[#2] (5,0) -- (6,1) -- (6,3) -- (3,6);

        \mline[#2] (13,4) -- (17,8);
        \mline[#2] (17,8) -- (18,9) -- (18,11) -- (15,14);
        \mline[#2] (15,14) -- (12,17) -- (10,17) -- (9,18);
      \end{scope}
    }
    \newcommand*\giantcross[2]{
      \giantcrossquarter{shift={(29,0)}}{#1}{#2}
      \giantcrossquarter{shift={(0,47)},rotate=-90}{#1}{#2}
      \giantcrossquarter{shift={(47,76)},rotate=180}{#1}{#2}
      \giantcrossquarter{shift={(76,29)},rotate=90}{#1}{#2}
    }

    \giantcross{cyan}{21,21}

    \giantcross{cyan}{93,21}


    \newcommand*{\bigoctagonquarter}[3]{
      \begin{scope}[shift={(#3)},#1]

        \pgfmathsetlengthmacro{\offset}{((.5*\pgflinewidth+\undergap)/cos(45))-.5*\gridunit}

        \mline[#2] (3,21) -- (7,17);
        \mline[#2] (7,17) -- (11,13);
        \mline[#2] (11,13) -- (15,9);
        \mline[#2] (15,9) -- (19,5);
        \begin{scope}
          \clip[shift={({(+.5*\pgflinewidth+\undergap)/cos(45)},0)}] (29,3) -- ++(-2,-2) -| ++(10,2) -| cycle;
          \mline[#2] (19,5) -- ($ (22,2) + (\offset,-\offset) $) -- ($ (36,2) + (-\offset,-\offset) $) -- (37,3);
        \end{scope}
        \begin{scope}
          \clip[shift={({(-.5*\pgflinewidth-\undergap)/cos(45)},0)}] (29,3) -- ++(-2,-2) -| ++(-8,4) -| cycle;
          \mline[#2] (19,5) -- ($ (22,2) + (\offset,-\offset) $) -- ($ (36,2) + (-\offset,-\offset) $) -- (37,3);
        \end{scope}
      \end{scope}
    }
    \newcommand*\bigoctagon[2]{
      \bigoctagonquarter{shift={(0,0)}}{#1}{#2}
      \bigoctagonquarter{shift={(0,58)},rotate=-90}{#1}{#2}
      \bigoctagonquarter{shift={(58,58)},rotate=180}{#1}{#2}
      \bigoctagonquarter{shift={(58,0)},rotate=90}{#1}{#2}
    }

    \bigoctagon{navy}{30,30}

    \bigoctagon{navy}{102,30}


    \newcommand*{\bigcrossquarter}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,3) -- (4,0) -- (6,0) -- (7,1);
        \mline[#2] (7,1) -- (11,5);
        \mline[#2] (11,5) -- (15,9);
        \mline[#2] (15,9) -- (19,13);
        \mline[#2] (19,13) -- (20,14) -- (22,14) -- (25,11);
        \mline[#2] (25,11) -- (26,10) -- (28,10) -- (31,13);
        \mline[#2] (31,13) -- (32,14) -- (34,14) -- (37,11);
        \mline[#2] (37,11) -- (41,7);
        \mline[#2] (41,7) -- (45,3);
        \mline[#2] (45,3) -- (48,0) -- (50,0) -- (51,1);
      \end{scope}
    }
    \newcommand*\bigcross[2]{
      \bigcrossquarter{shift={(0,0)}}{#1}{#2}
      \bigcrossquarter{shift={(0,54)},rotate=-90}{#1}{#2}
      \bigcrossquarter{shift={(54,54)},rotate=180}{#1}{#2}
      \bigcrossquarter{shift={(54,0)},rotate=90}{#1}{#2}
    }

    \bigcross{navy}{32,32}

    \bigcross{navy}{104,32}


    \newcommand*{\medcrossquarter}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (3,5) -- (0,2) -- (0,0) -- (2,0) -- (3,1);
        \mline[#2] (3,1) -- (6,4) -- (10,4) -- (11,5);
        \mline[#2] (11,5) -- (14,8) -- (14,10) -- (15,11);
        \mline[#2] (15,11) -- (16,12) -- (16,14) -- (18,14) -- (20,16) -- (22,16) -- (23,17);
        \mline[#2] (23,17) -- (26,20) -- (26,24) -- (27,25);
      \end{scope}
    }
    \newcommand*\medcross[2]{
      \medcrossquarter{shift={(0,0)}}{#1}{#2}
      \medcrossquarter{shift={(0,30)},rotate=-90}{#1}{#2}
      \medcrossquarter{shift={(30,30)},rotate=180}{#1}{#2}
      \medcrossquarter{shift={(30,0)},rotate=90}{#1}{#2}
    }

    \medcross{navy}{44,44}

    \medcross{navy}{116,44}


    \newcommand*{\medoctagonquarter}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,11) -- (0,10) -- (0,8) -- (3,5);
        \mline[#2] (3,5) -- (7,1);
      \end{scope}
    }
    \newcommand*\medoctagon[2]{
      \medoctagonquarter{shift={(0,0)}}{#1}{#2}
      \medoctagonquarter{shift={(0,18)},rotate=-90}{#1}{#2}
      \medoctagonquarter{shift={(18,18)},rotate=180}{#1}{#2}
      \medoctagonquarter{shift={(18,0)},rotate=90}{#1}{#2}
    }

    \medoctagon{cyan}{50,50}

    \medoctagon{cyan}{122,50}


    \newcommand*{\outerringquarter}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,15) -- (0,14) -- (0,8) -- (3,5);
        \mline[#2] (3,5) -- (7,1);
        \mline[#2] (7,1) -- (8,0) -- (14,0) -- (16,2) -- (16,4) -- (17,5);
        \mline[#2] (17,5) -- (18,6) -- (18,8) -- (21,11);
        \mline[#2] (21,11) -- (24,14) -- (26,14) -- (27,13);
        \mline[#2] (27,13) -- (31,9);
        \mline[#2] (31,9) -- (32,8) -- (32,6) -- (34,4) -- (34,2) -- (35,1);
      \end{scope}
    }
    \newcommand*\outerring[2]{
      \outerringquarter{shift={(0,0)}}{#1}{#2}
      \outerringquarter{shift={(0,50)},rotate=-90}{#1}{#2}
      \outerringquarter{shift={(50,50)},rotate=180}{#1}{#2}
      \outerringquarter{shift={(50,0)},rotate=90}{#1}{#2}
    }

    \outerring{orange}{34,34}

    \outerring{orange}{106,34}


    \newcommand*{\innerringquarter}[3]{
      \begin{scope}[shift={(#3)},#1]
        \mline[#2] (1,23) -- (0,22) -- (0,20) -- (2,18) -- (4,18) -- (5,17);
        \mline[#2] (5,17) -- (6,16) -- (8,16) -- (11,13);
        \mline[#2] (11,13) -- (15,9);
        \mline[#2] (15,9) -- (16,8) -- (16,6) -- (18,4) -- (18,2) -- (19,1);
        \mline[#2] (19,1) -- (20,0) -- (22,0) -- (24,2) -- (26,2) -- (27,1);
      \end{scope}
    }
    \newcommand*\innerring[2]{
      \innerringquarter{shift={(0,0)}}{#1}{#2}
      \innerringquarter{shift={(0,50)},rotate=-90}{#1}{#2}
      \innerringquarter{shift={(50,50)},rotate=180}{#1}{#2}
      \innerringquarter{shift={(50,0)},rotate=90}{#1}{#2}
    }

    \innerring{orange}{34,34}

    \innerring{orange}{106,34}

  \end{scope}

\end{tikzpicture}


\end{document}
