Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

So I had been looking to solve a little tiling problem and Google turned up this Mathematica Journal Article and I noticed the code was both incomplete and incorrect, so... My Question is, What is missing to make this code actually work?

To make this process slightly less time consuming, Here's a rough interpretation of the journal post in actual code form. Some things were changed that were obvious errors like undeclared variables and punctuation. Also there were functions inside of functions, and where it was clear (to me) that method was wrong, they were moved.

lexic[p_] := Sort[p, (Im[#1] < Im[#2]) || ((Im[#1] == Im[#2]) && (Re[#1] <= Re[#2])) &];

tess[{n_, m_}, poly_, justOneSolution_: False] := Module[{avail, pieces, i, j, ans = {}, tessAux, na, ma},tessAux[partial_] := Module[{f, c, candidates, newp, k},
candidates = Complement[avail, Flatten@partial];
If[candidates == {},
AppendTo[ans, partial]; If[justOneSolution, Throw[1]],
k = First@lexic@candidates;
Map[(newp = k + # - First[#];
 If[(Complement[newp, avail] == {}) && (f = 
     Flatten[{partial, newp}];
    Length@f == Length@Union@f), 
  tessAux[Append[partial, newp]]]) &, pieces]]];
{na, ma} = If[n < m, {m, n}, {n, m}];
pieces = lexic /@ Union[Flatten[pieces /@ poly, i]];
avail = Flatten[Table[i + j*i, {j, 0, na - i}, {i, 0, ma - 1}]];
Catch[tessAux[{}];
If[n < m, Map[m - 1 + i # &, ans], ans]];

getLines[tiling_] := Module[{p},
Partition[Flatten[Map[(p = #; 
Map[{If[Not[MemberQ[p, # + 1]], {# + 1 + i}, {}],
If[Not[MemberQ[p, # + i]], {# + i, # + 1 + i}, {}]} &, p]) &, tiling]], 2]];

tile[{n_, m_}, poly_, r_, justOneSolution_: False] := Module[{t, u, g},
t = tess[{n, m}, poly, justOneSolution];
g = Map[Graphics[Append[{{LightBlue, Rectangle[{0, 0}, {m, n}]},
Line[{{0, n}, {0, 0}, {m, 0}}]},
lexic /@ getLines[#]]] &, t];
Show[GraphicsArray[Partition[If[Mod[Length[t], r] == 0, g,     
Join[g,Table[Graphics[Point[{0, 0}]], {r - Mod[Length[t], r]}]]], r]]]

]

share|improve this question

1 Answer 1

up vote 11 down vote accepted

In the original article there is a semi-colon after the final Show in tile. Remove that semi-colon and the program should run normally.

While you're at it, replace GraphicsArray with GraphicsGrid.

You probably also noted that the article uses unusual color names that Mathematica does not recognize. (The package, Graphics Colors is not found.) You will need to replace the unknown color names with known ones. This does not apply to the code snippet we focused on below.

Edit: The following code is needed for your code. It is given elsewhere in the article.

polyominoQ[p_] := 
 And @@ ((IntegerQ[Re[#]] && IntegerQ[Im[#]]) & /@ p)
rot[p_?polyominoQ] := I p
ref[p_?polyominoQ] := (# - 2 Re[#]) & /@ p

cyclic[p_] := 
 Module[{i = p, ans = {p}}, 
  While[(i = rot[i]) != p, AppendTo[ans, i]]; ans]

dihedral[p_?polyominoQ] := Flatten[{#, ref[#]} & /@ cyclic[p], 1]

canonical[p_?polyominoQ] := 
 Union[(# - (Min[Re[p]] + Min[Im[p]] I)) & /@ p]

allPieces[p_] := Union[canonical /@ dihedral[p]]

liC[z_] := Line[{Re[#], Im[#]} & /@ z]
polC[z_] := Polygon[{Re[#], Im[#]} & /@ z]

draw[p_?polyominoQ, pr_: All] := 
 Graphics[{{Brown, polC[{#, # + 1, # + 1 + I, # + I}]}, 
     liC[{#, # + 1, # + 1 + I, # + I, #}]} & /@ p, PlotRange -> pr]

This is the part of the code of major interest:

lexic[p_]:=Sort[p,(Im[#1]<Im[#2])||((Im[#1]==Im[#2])&&(Re[#1]<=Re[#2]))&]

tess[{n_,m_},poly_,justOneSolution_:False]:=Module[{avail,pieces,i,j,ans={},tessAux,na,ma},

tessAux[partial_]:=Module[{f,c,candidates,newp,k},
candidates=Complement[avail,Flatten[partial]];
If[candidates=={},AppendTo[ans,partial];If[justOneSolution,Throw[1]],
k=First[lexic[candidates]];
Map[(newp=k+#-First[#];
If[(Complement[newp,avail]=={})&&(f=Flatten[{partial,newp}];Length[f]==Length[Union[f]]),tessAux[Append[partial,newp]]])&,pieces]]];

{na,ma}=If[n<m,{m,n},{n,m}];
pieces=lexic/@Union[Flatten[allPieces/@poly,1]];
avail=Flatten[Table[i+j I,{j,0,na-1},{i,0,ma-1}]];
Catch[tessAux[{}]];
If[n<m,Map[m-1+I #&,ans],ans]]

getLines[tiling_]:=Module[{p},
Partition[Flatten[Map[(p=#;Map[{If[Not[MemberQ[p,#+1]],{#+1,#+1+I},{}],If[Not[MemberQ[p,#+I]],{#+I,#+1+I},{}]}&,p])&,tiling]],2]]

tile[{n_,m_},poly_,r_,justOneSolution_:False]:=Module[{t,u,g},
t=tess[{n,m},poly,justOneSolution];
g=Map[Graphics[Append[{{LightBlue,Rectangle[{0,0},{m,n}]},Line[{{0,n},{0,0},{m,0}}]},liC/@getLines[#]]]&,t];
Show[GraphicsGrid[Partition[If[Mod[Length[t],r]==0,g,Join[g,Table[Graphics[Point[{0,0}]],{r-Mod[Length[t],r]}]]],r]]]]

Testing

tile[{4,3},{{0,I,1}},4]

tiling

share|improve this answer
    
I get an error running this code:GraphicsGrid::list: {} is not a list of lists. >> and Show::gtype: GraphicsGrid is not a type of graphics. >> Also noticing that "allPieces" is undeclared in your code also. –  R Hall Dec 14 '13 at 17:00
    
After your last edit here are the errors: GraphicsGrid::list: {} is not a list of lists. >> and Show::gtype: GraphicsGrid is not a type of graphics. >> –  R Hall Dec 14 '13 at 17:05
    
Hmm. Let me check. BTW, notice that I included some additional code that the main code appears to depend on. It is likely that this was the source of the error. (GraphicsGrid has been available since v. 6.) –  David Carraher Dec 14 '13 at 17:08
    
works now David. Thanks! –  R Hall Dec 14 '13 at 17:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.