; TeX output 1995.07.09:18283ڍ&ֶK`y cmr10%UU#:iV%UU9-10June1995(lines)U%UU13-14June1995(surfaces) ]%UU16June1995(commentsandrestructuring)%UU21June1995(somemoreformattingformft).:%UU26June1995(somesimpli cations)%UU9July1995(macrosandexamplesweresplitandcoGdebetterorganized)k%_%UUThisismy rst\large"METAPOSTprogram.qI'mstilllearning!Ĥ%UUThereforelotsofthingsareclumsyandIdon'tpretendthis%UUisanexampleofgoGodUUMETAPOSTprogramming.2V%UUIhadhoweverUUgreatfunwritingit!k%%UUSomehighlightsUUfeE8HC::%UUThecoloringoflinesworkedUUaftercirca4hours b> cmmi10:::&%UUTheonlyrealproblemImetwasthatanin niteloGopoccured9%UUwhenfollowffline5wasfollowingacyclicpath.k%G%UUConcerningthecolorationofsurfaces,IneededsometimeP%UUuntilInoticedthattwocolorsweresucient!]%UUOnceyounoticethisanddrawalltheconclusions,thetaskistrivial..%UU||||||||||||||||||||||||-k%$%UUAtilingNxg !", cmsy10Nyisamatrixoftilesindexedbyx%UUrangingbGetween%UU0andNx81andy.rangingbGetweenUU0andNy1.%UUTwokindsoftilesareconsidered.qW*ewillrefertothemas`tile0'κ%UUand`tile1'.qEachtilehasthreeparts,marked*,+andx.k%k%ЁJ% %?Tile0:3Tile1:"------" %/?"|*?||""| |?*|" %/?"|_/?+_|""|_?+\_|" %/?"|/x|""|x\|" %/?"|___|__|""|__|___|"k%%UU|||||||||||||||||||||||||k%`1*3ڍ&k֫%|||||||||||||||||||||||||{ lVy%UUTilingorientationandcolorinitialization:qBEGINNINGk%|||||||||||||||||||||||||{%DEHQ%UUW*ede nethetilingrandomly.definitىfftiling>pҫ(exprcnx%H|;ny#)UP=for/ZLiUP:=08uptoqnx)!81:2forCZNj۫:=UP08uptoqny)`81:FtileTK[i][j]UP:=c}'oin9;%UUwethrowacoinforeachtile.2endforR m;endfor> k;enddef6O;%UUThe`coin'macrobGehavesUUlikeacoinandyieldsrandomly0or1.;d%UUThisisusedfortheinitialtiling.defc}'oin)=UP oGorT(uniformdeviateD\y2)enddef% ;:!%UUW*elet581mean\nocoloryet".definitىffc}'olorsD=for/ZLiUP:=08uptoqNx*U81:mforvj۫:=0uptoqNy*1:2tile}'colorU1^[i][j][0]UP:=1;!tJ%UUright2tile}'colorU1^[i][j][1]UP:=1;+4K%UUup2tile}'colorU1^[i][j][2]UP:=1;(&%UUleft2tile}'colorU1^[i][j][3]UP:=1;B%UUdownendfor> k;0endfor%;enddef6O;definitىffp}'artcolorsU=for/ZLiUP:=08uptoqNx*U81:<forMZOj۫:=UP08uptoqNy*81:Z tilep}'artcolor2[i][j][0]UP:=1; %UUupleftoruprightZ tilep}'artcolor2[i][j][1]UP:=1;%UUmiddleZ tilep}'artcolor2[i][j][2]UP:=1;*Q%UUdownrightordownleft<endfor\ n;endfor> k;enddef6O;C%UUThemacroTileColors5allowstode neorrede nethecolors.Fs%UUOnlytwoUUcolorsarenecessary*.defTileColorsB (exprcc}'a#;cb ޫ)UP=def1p}'artcolorY(exprcc)UP=<ifG&cUP=0:mc}'a<elseN:TRcb< enddef@O;enddef6O;F4%UUThedefaultcolorsarer}'edand(1;1;0)UU(inRGBcoGordinates)TileColors.o(r}'ed S;(1;1;0));e%UUAusefulcolor.qW*emightalsohaveusedthe*_ccolortypGe.defyellow2=UP(1;1;0)enddef% ;%UURGBde nitioni%UUThebasicpatternisthefollowingquartercircle;ݤ%UUItisusedlateron,shiftedandrotated.defde ne]\ffnnb}'asetileQɫ(exprcu)UP=(pathB1dupp}'erlefth;`2 3ڍ&(upp}'erleftQu:=UP((0;0:5)frightC߷g.. シfup g(0:5;1))8scaledmu; enddef6O;]%UUHere,wede nethepathscorrespGondingtotheupperleftandmiddlez֒%UUpartsofthetile0.defde ne]\ffnnb}'asetilepartc<(exprcu)UP=(pathB1dtilep}'arta[];1%UUleftuppGercornerofastandardtile(tilep}'artG-qٓRcmr70O:=UP((0;0:5)frightC߷g.. シfup g(0:5;1)-UI-(0;1)-UI-cycle#|)8scaledmu;?]%UUmiddlepartofthestandardtile(tilep}'artG-qƮ1O:=UP((0;0:5)frightC߷g.. シfup g(0:5;1)-UI-(1;1)-UI-(1;0:5)fleft~2gUP.. シfdownAg(0:5;0)UP-UI-(0;0)UP-UI-cycle#|)8scaledmu;enddef6O;ݑ%UUTheexternalframeisdrawnoutsidetherectangle(0,0){(nx*u,ny*u)./{%UUGuesswhy?v\rardef#_dr}'awffboundariesk٫(exprcnx%H|;ny#)UP=(n9umericSu;delta>;(uUP=TilingUnit1;mdeltaJ=:5mmc;(pic9kupL)pQencircle{mscaled+(28deltam);(dra9wAo(08deltam;0delta)UP-UI-(0delta;ny\u+delta)UP-UI-P(nx 8u+deltam;ny\u+delta)UP-UI-(nx u+delta;0delta)UP-UI-cycle#|;(for9ZMiUP:=18uptoqnx)!81:mdra9w (iu;0)UP-UI-(iu;ny\u);0endfor%;(for9ZMiUP:=18uptoqny)`81:mdra9w (0;iu)UP-UI-(nx u;iu);0endfor%;enddef6O;k%|||||||||||||||||||||||||{%UUTilingorientationandcolorinitialization:qENDk%|||||||||||||||||||||||||{k%`33ڍ&k֫%|||||||||||||||||||||||||{ *Ս%UUComputationoflinecolors:qBEGINNINGk%|||||||||||||||||||||||||{ %UUThemacroc}'ompute]\ffnnlinecolorsX3`computes'thecolorsofthelines.'%UUAsamatteroffact,allwhatitdoGesisgoingthroughalld%UUthetiles,choGoseanotyetcoloredline,andfollowitonz %UUbGothdirections,andsimultaneouslycoloringit.9s%UUF*oreachlineanewcolorisused.k%v\rardef#_c}'ompute]\ffnnlinecolorsx=ګ=n9umericIkP;initىffc}'olorsK;for/ZLiUP:=08uptoqNx*U81:<forMZOj۫:=UP08uptoqNy*81:Z forev9er}Q:x k:=UPnotىffc}'oloredffdirection^(i;j);W)%UUchoGoseanoncoloredline.x exitif(k=UP1;(%UUthereisnone,gotothenexttile.x followffline^(i;jR;kP;nextc}'olor+׫);C<%UUF*ollowthislineandcoloritwith%UUnextc}'olor+.x k:=UPdualͫ(i;jR;kP);=%UUGettheoppGositedirection.x if&tile}'colorX [i][j][kP]UP=1:-%UUIfitisnotyetcolored,followfflineb(i;jR;kP;nextc}'olor+׫);j%UUfollowtheline.x ~c;x nextc}'olor:=UPnextc}'olor-+81;d endfor r;Fendforf o;(endforH l;enddef6O;V%UUThismacrogivesadirectionnotyetcolored.G%UUIfthereisnone,ityields581.defnotىffc}'oloredffdirectiono?F(exprcx;y[٫)UP=if)&tile}'colorLX[x][y[٫][0]UP=1:m0elseif;lhtile}'color^[x][y[٫][1]UP=1:m1elseif;lhtile}'color^[x][y[٫][2]UP=1:m2elseif;lhtile}'color^[x][y[٫][3]UP=1:m3else0:6O1 enddef6O;THS%UUColouralineandfollowit.r%UUNoticethatforeachtileexceptthe rstone,twodirectionsZ%UUmustbGecolored.k%v\rardef#_followfflineRG(exprcx;y[;d;c)UP=G%UUx,y.arethebGeginningcoordinates"%UUdisthedirectioninwhichweadvqance{-e%UUcisthecolorusedtile}'colorA1\[x][y[٫][d]UP:=c;mxx:=x;myy @:=y;mdd&:=d;forev9erAK:<exitifZ(lastىfftile|)(xx {^;yy _;dd);%UUareweatanedge?<nextىfftilea;Ɨ~%UUgetthenewvqaluesofxx г,yyC-anddd.<duJ:=UPdualͫ(xx {^;yy _;dd);<exitifZ(tile}'color}Y۫[xx {^][yy ][du >]UP>1;%UUareweinacycle?<tile}'color_1_[xx {^][yy ][du >]UP:=c;mtile}'color)Mƫ[xx][yy ][dd Ai]:=c;`4ݍ3ڍ&(endforH l; enddef6O;)/%UUThismacrogivesthedirectiondualtodinatile.defdual&R(exprcx;y[;d)UP=((((28tile[x][y[٫])+if _dUP=0:m3elseif 3}d=1:2elseif 3}d=2:1else:d0 *)8moGdo4)enddef@O;ݫ%UUThismacroistrueifalinegoGesoutsidetherectangle.deflastىfftile5X(exprcx;y[;d)UP=if&_(xUP=0)8and5(d=2)or<(xUP=Nx81)and5(dUP=0)or<(y)=UP0)8and5(d=3)or<(y)=UPNy81)and5(dUP=1):mtrueelse0:6Ofalse $c;enddef6O;defnextىfftile<=if)&dd7w=UP0:mxx:=xx +81;mdd&:=(1+2tile[xx {^][yy ])moGdo4;elseif;lhddJ!=UP1:myy @:=yy&+81;mdd&:=(0+2tile[xx {^][yy ])moGdo4;elseif;lhddJ!=UP2:mxx:=xx 81;mdd&:=(3+2tile[xx {^][yy ])moGdo4;else0:6OyyD":=UPyy&81;mdd&:=UP(2+2tile[xx {^][yy ])moGdo4; enddef6O;k%|||||||||||||||||||||||||{8r%UUComputationoflinecolors:qENDk%|||||||||||||||||||||||||{k%`5-3ڍ&k֫%|||||||||||||||||||||||||{  j%UUDrawingofthecoloredlines:qBEGINNINGk%|||||||||||||||||||||||||{>%UUF*oreachtile,wecolorthelines.v\rardef#_dr}'awffcoloredlineso*(exprcnx%H|;ny#)UP=sa9ve6.0u;uUP=TilingUnit1;pic9kupB(pQencircleqlscaled+TilingLineWidth*;for/ZLiUP:=08uptoqnx)!81:<forMZOj۫:=UP08uptoqny)`81:Pc}'olorffthetile+*(i;j);3\%UUherewedrawthecoloredtiles.<endfor\ n;endfor> k;enddef6O;Od%UUThismacrojustdrawstheleftandrightpartofatile.defc}'olorffthetileH(exprcx;y[٫)UP=(dr}'awffupperleftg9(x;y[;tileͫ[x][y]890;tile}'color%q[x][y][2]);(dr}'awffupperleftg9(x;y[;1808+90tile[x][y];tile}'color%q[x][y][0]);enddef@O;%UUThemacrodr}'awffupperleftFa߫drawstheupp}'erleft- ˫path,rotatedbyanglea,:%UUandwithacolordepGendingonc.defdr}'awffupperleftSU (exprcx;y[;a;c)UP=dra9w:6upp}'erleftbҺshiftedD(:5u;:5u)8rotated$#a48shiftedS(:5u;:5u)8shifted!((x;y[٫)8u)2withc}'olorZMЫ(line}'color%~&(c));enddef@O;%UUThekindoflinecolorischosenhere;thismaybGechanged.defline}'color9(exprcc)UP=(c=nextc}'olor(W)[r}'ed S;gr}'een)K]enddef% ;k%|||||||||||||||||||||||||{2+%UUDrawingofthecoloredlines:qENDk%|||||||||||||||||||||||||{k%`643ڍ&k֫%|||||||||||||||||||||||||{ .%UUComputationoftilecolors:qBEGINNINGk%|||||||||||||||||||||||||{ݡ%UUInordertocolorthetiling,weneedonlytwocolors..p%UUThesecolorsarenumbGeredUU0and1. ݢ%UUThewholetilingcanbGeperformedatonce.defc}'ompute]\ffnnpartcolorsjf«=initىffp}'artcolors\;for/ZLiUP:=08uptoqNx*U81:<forMZOj۫:=UP08uptoqNy*81:d llfftile@(i;j);<endfor\ n;endfor> k;enddef6O;\%UUThismacrocollectsthecolorsoftheneighbGours(onlydownandleft)%UUandupGdatesthecurrenttile,wherepossible.k%def llfftile1޶(exprcx;y[٫)UP=initىffneighb}'ours`f'(x;y[٫);ć%UUgetthecoloroftheneighbGouringtilesif)&tile7rs[x][y[٫]UP=0:2handle]\ffnne}'dgesj\(7)(x;y[;0);2handle]\ffnne}'dgesj\(5;6)(x;y[;1);2handle]\ffnne}'dgesj\(4)(x;y[;2);else0:2handle]\ffnne}'dgesj\(5;6)(x;y[;2);2handle]\ffnne}'dgesj\(4;7)(x;y[;1); $c;c}'omplete]\ffnntileVK%(x;y[٫);enddef6O;%UUNeighbGoursareinitializedinanobviousway*.d%UUThenumbGeringUUoftheneighboursisdualtothenumberingpA>%UUofedges(seebGelow).+%UU(commentedlinesarejustgivenforsakeofcompleteness;S%UUtheyarenotactuallyused)k%definitىffneighb}'oursV(exprcx;y[٫)UP=for/ZLiUP:=08uptoq7:mneighb}'ourffedgecolors`1[i]:=1;0endfor%;if)&xUP>0:<neighb}'ourffedgecolorskʫ[7]UP:=e}'dge]\ffnncolor0R(x81;y[;2);<neighb}'ourffedgecolorskʫ[6]UP:=e}'dge]\ffnncolor0R(x81;y[;3); $c;%UUif|xUPUP0:<neighb}'ourffedgecolorskʫ[5]UP:=e}'dge]\ffnncolor0R(x;y81;0);<neighb}'ourffedgecolorskʫ[4]UP:=e}'dge]\ffnncolor0R(x;y81;1); $c;]%UUif|y)<UPNy81:mneighb}'ourffedgecolors`1[0]UP:=e}'dge]\ffnncolor0R(x;y+1;5);O%UUneighb}'ourffedgecolors][1]UP:=e}'dge]\ffnncolor0R(x;y+81;4);0 ;enddef6O;`G%UUThismacrospreadsthecolorsoftheneighbGoursintheliste`7=3ڍ&M㙫%UUontothepartfhoftile[x;y[٫]. *Սk%defhandle]\ffnne}'dgesK(text*e)(exprcx;y[;f)UP=forsuxesSsUP:=e:<ifG&neighb}'ourffedgecolors[s]UP>1:Z tilep}'artcolor2[x][y[٫][f]UP:=neighb}'ourffedgecolors][s];< Bc;endfor> k;enddef6O;|s%UUThemacroe}'dge]\ffnncolor3Lgivesthecolorofanedge,ifany*.:%UUThereareeightedges,andtheyarenumbGeredfrom0to7.k%k%k%w% *0?00111*w% 792w% 792w% 792w% 693w% 693w% 693w% *5?55444*k%k%defe}'dge]\ffnncolor@w(exprcx;y[;e)UP=if)&tile7rs[x][y[٫]UP=0:2if:_(eUP=0)8or \l(e=7):mtilep}'artcolor;[x][y[٫][0];2elseifLR(eUP=3)8or \l(e=4):mtilep}'artcolor;[x][y[٫][2];2elseD:JQtilep}'artcolorz[x][y[٫][1];2 8c;else0:2if:_(eUP=1)8or \l(e=2):mtilep}'artcolor;[x][y[٫][0];2elseifLR(eUP=5)8or \l(e=6):mtilep}'artcolor;[x][y[٫][2];2elseD:JQtilep}'artcolorz[x][y[٫][1];2 8c; $c;enddef6O;r%UUSincetwoUUcolorsareenough,assoGonasonepartofatilei%UUiscolored,wecanimmediatelycolortheothers.defc}'omplete]\ffnntileK(exprcx;y[٫)UP=if)&tilep}'artcolor^[x][y[٫][0]UP>1:2tilep}'artcolorf.[x][y[٫][1]UP:=18tilep}'artcolor7- [x][y][0];2tilep}'artcolorf.[x][y[٫][2]UP:=tilep}'artcolor8Iy[x][y][0]; $c;if)&tilep}'artcolor^[x][y[٫][1]UP>1:2tilep}'artcolorf.[x][y[٫][0]UP:=18tilep}'artcolor7- [x][y][1];2tilep}'artcolorf.[x][y[٫][2]UP:=18tilep}'artcolor7- [x][y][1]; $c;if)&tilep}'artcolor^[x][y[٫][2]UP>1:2tilep}'artcolorf.[x][y[٫][1]UP:=18tilep}'artcolor7- [x][y][2];2tilep}'artcolorf.[x][y[٫][0]UP:=tilep}'artcolor8Iy[x][y][2]; $c;`8 Ks3ڍ&if)&tilep}'artcolor^[x][y[٫][0]UP=1:3]%UUthispartisonlyusedat d%UUtheverybGeginningofatiling,UUandg:%UUservesasinitialization.2tilep}'artcolorf.[x][y[٫][0]UP:=0;2tilep}'artcolorf.[x][y[٫][1]UP:=1;2tilep}'artcolorf.[x][y[٫][2]UP:=0; $c;enddef6O;k%|||||||||||||||||||||||||{:%UUComputationoftilecolors:qENDk%|||||||||||||||||||||||||{k%`9 V3ڍ&k֫%|||||||||||||||||||||||||{ )%UUDrawingoftilecolors:qBEGINNINGk%|||||||||||||||||||||||||{"q%UUF*oreachtile,wecolorthethreeparts.v\rardef#_dr}'awffcoloredpartsp"(exprcnx%H|;ny#)UP=sa9ve6.0u;uUP=TilingUnit1;for/ZLiUP:=08uptoqnx)!81:<forMZOj۫:=UP08uptoqny)`81:n llfftilep}'art`(i;jR;1);n llfftilep}'art`(i;jR;2);n llfftilep}'art`(i;jR;3);<endfor\ n;endfor> k;enddef6O;=s%UU llfftilep}'art6] llsUUpartp}'artyatpGositionx,y[٫.def llfftilep}'artCQ*(exprcx;y[;p}'art)UP=if)&p}'art>ݫ=UP1:<c}'olorpartcH (x;y[;0;908tile[x][y];p}'artcolor*/(tilep}'artcolor4)[x][y][0]));elseif;lhp}'artP=UP2:<c}'olorpartcH (x;y[;1;908tile[x][y];p}'artcolor*/(tilep}'artcolor4)[x][y][1]));else0:<c}'olorpartcH (x;y[;0;180890tile[x][y];p}'artcolor*/(tilep}'artcolor4)[x][y][2])); $c;enddef6O;Fq%UUc}'olorpart-īcolorsUUapartp(0iscorner,1ismiddle)%UUatpGositionx,y[٫,usingthefundamental,/%UUtilep}'art#~[p]UUpattern,rotatedbyadegreesandcoloredaccordingtoc.v\rardef#_c}'olorpartJ(exprcx;y[;p;a;c)UP= ll+ZP(tilep}'art)[p]8shifted!(:5u;:5u)>8rotated`#a>8shifted](:5u;:5u)>8shifted]((x;y[٫)8u))withc}'olor(M˫(c);enddef6O;k%|||||||||||||||||||||||||{Oq%UUDrawingoftilecolors:qENDk%|||||||||||||||||||||||||{k%10 Y3ڍ&k֫%|||||||||||||||||||||||||{ $%UUMainde nitionk%|||||||||||||||||||||||||{S%UUThisisthemainde nition.%UUTwocasesareconsidered:qeitherthelinesarecolored,andthesurfacesG%UUarenot lled,orthesurfacesarecolored,butthelinesarenot.v\rardef#_T;ruchetTiling_(exprcx;y[٫)UP=sa9ve6.0NxCt;Ny;Nx.=UPx;Ny.=UPy[٫;initىfftilingH(Nx F;Ny);dr}'awffboundariesfF(Nx F;Ny);if)¬:_T;ruchetTilingFillK:<de ne]\ffnnb}'asetilez+ܫ(TilingUnit.u);<n9umericgnextc}'olor ;tile}'color%q[][][];<nextc}'olorg:=UP0;<c}'ompute]\ffnnlinecolors;V%UUthecolorsofthelinesarecomputedhere,: %UUbutnotdrawn;<dr}'awffcoloredlines](Nx F;Ny);else0:<de ne]\ffnnb}'asetilepartP(TilingUnit.u);<n9umericgtilep}'artcolor[][][];neighb}'ourffedgecolors]2ܫ[];<c}'ompute]\ffnnpartcolorss;<dr}'awffcoloredpartsB(Nx F;Ny); $c;enddef6O;k%|||||||||||||||||||||||||{0%UUGlobalvqariablesanddefaultvaluesk%|||||||||||||||||||||||||{n9umeric+tile:[][];bQoolean*S"T;ruchetTilingFillv?;T;ruchetTilingFillOAE=UPfalsepL;n9umeric+TilingUnitZ*;TilingLineWidthKŬ;Y%UUdefaultvqaluesTilingUnit1:=UP1cm z;TilingLineWidthLS:=UP1mmc;k%|||||||||||||||||||||||||{q%UUEndof letiling.mpk%|||||||||||||||||||||||||{11bq;3G 3 cmmi10ٓRcmr7K`y cmr10j