# Penrose Kite and Dart Tilings with Haskell Diagrams

Revised version (no longer the full program in this literate Haskell)

Infinite non-periodic tessellations of Roger Penrose’s kite and dart tiles.

As part of a collaboration with Stephen Huggett, working on some mathematical properties of Penrose tilings, I recognised the need for quick renderings of tilings. I thought *Haskell diagrams* would be helpful here, and that turned out to be an excellent choice. Two dimensional vectors were well-suited to describing tiling operations and these are included as part of the diagrams package.

This literate Haskell uses the Haskell diagrams package to draw tilings with kites and darts. It also implements the main operations of `inflate`

and `decompose`

which are essential for constructing tilings (explained below).

Firstly, these 5 lines are needed in Haskell to use the diagrams package:

```
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
```

```
import Diagrams.Prelude
import Diagrams.Backend.SVG.CmdLine
```

and we will also import a module for half tiles (explained later)

`import HalfTile`

## Legal tilings

These are the kite and dart tiles.

The red line marking here on the right hand copies, is purely to illustrate rules about how tiles can be put together for legal (non-periodic) tilings. Obviously edges can only be put together when they have the same length. If all the tiles are marked with red lines as illustrated on the right, the vertices where tiles meet must all have a red line or none must have a red line at that vertex. This prevents us from forming a simple rombus by placing a kite top at the base of a dart and thus enabling periodic tilings.

All edges are powers of the golden section which we write as `phi`

.

```
phi::Double
phi = (1.0 + sqrt 5.0) / 2.0
```

So if the shorter edges are unit length, then the longer edges have length `phi`

. We also have the interesting property of the golden section that and so , and .

All angles in the figures are multiples of `tt`

which is `36 deg`

or `1/10 turn`

. We use `ttangle`

to express such angles (e.g 180 degrees is `ttangle 5`

).

```
ttangle:: Int -> Angle Double
ttangle n = (fromIntegral (n `mod` 10))*^tt
where tt = 1/10 @@ turn
```

## Pieces

In order to implement `inflate`

and `decompose`

, we need to work with half tiles. We now define these in the separately imported module `HalfTile`

with constructors for Left Dart, Right Dart, Left Kite, Right Kite

```
data HalfTile rep = LD rep -- defined in HalfTile module
| RD rep
| LK rep
| RK rep
```

where `rep`

is a type variable allowing for different representations. However, here, we want to use a more specific type which we will call `Piece`

:

`type Piece = HalfTile (V2 Double)`

where the half tiles have a simple 2D vector representation to provide orientation and scale. The vector represents the *join* edge of each half tile where halves come together. The origin for a dart is the tip, and the origin for a kite is the acute angle tip (marked in the figure with a red dot).

These are the only 4 pieces we use (oriented along the x axis)

```
ldart,rdart,lkite,rkite:: Piece
ldart = LD unitX
rdart = RD unitX
lkite = LK (phi*^unitX)
rkite = RK (phi*^unitX)
```

Perhaps confusingly, we regard left and right of a dart differently from left and right of a kite when viewed from the origin. The diagram shows the left dart before the right dart and the left kite before the right kite. Thus in a complete tile, going clockwise round the origin the right dart comes before the left dart, but the left kite comes before the right kite.

When it comes to drawing pieces, for the simplest case, we just want to show the two tile edges of each piece (and not the join edge). These edges are calculated as a list of 2 new vectors, using the join edge vector `v`

. They are ordered clockwise from the origin of each piece

```
pieceEdges:: Piece -> [V2 Double]
pieceEdges (LD v) = [v',v ^-^ v'] where v' = phi*^rotate (ttangle 9) v
pieceEdges (RD v) = [v',v ^-^ v'] where v' = phi*^rotate (ttangle 1) v
pieceEdges (RK v) = [v',v ^-^ v'] where v' = rotate (ttangle 9) v
pieceEdges (LK v) = [v',v ^-^ v'] where v' = rotate (ttangle 1) v
```

Now drawing lines for the 2 outer edges of a piece is simply

```
drawPiece:: Piece -> Diagram B
drawPiece = strokeLine . fromOffsets . pieceEdges
```

It is also useful to calculate a list of the 4 tile edges of a completed half-tile piece clockwise from the origin of the tile. (This is useful for colour filling a tile)

