# Makie to Simulate Turing Machine Tape

**URL:** https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183
**Category:** General Usage
**Tags:** makie, glmakie, cairomakie
**Created:** [November 27, 2024, 6:19pm UTC](https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183 "2024-11-27T18:19:25Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![CurryFavor](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@CurryFavor](https://discourse.julialang.org/u/CurryFavor)
#### Post date: [November 27, 2024, 6:19pm UTC](https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183/1 "2024-11-27T18:19:25Z")

</div>

Julia Makie supports recording of animated gif, etc. I wish to utilize this feature to create an animation of a Turing machine’s tape. I’d like the tape to appear such as follows (created using HTML) …

> ![image](https://global.discourse-cdn.com/julialang/original/3X/0/a/0afd133e3d7c481cde4db500eb5a5a0bc7271468.png)

My attempt at this in Julia appears below …

```
using CairoMakie
function mainFunction()
  figure = Figure()
  colgap!( figure.layout, 0 )
  Textbox( figure[1,1], placeholder = "☐", bordercolor = :black, cornerradius = 0, borderwidth = 2 )
  Textbox( figure[1,2], placeholder = "A", bordercolor = :black, cornerradius = 0, borderwidth = 2 )
  Textbox( figure[1,3], placeholder = "B", bordercolor = :black, cornerradius = 0, borderwidth = 2 )
  Textbox( figure[1,4], placeholder = "C", bordercolor = :black, cornerradius = 0, borderwidth = 2 )
  Textbox( figure[1,5], placeholder = "☐", bordercolor = :black, cornerradius = 0, borderwidth = 2 )
  save( "Turing_machine_tape.png", figure )
end # function mainFunction
mainFunction()

```

The above Julia program creates an image that suffers from the following “deficiencies” …

- has tape cells farther apart than I would like (as I wish to display as many cells as possible horizontally)
- does not have uniform cell-width (A, B, C cells seem narrower than ◻ cells)
- does not have cell contents in black (as A, B, C, and ◻ appear to be grey)

How may I correct these “deficiencies” in the Julia-generated image?

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [November 29, 2024, 7:00pm UTC](https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183/2 "2024-11-29T19:00:34Z")

</div>

Here’s one way you can do this. I wouldn’t use `Textbox` etc. because those block objects are relatively heavy and not nice for animation. Much easier to handle this with plain plot objects.

```julia
using CairoMakie

f = Figure()
ax = Axis(f[1, 1], autolimitaspect = 1)
hidedecorations!(ax)
hidespines!(ax)

letters = string.('A':'Z')

cellsize = (20, 25)
xs = cumsum(fill(cellsize[1], length(letters)))
poly!(
    ax, [Rect2f((x, 0) .- 0.5 .* cellsize, cellsize) for x in xs],
    color = [i == 10 ? :rosybrown1 : :gray95 for i in eachindex(letters)],
    strokecolor = :black,
    strokewidth = 1
)
text!(
    ax, xs, zeros(length(letters)),
    text = letters,
    align = (:center, :center),
    markerspace = :data
)
scatter!(ax, xs[10], cellsize[2] / 2 + 10, marker = :dtriangle, color = :red)
f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/0/4/04fb1f73f51cbffa0ddea1f8abf21a7b9a9cdb07.png)

---

<div class="post-metadata">

### Author: ![CurryFavor](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@CurryFavor](https://discourse.julialang.org/u/CurryFavor)
#### Post date: [December 1, 2024, 1:04pm UTC](https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183/3 "2024-12-01T13:04:43Z")

</div>

Thank you for this information.

---

<div class="post-metadata">

### Author: ![CurryFavor](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@CurryFavor](https://discourse.julialang.org/u/CurryFavor)
#### Post date: [December 2, 2024, 4:59pm UTC](https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183/4 "2024-12-02T16:59:01Z")

</div>

I am writing programs as I attempt to understand jules’ solution shown above. Below is one of my programs …

```
using CairoMakie
function mainFunction()
  figure = Figure()
  axis = Axis( figure[1,1], autolimitaspect = 1 )
  text!( axis, -5, 0, text = "A", align = (:center, :center), markerspace = :data, fontsize = 1.0 )
  text!( axis, 0, 0, text = "B", align = (:center, :center), markerspace = :data, fontsize = 1.0 )
  text!( axis, +5, 0, text = "C", align = (:center, :center), markerspace = :data, fontsize = 1.0 )
  save( "ABC_ok.png", figure )
end # function mainFunction
mainFunction()

```

The above program yields …

 ![ABC_ok](https://global.discourse-cdn.com/julialang/original/3X/e/0/e023fd67f94c7354e0122ce9f0aa25fbfe411833.png)

However, the following program yields …

`BoundsError: attempt to access 1-element Vector{Makie.GlyphCollection} at index [2]`

```julia
using CairoMakie
function mainFunction()
  figure = Figure()
  axis = Axis( figure[1,1], autolimitaspect = 1 )
  xs = [-5,0,+5]
  ys = [0,0,0]
  text!( axis, xs, ys, text = "ABC", align = (:center, :center), markerspace = :data, fontsize = 1.0 )
  save( "ABC_NOT_ok.png", figure )
end # function mainFunction
mainFunction()

```

* * *

It seems that I am using function `text!` in the same manner as jules, but getting an error. Might someone see where I am going wrong?

* * *

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [December 2, 2024, 5:29pm UTC](https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183/5 "2024-12-02T17:29:05Z")

</div>

You cannot pass a single string for three text positions in an array. In that case you need three strings in an array as well.

---

<div class="post-metadata">

### Author: ![CurryFavor](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@CurryFavor](https://discourse.julialang.org/u/CurryFavor)
#### Post date: [December 2, 2024, 5:43pm UTC](https://discourse.julialang.org/t/makie-to-simulate-turing-machine-tape/123183/6 "2024-12-02T17:43:26Z")

</div>

Thank you. I had (mistakenly) assumed that, when `letters = string.('A':'Z')`, that `typeof(letters)` would be `String`, but I see now that `typeof(letters)` is `Vector{String}`.
