There is a celebrated Commodore 64 program that randomly prints outs / and \
characters and fills the screen with neat-looking maze designs. It is just one
line of code, but there is a whole book written about
it.
10 PRINT CHR$(205.5+RND(1)); : GOTO 10
Screenshots of the 10 PRINT program in action. Images taken from the 10 PRINT book.
The basic idea, from my reading of the code, is that CHR$(205) is \,
CHR$(206) is /, and the program randomly selects between the two by adding a
random number to 205.5. Endlessly looping over this command fills the screen
with that pleasing maze pattern.
In R, we could replicate this functionality with by randomly sampling the
slashes:
sample_n_slashes //////\/\\/\/\/\/\\//\\\\//\//\/\//\///\
#__ \\\/\//\\/\/////////////\\/\//\\\/\//\\\
#__ /\\/\/\//\/\/\\/\\/\/\//\\\\//\/\\\/\///
#__ \\//\/\\\/\///\\/\/\/////\//\///\/\/\//\
#__ /\/\//\///\//\//\//\/\//\\/\\\\\/\\\//\/
#__ //\\\\\//////\//\//\/\//\\////\/\\\/\/\/
#__ \\////\/\\/\////\//////\\/\\/////\/////\
#__ /\/\/\/\/\\///\/\\\\/\/\\//\/\\/\\\\\\//
#__ //\\\/\///\///\/\\\//\//\\\\//\\/\\/\/\/
#__ /\\/\//\\//\/\\\\\/\\\/\\\/\/\/\/\////\/
#__ /\//\\//\////\\\///\//\/\\\/\\///\//\\\\
#__ /\//\\////\/\\\//\\\//\/\///\\\\\/\/\\//
#__ \\////\\/\\\\\\///\///\//\\\/\\\\//\////
#__ \\\///\/\//\//\/\//\/\/\\\/\/\\///\/////
#__ \\/\\//\\\\\\///\\\\/\\/\\//\//\\\/\\/\/
#__ ////\\\////\/\\\\\/////\/\\\////\\///\\\
#__ \//\\\//\///\/\\\\//\\\////\\//\\/\/\//\
#__ \/\//\\//\\///\\\\\\//\///\/\\\\\\\\/\\/
#__ ///\/\//\\/\/\\\\\\\\/\///\//\\///\\//\\
#__ /////\\///\/\\/\/\\//\\//\/\\/\//\//\\\\
where withr::with_options() lets us temporarily change the print width and
cat() concatenates the slashes and prints out the characters as text.
We can also make this much prettier by drawing the patterns using ggplot2.
Drawing line segments with ggplot2
Instead of writing out slashes, we will draw a grid of diagonal line segments,
some of which will be flipped at random. To draw a segment, we need a starting
x–y coordinate and an ending x–y coordinate. geom_segment() will
connect the two coordinates with a line. Here’s a small example where we draw
four “slashes”.
library(ggplot2)
library(dplyr)
data 6 0.25 0.3 0 0.05
#__ 7 0.3 0.35 0 0.05
#__ 8 0.35 0.4 0 0.05
#__ 9 0.4 0.45 0 0.05
#__ 10 0.45 0.5 0 0.05
#__ # ... with 390 more rows
We can confirm that the segments in the grid fill out a plot. (I
randomly color the line segments to make individual ones visible.)
ggplot(grid) +
aes(
x = x_start, y = y_start, xend = x_end, yend = y_end, color = runif(400)) +
geom_segment(size = 1) + guides(color = FALSE)
Finally, we need to flip slashes at random. A segment becomes flipped if the
y_start and y_end are swapped. In the code below, we flip the slash in each
row if a randomly drawn number between 0 and 1 is less than .5. For style, we
also use theme_void() to strip away the plotting theme, leaving us with just
the maze design.
p_flip % mutate(
p_flip = p_flip,
flip = runif(length(y_end)) %
select(x_start:y_end, p_flip)
ggplot(grid) +
aes(x = x_start, y = y_start, xend = x_end, yend = y_end) +
geom_segment(size = 1, color = &quot;grey20&quot;) last_plot() + theme_void()
Now, we wrap all these steps together into a pair of functions.
make_10_print_data