```
tileEdges:: Piece -> [V2 Double]
tileEdges (LD v) = pieceEdges (RD v) ++ map negated (reverse (pieceEdges (LD v)))
tileEdges (RD v) = tileEdges (LD v)
tileEdges (LK v) = pieceEdges (LK v) ++ map negated (reverse (pieceEdges (RK v)))
tileEdges (RK v) = tileEdges (LK v)
```

To fill whole tiles with colours, darts with `dcol`

and kites with `kcol`

we can use `fillDK'`

. This uses only the left pieces to identify the whole tile and ignores right pieces so that a tile is not filled twice.

```
fillDK':: Colour Double -> Colour Double -> Piece -> Diagram B
fillDK' dcol kcol c =
case c of (LD _) -> (strokeLoop $ glueLine $ fromOffsets $ tileEdges c) # fc dcol
(LK _) -> (strokeLoop $ glueLine $ fromOffsets $ tileEdges c) # fc kcol
_ -> mempty
```

To show half tiles separately, we can use `drawJPiece`

which adds the join edge of each half tile to make loops with 3 edges.

```
drawJPiece:: Piece -> Diagram B
drawJPiece = strokeLoop . closeLine . fromOffsets . pieceEdges
```

For an alternative fill operation we can use `drawJPiece`

with the line colour on the join edge matching the fill colour.

```
fillDK:: Colour Double -> Colour Double -> Piece -> Diagram B
fillDK dcol kcol piece = drawPiece piece <> (drawJPiece piece # fc col # lc col) where
col = case piece of (LD _) -> dcol
(RD _) -> dcol
(LK _) -> kcol
(RK _) -> kcol
```

By making `Pieces`

transformable we can reuse generic transform operations. These 4 lines of code are required to do this

```
type instance N (HalfTile a) = N a
type instance V (HalfTile a) = V a
instance Transformable a => Transformable (HalfTile a) where
transform t ht = fmap (transform t) ht
```

So we can also scale a piece, translate a piece by a vector, and rotate a piece by an angle. (Positive rotations are in the anticlockwise direction.)

```
scale:: Double -> Piece -> Piece
rotate :: Angle Double -> Piece -> Piece
translate:: V2 Double -> Piece -> Piece
```

## Patches

A patch is a list of located pieces (each with a 2D point)

`type Patch = [Located Piece]`

To turn a whole patch into a diagram using some function `cd`

for drawing the pieces, we use

`patchWith cd patch = position $ fmap (viewLoc . mapLoc cd) patch`

Here `mapLoc`

applies a function to the piece in a located piece – producing a located diagram in this case, and `viewLoc`

returns the pair of point and diagram from a located diagram. Finally `position`

forms a single diagram from the list of pairs of points and diagrams.

The common special case `drawPatch`

uses `drawPiece`

on each piece

`drawPatch = patchWith drawPiece`

Patches are automatically inferred to be transformable now Pieces are transformable, so we can also scale a patch, translate a patch by a vector, and rotate a patch by an angle.

```
scale :: Double -> Patch -> Patch
rotate :: Angle Double -> Patch -> Patch
translate:: V2 Double -> Patch -> Patch
```

As an aid to creating patches with 5-fold rotational symmetry, we combine 5 copies of a basic patch (rotated by multiples of `ttangle 2`

successively).

```
penta:: Patch -> Patch
penta p = concatMap copy [0..4]
where copy n = rotate (ttangle (2*n)) p
```

This must be used with care to avoid nonsense patches. But two special cases are

```
sun,star::Patch
sun = penta [rkite `at` origin, lkite `at` origin]
star = penta [rdart `at` origin, ldart `at` origin]
```

This figure shows some example patches, drawn with `drawPatch`

The first is a `star`

and the second is a `sun`

.

The tools so far for creating patches may seem limited (and do not help with ensuring legal tilings), but there is an even bigger problem.

## Correct Tilings

Unfortunately, correct tilings – that is, tilings which can be extended to infinity – are not as simple as just legal tilings. It is not enough to have a legal tiling, because an apparent (legal) choice of placing one tile can have non-local consequences, causing a conflict with a choice made far away in a patch of tiles, resulting in a patch which cannot be extended. This suggests that constructing correct patches is far from trivial.

