# Drawing hat tiling utilizing Racket

*by*Phil Tadros

### Introduction

*Observe: This blogpost is closely impressed from this wolfram community article*.

Whether or not a single geometrical form exists that may tile aircraft aperiodically was open drawback till not too long ago. Since discovery of hat and associated shapes we all know reply is affirmative. On this weblog we are going to draw tiling of aircraft utilizing hat and its reflection. Hat is a ploykite with `13`

elements, `3`

totally different edge lengths and inside angles multiples of `30`

diploma. Utilizing metapict library, let’s draw canonical hat and its reflection.

```
#lang racket
(require metapict)
(struct H-edge (flip) #:clear)
(struct T1 H-edge () #:clear)
(struct T2 H-edge () #:clear)
(struct T3 H-edge () #:clear)
(outline hat
(listing (T1 -1) (T1 1) (T2 4) (T2 6) (T1 3)
(T1 5) (T2 8) (T2 6) (T1 9) (T1 7)
(T2 10) (T3 12) (T2 14)))
(outline flipped-hat
(listing (T2 -2) (T3 0) (T2 2)
(T1 5) (T1 3) (T2 6) (T2 4) (T1 7)
(T1 9) (T2 6) (T2 8) (T1 11) (T1 1)))
(outline (H-edge-length e)
(cond
[(T1? e) (sqrt 12)]
[(T2? e) 2]
[(T3? e) 4]))
(outline (scan init fn lst)
(if (null? lst)
(listing init)
(cons init
(scan (fn init (first lst)) fn (relaxation lst)))))
(outline (H-edges->curve origo flip edges)
(let ([points (scan origo pt+
(map (lambda (e)
(pt@d (H-edge-length e)
(* 30 (+ turn (H-edge-turn e)))))
edges))])
(apply make-curve (append
(apply append (map (lambda (p) (listing p --))
(drop-right factors 1)))
(listing cycle)))))
(with-window (window -7 7 -7 7) (draw (H-edges->curve (pt 0 0) 0 hat)))
(with-window (window -7 7 -7 7) (draw (H-edges->curve (pt 0 0) 0 flipped-hat)))
```

When above code is run, it’s going to draw shapes proven beneath minus grid and inside being coloured blue.

### Substitution Tiling

Bigger and bigger space of aircraft may be tiled utilizing substitution tiling. In case of hat tiling, substitution is given when it comes to metatiles `H`

, `T`

, `P`

and `F`

. Let’s have a look at them and code used to attract them.

```
(struct M-edge (flip) #:clear)
(struct X+ M-edge () #:clear)
(struct X- M-edge () #:clear)
(struct A+ M-edge () #:clear)
(struct A- M-edge () #:clear)
(struct B+ M-edge () #:clear)
(struct B- M-edge () #:clear)
(struct F+ M-edge () #:clear)
(struct F- M-edge () #:clear)
(struct L M-edge () #:clear)
(outline meta-H
(listing (X+ 0)
(B- 1) (X- 1)
(X+ 2)
(B- 3) (X- 3)
(X+ 4)
(A+ 5) (X- 5)))
(outline meta-T
(listing (A- 0)
(A- 2)
(B+ 4)))
(outline meta-P
(listing (X+ 0) (A- 0)
( L 2) (X- 2)
(X+ 3) (B+ 3)
( L 5) (X- 5)))
(outline meta-F
(listing (X+ 0) (L 0) (X- 0)
(F+ 1)
(F- 2)
(X+ 3) (B+ 3)
( L 5) (X- 5)))
(outline (M-edge-length e)
(cond
[(or (A+? e) (A-? e) (B+? e) (B-? e)) 12]
[else 4]))
(outline (M-edges->curve origo flip edges)
(let ([points (scan origo pt+
(map (lambda (e)
(pt@d (M-edge-length e)
(* 60 (+ turn (M-edge-turn e)))))
edges))])
(apply make-curve (append
(apply append (map (lambda (p) (listing p --))
(drop-right factors 1)))
(listing cycle)))))
(with-window (window -20 20 -20 20) (draw (M-edges->curve (pt 0 0) 0 meta-H)))
(with-window (window -20 20 -20 20) (draw (M-edges->curve (pt 0 0) 0 meta-T)))
(with-window (window -20 20 -20 20) (draw (M-edges->curve (pt 0 0) 0 meta-P)))
(with-window (window -20 20 -20 20) (draw (M-edges->curve (pt 0 0) 0 meta-F)))
```

Discover that there’s mismatch in quantity between edges in image and code. Additionally there are two variants, optimistic and adverse, of edge varieties (`X+`

, `X-`

and so on) besides `L`

. Why such oddities can be clear when one appears to be like how hats are projected into these metatiles.

Edges of metatiles are deconstructed into elements and sort and attribute (optimistic or adverse) are given to them. Optimistic or adverse label will depend on whether or not elements of hats are bulging out or deflating in round metatile edges. Substitution rule ought to line up `A+`

and `A-`

, `B+`

and `B-`

, `X+`

and `X-`

so on. That is required in order that no hole stays in tiling. Subsequent let’s have a look at substitution guidelines of metatile `H`

, `T`

, `P`

and `F`

respectively.

Since projection of hats in metatiles isn’t invariant underneath rotation however `3`

metatiles are, substitution rule must also specify orientation of metatiles. In our case this orientation is specified by native origin of metatile.

