canvas_ant: Draw Langton's Ant

View source: R/canvas_ant.R

canvas_antR Documentation

Draw Langton's Ant

Description

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.

Usage

canvas_ant(
  colors,
  background = "#fafafa",
  iterations = 1000000,
  resolution = 500
)

Arguments

colors

a character (vector) specifying the color(s) used for the artwork.

background

a character specifying the color used for the background.

iterations

a positive integer specifying the number of iterations of the algorithm.

resolution

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.

Details

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.

Value

A ggplot object containing the artwork.

Author(s)

Koen Derks, koen-derks@hotmail.com

References

https://en.wikipedia.org/wiki/Langtons_ant

See Also

colorPalette

Examples


set.seed(1)

# Simple example
canvas_ant(colors = colorPalette("house"))



aRtsy documentation built on Aug. 21, 2023, 9:08 a.m.