The infinite number of possible infinite tilings do have some remarkable properties. Any finite patch from one of them, will occur in all the others (infinitely many times) and within a relatively small radius of any point in an infinite tiling. (For details of this see links at the end)

This is why we need a different approach to constructing larger patches. There are two significant processes used for creating patches, namely `inflate`

(also called *compose*) and `decompose`

.

To understand these processes, take a look at the following figure.

Here the *small* pieces have been drawn in an unusual way. The edges have been drawn with dashed lines, but long edges of kites have been emphasised with a solid line and the join edges of darts marked with a red line. From this you may be able to make out a patch of larger scale kites and darts. This is an inflated patch arising from the smaller scale patch. Conversely, the larger kites and darts decompose to the smaller scale ones.

## Decomposition

Since the rule for decomposition is uniquely determined, we can express it as a simple function on patches.

```
decompose :: Patch -> Patch
decompose = concatMap decompPiece
```

where the function `decompPiece`

acts on located pieces and produces a list of the smaller located pieces contained in the piece. For example, a larger right dart will produce both a smaller right dart and a smaller left kite. Decomposing a located piece also takes care of the location, scale and rotation of the new pieces.

```
decompPiece lp = case viewLoc lp of
(p, RD vd)-> [ LK vd `at` p
, RD vd' `at` (p .+^ v')
] where v' = phi*^rotate (ttangle 1) vd
vd' = (2-phi) *^ (negated v') -- (2-phi) = 1/phi^2
(p, LD vd)-> [ RK vd `at` p
, LD vd' `at` (p .+^ v')
] where v' = phi*^rotate (ttangle 9) vd
vd' = (2-phi) *^ (negated v') -- (2-phi) = 1/phi^2
(p, RK vk)-> [ RD vd' `at` p
, LK vk' `at` (p .+^ v')
, RK vk' `at` (p .+^ v')
] where v' = rotate (ttangle 9) vk
vd' = (2-phi) *^ v' -- v'/phi^2
vk' = ((phi-1) *^ vk) ^-^ v' -- (phi-1) = 1/phi
(p, LK vk)-> [ LD vd' `at` p
, RK vk' `at` (p .+^ v')
, LK vk' `at` (p .+^ v')
] where v' = rotate (ttangle 1) vk
vd' = (2-phi) *^ v' -- v'/phi^2
vk' = ((phi-1) *^ vk) ^-^ v' -- (phi-1) = 1/phi
```

This is illustrated in the following figure for the cases of a right dart and a right kite.

The symmetric diagrams for left pieces are easy to work out from these, so they are not illustrated.

With the `decompose`

operation we can start with a simple correct patch, and decompose repeatedly to get more and more detailed patches. (Each decomposition scales the tiles down by a factor of but we can rescale at any time.)

This figure illustrates how each piece decomposes with 4 decomposition steps below each one.

```
thePieces = [ldart, rdart, lkite, rkite]
fourDecomps = hsep 1 $ fmap decomps thePieces # lw thin where
decomps pc = vsep 1 $ fmap drawPatch $ take 5 $ decompositions [pc `at` origin]
```

We have made use of the fact that we can create an infinite list of finer and finer decompositions of any patch, using:

```
decompositions:: Patch -> [Patch]
decompositions = iterate decompose
```

We could get the n-fold decomposition of a patch as just the nth item in a list of decompositions.

For example, here is an infinite list of decomposed versions of `sun`

.

`suns = decompositions sun`

The coloured tiling shown at the beginning is simply 6 decompositions of `sun`

displayed using `fillDK'`

```
sun6 = suns!!6
filledSun6 = patchWith (fillDK' red blue) sun6 # lw ultraThin
```

The earlier figure illustrating larger kites and darts emphasised from the smaller ones is also `sun6`

but this time drawn with

`experimentFig = patchWith experiment sun6 # lw thin`

where pieces are drawn with

```
experiment:: Piece -> Diagram B
experiment pc = emph pc <> (drawJPiece pc # dashingN [0.002,0.002] 0 # lw ultraThin)
where emph pc = case pc of
(LD v) -> (strokeLine . fromOffsets) [v] # lc red -- emphasise join edge of darts in red
(RD v) -> (strokeLine . fromOffsets) [v] # lc red
(LK v) -> (strokeLine . fromOffsets) [rotate (ttangle 1) v] -- emphasise long edge for kites
(RK v) -> (strokeLine . fromOffsets) [rotate (ttangle 9) v]
```