Up till now all our drawing is completed at origin `(pt 0 0)`

with out rotation despite the fact that our strategies `H-edges->curve`

and `M-edges->curve`

can account for translation and rotation. To broaden metatiles utilizing substitution rule we would require translation and rotation. We wrap our canonical drawing of metatiles in `p-meta`

construction which incorporates rotation `flip`

and translation `dist`

. Translation isn’t given as single vector however listing of vectors every of which is given when it comes to metatile edges `M-edge`

. Thought is that if we journey alongside these edges from origin we arrive at level which is native origin of wrapped metatile. Additionally discover that attributes (`+`

/ `-`

) of `M-edge`

does n’t matter in translation vector as `+`

and `-`

of similar kind are all the time lined up. Changing `X+`

with `X-`

(or every other such pairing) or vice versa makes no distinction.

```
(struct p-meta
(meta flip dist) #:clear)
(outline meta-H~>
(listing (p-meta meta-H 0 (listing (F- 1) (X+ 2) (B+ 2) (X- 1)))
(p-meta meta-H -2 (listing (F- 1) (X+ 2) (B+ 2) (X- 1)))
(p-meta meta-H 0 (listing (F- 1) (X+ 0) (B- 1) (X- 1)))
(p-meta meta-T 0 (listing (F- 1) (X+ 2) (B+ 2) (X- 1)
(X+ 0)))
(p-meta meta-F -1 (listing (F- 3) (X+ 2) (L 2) (X- 2)))
(p-meta meta-F 1 (listing (F- 1) (X+ 0) (B- 1) (X- 1)
(X+ 0) (L 0) (X- 0)))
(p-meta meta-F 3 (listing (F- 1) (X+ 2) (B+ 2) (X- 1)
(X+ 0) (B- 1) (X- 1) (X+ 2)
(L 2) (X- 2)))
(p-meta meta-P 2 (listing (F- 1) (X+ 2) (B+ 2) (X- 1)))
(p-meta meta-P 1 (listing (F- 1) (X+ 0) (L 0) (X- 0)))
(p-meta meta-P 3 (listing (F- 1) (X+ 0) (B- 1) (X- 1)
(X+ 0) (B- 1) (X- 1) (X+ 2)
(L 2) (X- 2)))))
```

Image beneath exhibits how translation of `H`

metatiles are calculated utilizing `M-edges`

. Discover that first and second `H`

metatile has similar native origin. Second `H`

metatile (backside one) is rotated by `120`

diploma clockwise (`-2 * 60`

).

We now want to put in writing `substitute-many`

in order that we are able to name it successively to get larger and larger substitution. To attain this we want substitution guidelines for `M-edges`

for fixing `dist`

which is given by `M-edge-reps`

.

```
(outline (M-edge-reps e)
(let ([turn (M-edge-turn e)])
(cond
[(A-? e) (list (B- turn) (X- turn) (X+ (+ turn 1)))]
[(A+? e) (list (X- (+ turn 1)) (X+ turn) (B+ turn))]
[(B-? e) (list (X- (+ turn 1)) (X+ turn) (A- turn))]
[(B+? e) (list (A+ turn) (X- turn) (X+ (+ turn 1)))]
[(F-? e) (list (X+ turn) (L turn) (X- turn) (F+ (+ turn 1)))]
[(F+? e) (list (F- (+ turn 1)) (X+ turn) (L turn) (X- turn))]
[(L? e) (list (L (- turn 1)))]
[(X-? e) (list (X- (- turn 1)) (X+ turn) (L turn) (X- turn) (F+ (+ turn 1)))]
[(X+? e) (list (F- (+ turn 1)) (X+ turn) (L turn) (X- turn) (X+ (- turn 1)))])))
(outline (turn-M-edge e t)
(cond
[(A-? e) (A- (+ t (M-edge-turn e)))]
[(A+? e) (A+ (+ t (M-edge-turn e)))]
[(B-? e) (B- (+ t (M-edge-turn e)))]
[(B+? e) (B+ (+ t (M-edge-turn e)))]
[(F-? e) (F- (+ t (M-edge-turn e)))]
[(F+? e) (F+ (+ t (M-edge-turn e)))]
[( L? e) ( L (+ t (M-edge-turn e)))]
[(X-? e) (X- (+ t (M-edge-turn e)))]
[(X+? e) (X+ (+ t (M-edge-turn e)))]))
(outline (substitute pm)
(let ([o-turn (p-meta-turn pm)]
[o-dist (p-meta-dist pm)]
[o-meta (p-meta-meta pm)])
(map
(lambda (p) (struct-copy p-meta p
[turn (+ o-turn (p-meta-turn p))]
[dist (append (apply append (map M-edge-reps o-dist))
(map (lambda (e) (turn-M-edge e o-turn))
(p-meta-dist p)))]))
(cond
[(eq? o-meta meta-H) meta-H~>]
[(eq? o-meta meta-T) meta-T~>]
[(eq? o-meta meta-P) meta-P~>]
[(eq? o-meta meta-F) meta-F~>]))))
(outline (substitute-many p-metatiles)
(apply append (map substitute p-metatiles)))
```

We’ve got all equipment prepared for hat tiling. Code is listed here which additionally consists of features to mission hats into metatiles, which isn’t lined right here.