Skip to contents

Replays the search recorded by solve_maze(trace = TRUE) and writes it to an animated GIF. Each frame shades the cells already expanded, the cells waiting on the frontier, and the cell being expanded, so the characteristic shape of each search becomes visible: A-star's bias towards the goal, Dijkstra's uniform spread, and breadth-first search's expanding rings. The frame titles name whichever algorithm produced the trace. The final frames hold the solved path. The optional magick package is required only when this function is called.

For a trace produced by one of the three bidirectional algorithms ("bidirectional", "bidirectional_dijkstra", "bidirectional_astar"), each step also records which frontier it belongs to (see the side field documented in solve_maze()), and cells touched by the forward search (growing from start) and the backward search (growing from end) are shaded with forward_color and backward_color instead of the usual explored_color / frontier_color pair. A trace from any other algorithm carries no side information and renders exactly as before.

Usage

animate_solution(maze, solution = NULL,
  output_file = "maze-solution.gif", fps = 10, every = 1L,
  width = 800L, height = 800L, wall_width = 3,
  explored_color = "#bae6fd", frontier_color = "#fdba74",
  current_color = "#8b5cf6", forward_color = "#0ea5e9",
  backward_color = "#f43f5e", solution_color = "#f59e0b", hold = 12L,
  progress = interactive())

Arguments

maze

A maze object returned by generate_maze().

solution

Optional result of solve_maze() called with trace = TRUE. When omitted the maze is solved again with tracing enabled.

output_file

Destination path for the GIF.

fps

Frames per second.

every

Render every nth search step. Increase this for large mazes.

width, height

Frame dimensions in pixels.

wall_width

Width of wall lines.

explored_color

Fill for cells that have been expanded. Used for every algorithm except that, for a trace carrying side information (see Details), cells touched by either bidirectional frontier are shaded with forward_color / backward_color instead.

frontier_color

Fill for cells waiting in the open set. Same caveat as explored_color.

current_color

Fill for the cell being expanded.

forward_color

Fill for cells (explored or still on the frontier) belonging to the forward search – the one growing from start – of a bidirectional trace. Ignored for a trace with no side information.

backward_color

Fill for cells belonging to the backward search – the one growing from end – of a bidirectional trace. Ignored for a trace with no side information.

solution_color

Color of the path drawn in the closing frames.

hold

Number of closing frames showing the solved path.

progress

Show a progress bar in interactive sessions.

Value

The normalized output path, invisibly.

See also

animate_maze for the generation animation.

Examples

if (requireNamespace("magick", quietly = TRUE)) {
  maze <- generate_maze(8, 8, seed = 42)
  animate_solution(maze, output_file = tempfile(fileext = ".gif"))
}
#> Animation saved to '/tmp/RtmpFI2xvQ/file19bf4ea85e9c.gif'.