## Inflation

You might expect inflation (also called *composition*) to be a kind of inverse to decomposition, but it is a bit more complicated than that. With our current representation of pieces, we can only inflate single pieces. This amounts to embedding the piece into a larger piece that matches how the larger piece decomposes. There is thus a choice at each inflation step as to which of several possibilities we select as the larger half-tile. We represent this choice as a list of alternatives. This list should not be confused with a patch. It only makes sense to select one of the alternatives giving a new single piece.

The earlier diagram illustrating how decompositions are calculated also shows the two choices for inflating a right dart into either a right kite or a larger right dart. There will be two symmetric choices for a left dart, and three choices for left and right kites.

Once again we work with located pieces to ensure the resulting larger piece contains the original in its original position in a decomposition.

```
inflate :: Located Piece -> [Located Piece]
inflate lp = case viewLoc lp of
(p, RD vd)-> [ RD vd' `at` (p .+^ v')
, RK vk `at` p
] where v' = (phi+1) *^ vd -- vd*phi^2
vd' = rotate (ttangle 9) (vd ^-^ v')
vk = rotate (ttangle 1) v'
(p, LD vd)-> [ LD vd' `at` (p .+^ v')
, LK vk `at` p
] where v' = (phi+1) *^ vd -- vd*phi^2
vd' = rotate (ttangle 1) (vd ^-^ v')
vk = rotate (ttangle 9) v'
(p, RK vk)-> [ LD vk `at` p
, LK lvk' `at` (p .+^ lv')
, RK rvk' `at` (p .+^ rv')
] where lv' = phi*^rotate (ttangle 9) vk
rv' = phi*^rotate (ttangle 1) vk
rvk' = phi*^rotate (ttangle 7) vk
lvk' = phi*^rotate (ttangle 3) vk
(p, LK vk)-> [ RD vk `at` p
, RK rvk' `at` (p .+^ rv')
, LK lvk' `at` (p .+^ lv')
] where v0 = rotate (ttangle 1) vk
lv' = phi*^rotate (ttangle 9) vk
rv' = phi*^rotate (ttangle 1) vk
rvk' = phi*^rotate (ttangle 7) vk
lvk' = phi*^rotate (ttangle 3) vk
```

As the result is a list of alternatives, we need to select one to do further inflations. We can express all the alternatives after n steps as `inflations n`

where

```
inflations :: Int -> Located Piece -> [Located Piece]
inflations 0 lp = [lp]
inflations n lp = do
lp' <- inflate lp
inflations (n-1) lp'
```

This figure illustrates 5 consecutive choices for inflating a left dart to produce a left kite. On the left, the finishing piece is shown with the starting piece embedded, and on the right the 5-fold decomposition of the result is shown.

```
fiveInflate = hsep 1 $ fmap drawPatch [[ld,lk'], multiDecomp 5 [lk']] where -- two separate patches
ld = (ldart `at` origin)
lk = inflate ld !!1
rk = inflate lk !!1
rk' = inflate rk !!2
ld' = inflate rk' !!0
lk' = inflate ld' !!1
```

Finally, at the end of this literate haskell program we choose which figure to draw as output.

```
fig::Diagram B
fig = filledSun6
```

`main = mainWith fig`

That’s it. But, *What about inflating whole patches?*, I hear you ask. Unfortunately we need to answer questions like what pieces are adjacent to a piece in a patch and whether there is a corresponding other half for a piece. These cannot be done with our simple vector representations. We would need some form of planar graph representation, which is much more involved. That is another story.

Many thanks to Stephen Huggett for his inspirations concerning the tilings. A library version of the above code is available on GitHub

## Further reading on Penrose Tilings

As well as the Wikipedia entry Penrose Tilings I recommend two articles in Scientific American from 2005 by David Austin Penrose Tiles Talk Across Miles and Penrose Tilings Tied up in Ribbons.

There is also a very interesting article by Roger Penrose himself: Penrose R *Tilings and quasi-crystals; a non-local growth problem?* in Aperiodicity and Order 2, edited by Jarich M, Academic Press, 1989.

More information about the diagrams package can be found from the home page Haskell diagrams