Function Repository Resource:

PolyominoPlot

Source Notebook

Represent an array as polyomino regions

Contributed by: Bradley Klee

ResourceFunction["PolyominoPlot"][mat]

for matrix mat, aggregates adjacent identical values and plots the result as a configuration of polyominoes.

Details

ResourceFunction["PolyominoPlot"] is similar to ArrayPlot but only draws edges between dissimilar values
The output of PolyominoPlot is a Graphics object.
PolyominoPlot accepts all the options of Graphics, as well as ColorRules and ColorFunction.
PolyominoPlot accepts an additional option "GraphicsStyle", which directly feeds style primitives into the first argument of the underlying Graphics.

Examples

Basic Examples (3) 

Plot a chair tromino next to a monomino:

In[1]:=
ResourceFunction["PolyominoPlot"][{{1, 2}, {1, 1}}]
Out[1]=

Plot two chair trominoes next to three monominoes:

In[2]:=
ResourceFunction["PolyominoPlot"][
 DiamondMatrix[1] + IdentityMatrix[3]]
Out[2]=

A random polyomino plot over four values:

In[3]:=
With[{seed = SeedRandom[123]},
 ResourceFunction["PolyominoPlot"][RandomInteger[{1, 4}, {5, 10}]]]
Out[3]=

Options (2) 

Add color by setting ColorRules:

In[4]:=
With[{seed = SeedRandom[123]},
 ResourceFunction["PolyominoPlot"][RandomInteger[3, {5, 10}],
  ColorRules -> {0 -> Yellow, 1 -> Red, 2 -> Darker[Green, .3], 3 -> Blue}
  ]]
Out[4]=

Colors can also be added by setting ColorFunction:

In[5]:=
With[{seed = SeedRandom[123]},
 ResourceFunction["PolyominoPlot"][RandomInteger[3, {5, 10}],
  ColorFunction -> Function[Hue[#/4]]
  ]]
Out[5]=

Override the edge style interior to Graphics:

In[6]:=
ResourceFunction["PolyominoPlot"][DiamondMatrix[1] + IdentityMatrix[3],
 "GraphicsStyle" -> Directive[Thick, EdgeForm[Red]]]
Out[6]=

Properties and Relations (3) 

PolyominoPlot differs from ArrayPlot in its ability to draw region boundaries:

In[7]:=
With[{seed = SeedRandom[123]},
   #[RandomInteger[3, {5, 10}],
    ColorRules -> {0 -> Yellow, 1 -> Red, 2 -> Darker[Green, .3], 3 -> Blue},
    Apply[Sequence, If[SameQ[#, ArrayPlot],
      {Mesh -> All, MeshStyle -> Black}, {}]]
    ]] & /@ {ResourceFunction["PolyominoPlot"], ArrayPlot}
Out[7]=

Unlike ArrayPlot, colors are not strictly necessary:

In[8]:=
With[{seed = SeedRandom[123]},
   #[RandomInteger[3, {5, 10}],
    ColorFunction -> Function[null, White]
    ]] & /@ {ResourceFunction["PolyominoPlot"], ArrayPlot}
Out[8]=

PolyominoPlot can achieve finer results than ArrayMesh:

In[9]:=
#[{{1, 2, 2, 1}, {1, 1, 1, 1}, {0, 1, 0, 1}}] & /@ {ResourceFunction[
  "PolyominoPlot"], ArrayMesh}
Out[9]=

Neat Examples (2) 

Plot a solution to Scott's pentomino problem:

In[10]:=
With[{sol = {{3, 3, 2, 4, 4, 4, 4, 4}, {3, 2, 2, 2, 5, 5, 5, 6}, {3, 3, 2, 5, 5, 6, 6, 6}, {7, 7, 8, 0, 0, 13, 13, 6}, {7, 7, 8, 0, 0,
      11, 13, 13}, {9, 7, 8, 11, 11, 11, 12, 13}, {9, 8, 8, 11, 10, 12, 12, 12}, {9, 9, 9, 10, 10, 10, 10, 12}}},
 ResourceFunction["PolyominoPlot"][sol, ColorFunction -> Function[
    If[SameQ[#, 0], Gray, Lighter[Hue[#/16], 1/1.5]]],
  "GraphicsStyle" -> Directive[Thick, EdgeForm[Gray]]
  ]
 ]
Out[10]=

Plot a solution to the heart puzzle:

In[11]:=
With[{sol = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 3, 5, 5, 5, 0, 0, 6, 6, 7, 7, 0, 0}, {0, 3, 3, 3, 11, 11, 11, 11, 6, 6, 9, 7, 8, 0}, {0, 4, 3, 12, 11, 10, 10, 10, 10, 9, 9, 9, 8, 0}, {0, 4, 12, 12, 14, 14, 14, 14, 2, 17, 17, 8, 8, 0}, {0, 0, 12, 13, 13, 14, 21, 20, 20, 20, 17, 17, 0, 0}, {0, 0, 13, 13, 13, 1, 21, 20, 18, 20, 19, 17, 0, 0}, {0, 0, 0, 15, 15, 21, 21, 21, 18, 19, 19, 0,
    0, 0}, {0, 0, 0, 0, 15, 22, 22, 22, 18, 19, 0, 0, 0, 0}, {0, 0, 0,
    0, 15, 15, 16, 22, 18, 19, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 16, 16, 22, 18, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 16, 16, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}},
 ResourceFunction["PolyominoPlot"][sol, ColorFunction -> Function[
    If[SameQ[#, 0], White, Lighter[Hue[#/23], 1/1.5]]],
  "GraphicsStyle" -> Directive[Thick, EdgeForm[Gray]]
  ]
 ]
Out[11]=

Publisher

Brad Klee

Version History

  • 1.0.0 – 31 May 2023

Related Resources

License Information