\input mftmac
$\9 {\tt tiling.mp}\par
$\9 {\bf Truchet's problem: macros}\par
$\9==================================================\par
$\9 D. Roegel's solution for Denis Girou's {\tt pstricks} competition.\par
$\9 This solution is written in {\tt METAPOST}.\par
$\9 The source file may be formatted using the {\tt mft} program.\par
$\9 (the program used for volumes B and E of {\it Computers \& Typesetting\/})%
\par
$\9 Send comments and improvements to {\tt roegel@loria.fr}.\par
$\9 -------------------------------------------------------------------------%
\par
$\9 \underbar{History}:\par
$\9 9-10 June 1995 (lines)\par
$\9 13-14 June 1995 (surfaces)\par
$\9 16 June 1995 (comments and restructuring)\par
$\9 21 June 1995 (some more formatting for {\tt mft})\par
$\9 26 June 1995 (some simplifications)\par
$\9  9 July 1995 (macros and examples were split and code better organized)\par
$\9\par
$\9 This is my first ``large'' {\tt METAPOST} program. I'm still learning!\par
$\9 Therefore lots of things are clumsy and I don't pretend this\par
$\9 is an example of good {\tt METAPOST} programming.\par
$\9 I had however great fun writing it!\par
$\9\par
$\9 \underbar{Some highlights}:\par
$\9 The coloring of lines worked after circa 4 hours $\ldots$\par
$\9 The only real problem I met was that an infinite loop occured\par
$\9 when $\\{follow\_line}$ was following a cyclic path.\par
$\9\par
$\9 Concerning the coloration of surfaces, I needed some time\par
$\9 until I noticed that two colors were sufficient!\par
$\9 Once you notice this and draw all the conclusions, the task is trivial.\par
$\9 -------------------------------------------------------------------------%
\par
$\9\par
$\9 A tiling $\\{Nx}$ $\times$ $\\{Ny}$ is a matrix of tiles indexed by $x$\par
$\9 ranging between\par
$\9 $0$ and $\\{Nx}-1$ and $y$ ranging between $0$ and $\\{Ny}-1$.\par
$\9 Two kinds of tiles are considered. We will refer to them as `tile 0'\par
$\9 and `tile 1'. Each tile has three parts, marked {\tt*}, {\tt+} and {\tt x}.%
\par
$\9 {\catcode`\.=13\gdef\os{\obeyspaces\tt\frenchspacing\par
$\9                         \catcode`\.=13\def.{\char124 }}}\par
$\9 {\os\par
$\9 Tile 0: $\7" ------ "$ Tile 1: $\7" ------ "$\par
$\9         $\7".* .   ."$         $\7".  . * ."$\par
$\9         $\7"._/ + _."$         $\7"._ +\ _."$\par
$\9         $\7".    /x."$         $\7".x\    ."$\par
$\9         $\7".___.__."$         $\7".__.___."$}\par
$\9\par
$\9 ---------------------------------------------------------------------------%
\par
$\9\vfill\eject\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Tiling orientation and color initialization: BEGINNING\par
$%
\9-----------------------------------------------------------------------------%
\par
\BL
$\9 We define the tiling randomly.\par
$\2{def}\\{init\_tiling}(\2{expr}\\{nx},\\{ny})=$\par
\quad\quad\quad$\2{for}i:=0\4{upto}\\{nx}-1\?:$\par
\quad\quad\quad\quad\quad$\2{for}j:=0\4{upto}\\{ny}-1\?:$\par
\quad\quad\quad\quad\quad\quad\quad$\\{tile}[i][j]:=\\{coin};\ \9 we throw a
coin for each tile.\par
\quad\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 The `coin' macro behaves like a coin and yields randomly $0$ or $1$.\par
$\9 This is used for the initial tiling.\par
$\2{def}\\{coin}=\1{floor}(\1{uniformdeviate}2)\3{enddef};$\par
\BL
$\9 We let ${}-1$ mean ``no color yet''.\par
$\2{def}\\{init\_colors}=$\par
\quad\quad\quad$\2{for}i:=0\4{upto}\\{Nx}-1\?:\2{for}j:=0\4{upto}\\{Ny}-1\?:$%
\par
\quad\quad\quad\quad\quad$\\{tilecolor}[i][j][0]:=-1;\ \9 right\par
\quad\quad\quad\quad\quad$\\{tilecolor}[i][j][1]:=-1;\ \9 up\par
\quad\quad\quad\quad\quad$\\{tilecolor}[i][j][2]:=-1;\ \9 left\par
\quad\quad\quad\quad\quad$\\{tilecolor}[i][j][3]:=-1;\ \9 down\par
\quad\quad\quad$\!\3{endfor};\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\2{def}\\{init\_partcolors}=$\par
\quad\quad\quad$\2{for}i:=0\4{upto}\\{Nx}-1\?:$\par
\quad\quad\quad\quad\quad\quad$\2{for}j:=0\4{upto}\\{Ny}-1\?:$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{tilepartcolor}[i][j][0]:=-1;\ %
\9 up left  or up right\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{tilepartcolor}[i][j][1]:=-1;\ %
\9 middle\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{tilepartcolor}[i][j][2]:=-1;\ %
\9 down right or down left\par
\quad\quad\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 The macro $\\{TileColors}$ allows to define or redefine the colors.\par
$\9 Only two colors are necessary.\par
\BL
$\2{def}\\{TileColors}(\2{expr}\\{ca},\\{cb})=$\par
\quad\quad\quad$\2{def}\\{partcolor}(\2{expr}c)=$\par
\quad\quad\quad\quad\quad\quad$\2{if}c=0\?:\\{ca}$\par
\quad\quad\quad\quad\quad\quad$\2{else}\?:\\{cb}$\par
\quad\quad\quad\quad\quad\quad$\!\3{fi}$\par
\quad\quad\quad$\!\3{enddef};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 The default colors are $\\{red}$ and $(1,1,0)$ (in RGB coordinates)\par
$\\{TileColors}(\\{red},(1,1,0));$\par
\BL
$\9 A useful color. We might also have used the \.{color} type.\par
$\2{def}\\{yellow}=(1,1,0)\3{enddef};\ \9 RGB definition\par
\BL
$\9 The basic pattern is the following quartercircle;\par
$\9 It is used later on, shifted and rotated.\par
$\2{def}\\{define\_basetile}(\2{expr}u)=$\par
\quad\quad\quad\quad$\2{path}\\{upperleft};$\par
\quad\quad\quad\quad$\\{upperleft}:=((0,0.5)\{\\{right}\}\8{..}\{\\{up}%
\}(0.5,1))\6{scaled}u;$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 Here, we define the paths corresponding to the upper left and middle\par
$\9 parts of the tile 0.\par
\BL
$\2{def}\\{define\_basetilepart}(\2{expr}u)=$\par
\quad\quad\quad\quad$\2{path}\\{tilepart}[];$\par
\quad\quad\quad\quad$\9 left upper corner of a standard tile\par
\quad\quad\quad\quad$\\{tilepart}_{0}:=((0,0.5)\{\\{right}\}\8{..}\{\\{up}%
\}(0.5,1)\8{--}(0,1)\8{--}\1{cycle})\6{scaled}u;$\par
\quad\quad\quad\quad$\9 middle part of the standard tile\par
\quad\quad\quad\quad$\\{tilepart}_{1}:=((0,0.5)\{\\{right}\}\8{..}\{\\{up}%
\}(0.5,1)\8{--}(1,1)\8{--}$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad$(1,0.5)\{\\{left}\}\8{..}\{\\{down}\}(0.5,0)\8{--}(0,0)\8{--}\1{cycle})%
\6{scaled}u;$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 The external frame is drawn outside the rectangle (0,0) -- (nx*u,ny*u).\par
$\9 Guess why ?\par
$\2{vardef}\\{draw\_boundaries}(\2{expr}\\{nx},\\{ny})=$\par
\quad\quad\quad\quad$\2{numeric}u,\\{delta};$\par
\quad\quad\quad\quad$u=\\{TilingUnit};\ \\{delta}=.5\\{mm};$\par
\quad\quad\quad\quad$\2{pickup}\5{pencircle}\6{scaled}(2\ast \\{delta});$\par
\quad\quad\quad\quad$\2{draw}(0-\\{delta},0-\\{delta})\8{--}(0-\\{delta},\\{ny}%
\ast u+\\{delta})\8{--}$\par
\quad\quad\quad\quad\quad\quad\quad\quad$(\\{nx}\ast u+\\{delta},\\{ny}\ast u+%
\\{delta})\8{--}(\\{nx}\ast u+\\{delta},0-\\{delta})\8{--}\1{cycle};$\par
\quad\quad\quad\quad$\2{for}i:=1\4{upto}\\{nx}-1\?:\2{draw}(i\ast u,0)\8{--}(i%
\ast u,\\{ny}\ast u);\3{endfor};$\par
\quad\quad\quad\quad$\2{for}i:=1\4{upto}\\{ny}-1\?:\2{draw}(0,i\ast u)\8{--}(%
\\{nx}\ast u,i\ast u);\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Tiling orientation and color initialization: END\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9\vfill\eject\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Computation of line colors: BEGINNING\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9 The macro $\\{compute\_linecolors}$ `computes' the colors of the lines.\par
$\9 As a matter of fact, all what it does is going through all\par
$\9 the tiles, choose a not yet colored line, and follow it on\par
$\9 both directions, and simultaneously coloring it.\par
$\9 For each line a new color is used.\par
$\9\par
$\2{vardef}\\{compute\_linecolors}=$\par
\quad\quad\quad$\2{numeric}k;$\par
\quad\quad\quad$\\{init\_colors};$\par
\quad\quad\quad$\2{for}i:=0\4{upto}\\{Nx}-1\?:$\par
\quad\quad\quad\quad\quad\quad$\2{for}j:=0\4{upto}\\{Ny}-1\?:$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad$\2{forever}\?:$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$k:=\\{not\_colored%
\_direction}(i,j);\ \9 choose a non colored line.\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\2{exitif}k=-1;\ %
\9 there is none, go to the next tile.\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{follow%
\_line}(i,j,k,\\{nextcolor});\ \9 Follow this line and color it with\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad\quad\quad$\9 $\\{nextcolor}$.\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$k:=%
\\{dual}(i,j,k);\ \9 Get the opposite direction.\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\2{if}%
\\{tilecolor}[i][j][k]=-1\?:\9 If it is not yet colored,\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad$\\{follow\_line}(i,j,k,\\{nextcolor});\ \9 follow the line.\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{nextcolor}:=%
\\{nextcolor}+1;$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad\quad\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 This macro gives a direction not yet colored.\par
$\9 If there is none, it yields ${}-1$.\par
$\2{def}\\{not\_colored\_direction}(\2{expr}x,y)=$\par
\quad\quad\quad$\2{if}\\{tilecolor}[x][y][0]=-1\?:0$\par
\quad\quad\quad$\2{elseif}\\{tilecolor}[x][y][1]=-1\?:1$\par
\quad\quad\quad$\2{elseif}\\{tilecolor}[x][y][2]=-1\?:2$\par
\quad\quad\quad$\2{elseif}\\{tilecolor}[x][y][3]=-1\?:3$\par
\quad\quad\quad$\2{else}\?:-1$\par
\quad\quad\quad$\!\3{fi}$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 Colour a line and follow it.\par
$\9 Notice that for each tile except the first one, two directions\par
$\9 must be colored.\par
$\9\par
$\2{vardef}\\{follow\_line}(\2{expr}x,y,d,c)=\9 $x$,$y$ are the beginning
coordinates\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\9 $d$ is the
direction in which we advance\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\9 $c$ is the
color used\par
\quad\quad\quad$\\{tilecolor}[x][y][d]:=c;\ \\{xx}:=x;\ \\{yy}:=y;\ \\{dd}:=d;$%
\par
\quad\quad\quad$\2{forever}\?:$\par
\quad\quad\quad\quad\quad\quad$\2{exitif}\\{last\_tile}(\\{xx},\\{yy},\\{dd});\
\9 are we at an edge ?\par
\quad\quad\quad\quad\quad\quad$\\{next\_tile};\ \9 get the new values of $%
\\{xx}$, $\\{yy}$ and $\\{dd}$.\par
\quad\quad\quad\quad\quad\quad$\\{du}:=\\{dual}(\\{xx},\\{yy},\\{dd});$\par
\quad\quad\quad\quad\quad\quad$\2{exitif}\\{tilecolor}[\\{xx}][\\{yy}][%
\\{du}]>-1;\ \9 are we in a cycle ?\par
\quad\quad\quad\quad\quad\quad$\\{tilecolor}[\\{xx}][\\{yy}][\\{du}]:=c;\ %
\\{tilecolor}[\\{xx}][\\{yy}][\\{dd}]:=c;$\par
\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
\BL
$\9 This macro gives the direction dual to $d$ in a tile.\par
$\2{def}\\{dual}(\2{expr}x,y,d)=$\par
\quad\quad\quad\quad$(((2\ast \\{tile}[x][y])+\2{if}d=0\?:3\2{elseif}d=1\?:2%
\2{elseif}d=2\?:1\2{else}\?:0\3{fi})\6{mod}4)$\par
\quad\quad\quad$\!\3{enddef};$\par
\BL
$\9 This macro is true if a line goes outside the rectangle.\par
$\2{def}\\{last\_tile}(\2{expr}x,y,d)=$\par
\quad\quad\quad$\2{if}(x=0)\6{and}(d=2)\6{or}$\par
\quad\quad\quad\quad\quad\quad$(x=\\{Nx}-1)\6{and}(d=0)\6{or}$\par
\quad\quad\quad\quad\quad\quad$(y=0)\6{and}(d=3)\6{or}$\par
\quad\quad\quad\quad\quad\quad$(y=\\{Ny}-1)\6{and}(d=1)\?:\5{true}$\par
\quad\quad\quad$\2{else}\?:\5{false}$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\2{def}\\{next\_tile}=$\par
\quad\quad\quad$\2{if}\\{dd}=0\?:\\{xx}:=\\{xx}+1;\ \\{dd}:=(1+2\ast \\{tile}[%
\\{xx}][\\{yy}])\6{mod}4;$\par
\quad\quad\quad$\2{elseif}\\{dd}=1\?:\\{yy}:=\\{yy}+1;\ \\{dd}:=(0+2\ast %
\\{tile}[\\{xx}][\\{yy}])\6{mod}4;$\par
\quad\quad\quad$\2{elseif}\\{dd}=2\?:\\{xx}:=\\{xx}-1;\ \\{dd}:=(3+2\ast %
\\{tile}[\\{xx}][\\{yy}])\6{mod}4;$\par
\quad\quad\quad$\2{else}\?:\\{yy}:=\\{yy}-1;\ \\{dd}:=(2+2\ast \\{tile}[%
\\{xx}][\\{yy}])\6{mod}4;$\par
\quad\quad\quad$\!\3{fi}$\par
\quad\quad$\!\3{enddef};$\par
\BL
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Computation of line colors: END\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9\vfill\eject\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Drawing of the colored lines: BEGINNING\par
$%
\9-----------------------------------------------------------------------------%
\par
\BL
$\9 For each tile, we color the lines.\par
$\2{vardef}\\{draw\_coloredlines}(\2{expr}\\{nx},\\{ny})=$\par
\quad\quad\quad$\2{save}u;$\par
\quad\quad\quad$u=\\{TilingUnit};$\par
\quad\quad\quad$\2{pickup}\5{pencircle}\6{scaled}\\{TilingLineWidth};$\par
\quad\quad\quad$\2{for}i:=0\4{upto}\\{nx}-1\?:$\par
\quad\quad\quad\quad\quad\quad$\2{for}j:=0\4{upto}\\{ny}-1\?:$\par
\quad\quad\quad\quad\quad\quad\quad\quad$\\{color\_thetile}(i,j);\ \9 here we
draw the colored tiles.\par
\quad\quad\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 This macro just draws the left and right part of a tile.\par
$\2{def}\\{color\_thetile}(\2{expr}x,y)=$\par
\quad\quad\quad\quad$\\{draw\_upperleft}(x,y,\\{tile}[x][y]\ast 90,%
\\{tilecolor}[x][y][2]);$\par
\quad\quad\quad\quad$\\{draw\_upperleft}(x,y,180+90\ast \\{tile}[x][y],%
\\{tilecolor}[x][y][0]);$\par
\quad\quad\quad$\!\3{enddef};$\par
\BL
$\9 The macro $\\{draw\_upperleft}$ draws the $\\{upperleft}$ path, rotated by
angle $a$,\par
$\9 and with a color depending on $c$.\par
\BL
$\2{def}\\{draw\_upperleft}(\2{expr}x,y,a,c)=$\par
\quad\quad\quad$\2{draw}\\{upperleft}\6{shifted}(-.5u,-.5u)\6{rotated}a$\par
\quad\quad\quad\quad\quad${}\6{shifted}(.5u,.5u)\6{shifted}((x,y)\ast u)$\par
\quad\quad\quad\quad\quad$\\{withcolor}(\\{linecolor}(c));$\par
\quad\quad\quad$\!\3{enddef};$\par
\BL
$\9 The kind of line color is chosen here; this may be changed.\par
$\2{def}\\{linecolor}(\2{expr}c)=(c/\\{nextcolor})[\\{red},\\{green}]%
\3{enddef};$\par
\BL
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Drawing of the colored lines: END\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9\vfill\eject\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Computation of tile colors: BEGINNING\par
$%
\9-----------------------------------------------------------------------------%
\par
\BL
$\9 In order to color the tiling, we need only two colors.\par
$\9 These colors are numbered $0$ and $1$.\par
$\9 The whole tiling can be performed at once.\par
\BL
$\2{def}\\{compute\_partcolors}=$\par
\quad\quad\quad$\\{init\_partcolors};$\par
\quad\quad\quad$\2{for}i:=0\4{upto}\\{Nx}-1\?:$\par
\quad\quad\quad\quad\quad\quad$\2{for}j:=0\4{upto}\\{Ny}-1\?:$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{fill\_tile}(i,j);$\par
\quad\quad\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 This macro collects the colors of the neighbours (only down and left)\par
$\9 and updates the current tile, where possible.\par
$\9\par
$\2{def}\\{fill\_tile}(\2{expr}x,y)=$\par
\quad\quad\quad$\\{init\_neighbours}(x,y);\ \9 get the color of the
neighbouring tiles\par
\quad\quad\quad$\2{if}\\{tile}[x][y]=0\?:$\par
\quad\quad\quad\quad\quad$\\{handle\_edges}(7)(x,y,0);$\par
\quad\quad\quad\quad\quad$\\{handle\_edges}(5,6)(x,y,1);$\par
\quad\quad\quad\quad\quad$\\{handle\_edges}(4)(x,y,2);$\par
\quad\quad\quad$\2{else}\?:$\par
\quad\quad\quad\quad\quad$\\{handle\_edges}(5,6)(x,y,2);$\par
\quad\quad\quad\quad\quad$\\{handle\_edges}(4,7)(x,y,1);$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad$\\{complete\_tile}(x,y);$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 Neighbours are initialized in an obvious way.\par
$\9 The numbering of the neighbours is dual to the numbering\par
$\9 of edges (see below).\par
$\9 (commented lines are just given for sake of completeness;\par
$\9  they are not actually used)\par
$\9\par
\BL
$\2{def}\\{init\_neighbours}(\2{expr}x,y)=$\par
\quad\quad\quad$\2{for}i:=0\4{upto}7\?:\\{neighbour\_edgecolors}[i]:=-1;%
\3{endfor};$\par
\quad\quad\quad$\2{if}x>0\?:$\par
\quad\quad\quad\quad\quad\quad$\\{neighbour\_edgecolors}[7]:=\\{edge%
\_color}(x-1,y,2);$\par
\quad\quad\quad\quad\quad\quad$\\{neighbour\_edgecolors}[6]:=\\{edge%
\_color}(x-1,y,3);$\par
\quad\quad\quad$\!\3{fi};$\par
$\9      $\2{if}x<\\{Nx}-1\?:\\{neighbour\_edgecolors}[2]:=\\{edge%
\_color}(x+1,y,7);$\par
$\9                $\\{neighbour\_edgecolors}[3]:=\\{edge\_color}(x+1,y,6);%
\3{fi};$\par
\quad\quad\quad$\2{if}y>0\?:$\par
\quad\quad\quad\quad\quad\quad$\\{neighbour\_edgecolors}[5]:=\\{edge%
\_color}(x,y-1,0);$\par
\quad\quad\quad\quad\quad\quad$\\{neighbour\_edgecolors}[4]:=\\{edge%
\_color}(x,y-1,1);$\par
\quad\quad\quad$\!\3{fi};$\par
$\9      $\2{if}y<\\{Ny}-1\?:\\{neighbour\_edgecolors}[0]:=\\{edge%
\_color}(x,y+1,5);$\par
$\9                $\\{neighbour\_edgecolors}[1]:=\\{edge\_color}(x,y+1,4);%
\3{fi};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 This macro spreads the colors of the neighbours in the list $e$\par
$\9 onto the part $f$ of tile $[x,y]$.\par
$\9\par
$\2{def}\\{handle\_edges}(\2{text}e)(\2{expr}x,y,f)=$\par
\quad\quad\quad$\2{forsuffixes}s:=e\?:$\par
\quad\quad\quad\quad\quad\quad$\2{if}\\{neighbour\_edgecolors}[s]>-1\?:$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][f]:=%
\\{neighbour\_edgecolors}[s];$\par
\quad\quad\quad\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 The macro $\\{edge\_color}$ gives the color of an edge, if any.\par
$\9 There are eight edges, and they are numbered from $0$ to $7$.\par
$\9\par
$\9\par
$\9{\os *0 0 0 1 1 1*\par
$\9  7           2\par
$\9  7           2\par
$\9  7           2\par
$\9  6           3\par
$\9  6           3\par
$\9  6           3\par
$\9  *5 5 5 4 4 4*}\par
$\9\par
$\9\par
$\2{def}\\{edge\_color}(\2{expr}x,y,e)=$\par
\quad\quad\quad$\2{if}\\{tile}[x][y]=0\?:$\par
\quad\quad\quad\quad\quad$\2{if}(e=0)\6{or}(e=7)\?:\\{tilepartcolor}[x][y][0];$%
\par
\quad\quad\quad\quad\quad$\2{elseif}(e=3)\6{or}(e=4)\?:%
\\{tilepartcolor}[x][y][2];$\par
\quad\quad\quad\quad\quad$\2{else}\?:\\{tilepartcolor}[x][y][1];$\par
\quad\quad\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad$\2{else}\?:$\par
\quad\quad\quad\quad\quad$\2{if}(e=1)\6{or}(e=2)\?:\\{tilepartcolor}[x][y][0];$%
\par
\quad\quad\quad\quad\quad$\2{elseif}(e=5)\6{or}(e=6)\?:%
\\{tilepartcolor}[x][y][2];$\par
\quad\quad\quad\quad\quad$\2{else}\?:\\{tilepartcolor}[x][y][1];$\par
\quad\quad\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 Since two colors are enough, as soon as one part of a tile\par
$\9 is colored, we can immediately color the others.\par
$\2{def}\\{complete\_tile}(\2{expr}x,y)=$\par
\quad\quad\quad$\2{if}\\{tilepartcolor}[x][y][0]>-1\?:$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][1]:=1-%
\\{tilepartcolor}[x][y][0];$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][2]:=%
\\{tilepartcolor}[x][y][0];$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad$\2{if}\\{tilepartcolor}[x][y][1]>-1\?:$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][0]:=1-%
\\{tilepartcolor}[x][y][1];$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][2]:=1-%
\\{tilepartcolor}[x][y][1];$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad$\2{if}\\{tilepartcolor}[x][y][2]>-1\?:$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][1]:=1-%
\\{tilepartcolor}[x][y][2];$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][0]:=%
\\{tilepartcolor}[x][y][2];$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad\quad$\2{if}\\{tilepartcolor}[x][y][0]=-1\?:\9 this part is only used
at\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad$\9 the very beginning of a tiling, and\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad$\9 serves as initialization.\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][0]:=0;$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][1]:=1;$\par
\quad\quad\quad\quad\quad$\\{tilepartcolor}[x][y][2]:=0;$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Computation of tile colors: END\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9\vfill\eject\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Drawing of tile colors: BEGINNING\par
$%
\9-----------------------------------------------------------------------------%
\par
\BL
$\9 For each tile, we color the three parts.\par
$\2{vardef}\\{draw\_coloredparts}(\2{expr}\\{nx},\\{ny})=$\par
\quad\quad\quad$\2{save}u;$\par
\quad\quad\quad$u=\\{TilingUnit};$\par
\quad\quad\quad$\2{for}i:=0\4{upto}\\{nx}-1\?:$\par
\quad\quad\quad\quad\quad\quad$\2{for}j:=0\4{upto}\\{ny}-1\?:$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{fill%
\_tilepart}(i,j,1);$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{fill%
\_tilepart}(i,j,2);$\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\\{fill%
\_tilepart}(i,j,3);$\par
\quad\quad\quad\quad\quad\quad$\!\3{endfor};$\par
\quad\quad\quad$\!\3{endfor};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 $\\{fill\_tilepart}$ fills part $\\{part}$ at position $x$, $y$.\par
$\2{def}\\{fill\_tilepart}(\2{expr}x,y,\\{part})=$\par
\quad\quad\quad$\2{if}\\{part}=1\?:$\par
\quad\quad\quad\quad\quad\quad$\\{colorpart}(x,y,0,-90\ast \\{tile}[x][y],%
\\{partcolor}(\\{tilepartcolor}[x][y][0]));$\par
\quad\quad\quad$\2{elseif}\\{part}=2\?:$\par
\quad\quad\quad\quad\quad\quad$\\{colorpart}(x,y,1,90\ast \\{tile}[x][y],%
\\{partcolor}(\\{tilepartcolor}[x][y][1]));$\par
\quad\quad\quad$\2{else}\?:$\par
\quad\quad\quad\quad\quad\quad$\\{colorpart}(x,y,0,180-90\ast \\{tile}[x][y],%
\\{partcolor}(\\{tilepartcolor}[x][y][2]));$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$\9 $\\{colorpart}$ colors a part $p$ (0 is corner, 1 is middle)\par
$\9 at position $x$, $y$, using the fundamental\par
$\9 $\\{tilepart}[p]$ pattern, rotated by $a$ degrees and colored according to
$c$.\par
\BL
$\2{vardef}\\{colorpart}(\2{expr}x,y,p,a,c)=$\par
\quad\quad\quad$\2{fill}(\\{tilepart}[p]\6{shifted}(-.5u,-.5u)$\par
\quad\quad\quad\quad\quad\quad${}\6{rotated}a$\par
\quad\quad\quad\quad\quad\quad${}\6{shifted}(.5u,.5u)$\par
\quad\quad\quad\quad\quad\quad${}\6{shifted}((x,y)\ast u))\\{withcolor}(c);$\par
\quad\quad$\!\3{enddef};$\par
\BL
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Drawing of tile colors: END\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9\vfill\eject\par
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Main definition\par
$%
\9-----------------------------------------------------------------------------%
\par
\BL
$\9 This is the main definition.\par
$\9 Two cases are considered: either the lines are colored, and the surfaces\par
$\9 are not filled, or the surfaces are colored, but the lines are not.\par
\BL
$\2{vardef}\\{TruchetTiling}(\2{expr}x,y)=$\par
\quad\quad\quad$\2{save}\\{Nx},\\{Ny};$\par
\quad\quad\quad$\\{Nx}=x;$\par
\quad\quad\quad$\\{Ny}=y;$\par
\quad\quad\quad$\\{init\_tiling}(\\{Nx},\\{Ny});$\par
\quad\quad\quad$\\{draw\_boundaries}(\\{Nx},\\{Ny});$\par
\quad\quad\quad$\2{if}\1{not}\\{TruchetTilingFill}\?:$\par
\quad\quad\quad\quad\quad\quad$\\{define\_basetile}(\\{TilingUnit});$\par
\quad\quad\quad\quad\quad\quad$\2{numeric}\\{nextcolor},\\{tilecolor}[][][];$%
\par
\quad\quad\quad\quad\quad\quad$\\{nextcolor}:=0;$\par
\quad\quad\quad\quad\quad\quad$\\{compute\_linecolors};\ \9 the colors of the
lines are computed here,\par
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad%
\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad\quad$\9 but not drawn;\par
\quad\quad\quad\quad\quad\quad$\\{draw\_coloredlines}(\\{Nx},\\{Ny});$\par
\quad\quad\quad$\2{else}\?:$\par
\quad\quad\quad\quad\quad\quad$\\{define\_basetilepart}(\\{TilingUnit});$\par
\quad\quad\quad\quad\quad\quad$\2{numeric}\\{tilepartcolor}[][][],\\{neighbour%
\_edgecolors}[];$\par
\quad\quad\quad\quad\quad\quad$\\{compute\_partcolors};$\par
\quad\quad\quad\quad\quad\quad$\\{draw\_coloredparts}(\\{Nx},\\{Ny});$\par
\quad\quad\quad$\!\3{fi};$\par
\quad\quad$\!\3{enddef};$\par
\BL
$%
\9-----------------------------------------------------------------------------%
\par
$\9 Global variables and default values\par
$%
\9-----------------------------------------------------------------------------%
\par
\BL
$\2{numeric}\\{tile}[][];$\par
$\2{boolean}\\{TruchetTilingFill};$\par
$\\{TruchetTilingFill}=\5{false};$\par
$\2{numeric}\\{TilingUnit},\\{TilingLineWidth};$\par
$\9 default values\par
$\\{TilingUnit}:=1\\{cm};$\par
$\\{TilingLineWidth}:=1\\{mm};$\par
\BL
$%
\9-----------------------------------------------------------------------------%
\par
$\9 End of file tiling.mp\par
$%
\9-----------------------------------------------------------------------------%
\par
\BL
