The puzzle

I saw a nice Facebook post by Keith Elliott Peterson about transforming a text string into a Truchet Tiling. He demonstrated the transformation by a nice animation you see above. Can you figure out the exact method he used just by watching the animation? It could take a bit but it is pretty simple. You can pause the video below to observe the details of the puzzle. Read further to see the answer. I thought it’d be pretty easy to demo with Wolfram Language.

The answer

The transformation uses first ASCII to go from text to binary and then a specific rule to go form 0s and 1s to Truchet Tiling. ASCII Conversion Chart looks like the below (this is only part of it):
The decimal number in Wolfram Language is obtained as
In[]:=
ToCharacterCode["h"]
Out[]=
{104}
which is further converted to its binary from (base 2):
In[]:=
IntegerString[104,2]
Out[]=
1101000
Note in the chart binary numbers are padded with zeros on the left to always have 8 digit. Let’s do this for the whole string “hello world”, which gives us a matrix or grid:
a=Characters[StringPadLeft[IntegerString[#,2],8,"0"]]&/@​​ToCharacterCode["hello world"];​​a//Grid
Out[]=
0
1
1
0
1
0
0
0
0
1
1
0
0
1
0
1
0
1
1
0
1
1
0
0
0
1
1
0
1
1
0
0
0
1
1
0
1
1
1
1
0
0
1
0
0
0
0
0
0
1
1
1
0
1
1
1
0
1
1
0
1
1
1
1
0
1
1
1
0
0
1
0
0
1
1
0
1
1
0
0
0
1
1
0
0
1
0
0
To go from 0s and 1s to Truchet Tiling we need first to realize that there are 4 distinct types of tiles. They can be easily depicted by placing quarter-circles into the diagonal corners of a square with the opposite background color:
i=Table[{#,ColorNegate@#}&@Rasterize[Graphics[Disk[#,1/2]&/@k,​​PlotRange->{{0,1},{0,1}}],RasterSize->100{1,1}],​​{k,{{{0,0},{1,1}},{{0,1},{1,0}}}}]//Flatten
Out[]=

,
,
,

The final observation is that type of tile depends not only on whether it is 0 or 1, but also if the sum of indexes of the digits in matrix a is even or odd. In other words the rules of transformation follow Checkerboard pattern:
In[]:=
rules=​​{{x_,y_}/;EvenQ[x+y]&&a[[x,y]]=="0"i[[4]],​​{x_,y_}/;EvenQ[x+y]&&a[[x,y]]=="1"i[[1]],​​{x_,y_}/;OddQ[x+y]&&a[[x,y]]=="0"i[[3]],​​{x_,y_}/;OddQ[x+y]&&a[[x,y]]=="1"i[[2]]};
We now replace digits with pictures and assemble the matrix of images into a single whole image:
In[]:=
ImageAssemble[ReplacePart[a,rules]]
Out[]=

Packing all into a function

This is how to pack all this code in a single function:
In[]:=
ClearAll@textTruchet;​​textTruchet[s_String]:=Module​​a=Characters[StringPadLeft[IntegerString[#,2],8,"0"]]&/@ToCharacterCode[s],​​rules=​​{x_,y_}/;EvenQ[x+y]&&a[[x,y]]=="0"
,​​{x_,y_}/;EvenQ[x+y]&&a[[x,y]]=="1"
,​​{x_,y_}/;OddQ[x+y]&&a[[x,y]]=="0"
,​​{x_,y_}/;OddQ[x+y]&&a[[x,y]]=="1"
,​​ImageAssemble[ReplacePart[a,rules]]
In[]:=
textTruchet["hello world"]
Out[]=

Questions

The transformation can be also reversed. Can you write a function that takes an image and returns the text? Or maybe you have thoughts on simplifying my function? PLease do share in the comments.

CITE THIS NOTEBOOK

Representing or encrypting text with Truchet tiling. Can we reverse or simplify the encryption​
by Vitaliy Kaurov​
Wolfram Community, STAFF PICKS, April 30, 2024
​https://community.wolfram.com/groups/-/m/t/3166774