This function draws Langton's Ant on a canvas. Langton's Ant is
a two-dimensional cellular automaton that is named after its creator, Chris
Langton. See the Details
section for more specific information about
the algorithm used in this function.
canvas_ant(
colors,
background = "#fafafa",
iterations = 1000000,
resolution = 500
)
a character (vector) specifying the color(s) used for the artwork.
a character specifying the color used for the background.
a positive integer specifying the number of iterations of the algorithm.
resolution of the artwork in pixels per row/column. Increasing the resolution increases the quality of the artwork but also increases the computation time exponentially.
A ggplot
object containing the artwork.
The algorithm for Langton's Ant involves the following steps:
Set up a two-dimensional grid of cells, where each cell can either be "colored" or "non-colored." The initial state of the grid is usually a single non-colored cell in the center of the grid.
Place an "ant" on the grid at the position of the initial non-colored cell. The ant can move in four directions: up, down, left, or right.
At each step of the algorithm, the ant examines the color of the cell it is currently on. If the cell is non-colored, the ant turns 90 degrees clockwise, colors the cell, and moves forward one unit.
If the cell is colored, the ant turns 90 degrees counterclockwise, uncolors the cell, and moves forward one unit.
The ant continues to move around the grid, following these rules at each step. If a certain number of iterations has passed, the ant chooses a different color which corresponds to a different combination of these rules.
colorPalette
# \donttest{
set.seed(1)
# Simple example
canvas_ant(colors = colorPalette("house"))
# }