Animated image to illustrate sequence of moves

I would like to know if it is possible to show the solutions of this problem in a dynamic way with an animated sequence of images that illustrate the various steps of the solution sequence.
In summary I have a nxn table, I would like to show an animation that somehow highlights the various cells according to a sequence determined by a given vector of positions.

for example, make the following sequence “animate” in the 5x5 table

julia> tab=CartesianIndices((1:n,1:n))
5×5 CartesianIndices{2, Tuple{UnitRange{Int64}, UnitRange{Int64}}}:
 CartesianIndex(1, 1)  CartesianIndex(1, 2)  …  CartesianIndex(1, 5)
 CartesianIndex(2, 1)  CartesianIndex(2, 2)     CartesianIndex(2, 5)
 CartesianIndex(3, 1)  CartesianIndex(3, 2)     CartesianIndex(3, 5)
 CartesianIndex(4, 1)  CartesianIndex(4, 2)     CartesianIndex(4, 5)
 CartesianIndex(5, 1)  CartesianIndex(5, 2)     CartesianIndex(5, 5)

julia> branch1(init)
552-element Vector{Any}:
 CartesianIndex{2}[CartesianIndex(1, 1), CartesianIndex(4, 1), CartesianIndex(4, 4), CartesianIndex(1, 4), CartesianIndex(3, 2), CartesianIndex(3, 5), CartesianIndex(5, 3), CartesianIndex(2, 3), CartesianIndex(4, 5), CartesianIndex(1, 5)  …  CartesianIndex(2, 1), CartesianIndex(4, 3), CartesianIndex(1, 3), CartesianIndex(3, 1), CartesianIndex(3, 4), CartesianIndex(5, 2), CartesianIndex(2, 2), CartesianIndex(2, 5), CartesianIndex(5, 5), CartesianIndex(3, 3)] 

Would something like this suit you?

Plots_scatter_CartesianIndices

4 Likes

Yes. I’m looking for just such a thing.
Was it produced with some package of julia?
Would it be possible to add for each colored dot its position in the sequence? That is 1 for the first, 2 for the second, … nxn for the last?

Using Plots.jl, you could do:

Plots_scatter_CartesianIndices

Code
#  ... resuming here from your code:
n = 5
init = CartesianIndex(1,1)
tab = CartesianIndices((1:n,1:n))
y = branch(init)

using Plots
t0 = Tuple(init)
scatter(vec(Tuple.(tab)), mc=:white, msc=:black, ms=10, lims=(0.5,n+0.5), ratio=1)
anim1 = @animate  for (i,yi) in pairs(y[1])    # consider first sequence y[1] of 25 moves
    scatter!(t0, mc=:grey, msc=:red, ms=10)
    ti = Tuple(yi)
    scatter!(ti, c=:red, msc=:red, ms=10)
    global t0 = ti
    annotate!((t0 .+ (0,-1/n))...,text("$i","Computer Modern",8,:top,:black))
end
gif(anim1, "Plots_scatter_CartesianIndices.gif", fps=1)
1 Like

Which backend are you using with Plots.jl? I don’t see it explicitly defined in your example.

BTW, excellent example and clarity :+1:t4:

When you start with a fresh Julia session, using Plots uses the gr() backend by default, so we can ommit it.