Advertisement
fangfufu

Cellular Automata in Beamer

Nov 11th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 4.67 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. The cellular automata code was copied and pasted from:
  3. http://tex.stackexchange.com/questions/118377/drawing-multiple-iterations-of-cellular-automatons-inline-possibly-with-tikz
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. \documentclass[11pt]{beamer}
  6. \usepackage{tikz}
  7.  
  8. % Each cell is stored globally as a macro
  9. % for example \csname cell-glider-1-7\endcsname
  10. \def\setcell#1#2#3{%
  11.     \pgfmathparse{int(#2)}\let\cx=\pgfmathresult%
  12.     \pgfmathparse{int(#3)}\let\cy=\pgfmathresult%
  13.     \expandafter\xdef\csname cell-#1-\cx-\cy\endcsname%
  14. }
  15.  
  16. % Cells are accessed by parsing #2 and #3 to integers
  17. % and assigning the cell contents to the macro #4
  18. \def\getcell#1#2#3#4{%
  19.     \pgfmathparse{int(#2)}\let\cx=\pgfmathresult%
  20.     \pgfmathparse{int(#3)}\let\cy=\pgfmathresult%
  21.     \def\marshal{\xdef#4}%
  22.     \marshal{\csname cell-#1-\cx-\cy\endcsname}%
  23. }
  24.  
  25.  
  26. % We flip the current from `current' to `next'
  27. % which is probably misleading. The texts can be
  28. % any arbitrary string as long as they are different.
  29. \def\currenttext{current}
  30. \def\nexttext{next}
  31.  
  32. \let\currentgeneration=\currenttext
  33. \let\nextgeneration=\nexttext
  34.  
  35. \def\switchgenerations{%
  36.     \ifx\currentgeneration\currenttext%
  37.         \global\let\currentgeneration=\nexttext%
  38.         \global\let\nextgeneration=\currenttext%
  39.     \else%
  40.         \global\let\currentgeneration=\currenttext%
  41.         \global\let\nextgeneration=\nexttext%
  42.     \fi%
  43. }
  44.  
  45. % Define colors \csname color-#1\endcsname
  46. % Which can be used according to the cell contents
  47. % So \csname color-0\endcsname holds the color for cells with value 0
  48. \def\setcolor#1{\expandafter\xdef\csname color-#1\endcsname}
  49. \def\getcolor#1{\csname color-#1\endcsname}
  50.  
  51.  
  52. % Define a 10x10 board with a border of 1 so edge
  53. % detection is not necessary.
  54. \foreach \x in {0,...,11}{%
  55.     \foreach \y in {0,...,11}{%
  56.         \setcell{\currentgeneration}{\x}{\y}{0}%
  57.     }% %
  58. }%
  59.  
  60.  
  61.  
  62. % Copy this board for the next generation
  63. % This is HUGELY inefficient. What would be better is
  64. % to maintain a list of only changing cells (hard in LaTeX)
  65. \foreach \x in {0,...,11}{%
  66.     \foreach \y in {0,...,11}{%
  67.         \setcell{\nextgeneration}{\x}{\y}{0}%
  68.     }%
  69. }
  70. \newcount\neighbors
  71.  
  72. \def\updategenerations{%
  73. \begingroup\nullfont% Hmm lots of unwanted spaces occur here
  74. \foreach \x in {1,...,10}{%
  75.     \foreach \y in {1,...,10}{%
  76.         \getcell{\currentgeneration}{\x}{\y}{\n}%
  77.         \getcell{\currentgeneration}{\x-1}{\y}{\a}%
  78.         \getcell{\currentgeneration}{\x+1}{\y}{\b}%
  79.         \getcell{\currentgeneration}{\x}{\y-1}{\c}%
  80.         \getcell{\currentgeneration}{\x}{\y+1}{\d}%
  81.         \getcell{\currentgeneration}{\x-1}{\y-1}{\e}%
  82.         \getcell{\currentgeneration}{\x+1}{\y-1}{\f}
  83.        \getcell{\currentgeneration}{\x-1}{\y+1}{\g}%
  84.         \getcell{\currentgeneration}{\x+1}{\y+1}{\h}%
  85.         \pgfmathsetcount\neighbors{\a+\b+\c+\d+\e+\f+\g+\h}%
  86.         % Here are the neighbor rules
  87.         \ifcase\neighbors %
  88.             \setcell{\nextgeneration}{\x}{\y}{0}% 0 neighbors
  89.         \or
  90.             \setcell{\nextgeneration}{\x}{\y}{0}% 1 neighbors
  91.         \or
  92.             \ifnum\n=1\relax
  93.             \setcell{\nextgeneration}{\x}{\y}{1}% 2 neighbors
  94.             \fi
  95.         \or
  96.             \setcell{\nextgeneration}{\x}{\y}{1}% 3 neighbors
  97.         \else
  98.             \setcell{\nextgeneration}{\x}{\y}{0}% more than 3 neighbors
  99.         \fi%
  100.     }%
  101. }%
  102. \endgroup%
  103. \switchgenerations%
  104. }
  105.  
  106.  
  107.  
  108. \def\drawcurrentgeneration{%}
  109. \begin{tikzpicture}[x=10pt,y=10pt]
  110. \foreach \x in {1,...,10}{%
  111.     \foreach \y in {1,...,10}{%
  112.         \setcell{\nextgeneration}{\x}{\y}{0}% Do here for a *minor* increment in speed
  113.         \getcell{\currentgeneration}{\x}{\y}{\value}%
  114.         \edef\c{\getcolor{\value}}%
  115.         \fill [fill=\c](\x, \y) rectangle ++(1,1);
  116.     }
  117. }
  118. \end{tikzpicture}%
  119. }
  120.  
  121. \setcolor{0}{blue!5}
  122. \setcolor{1}{blue!20}
  123.  
  124. \setcell{\currentgeneration}{1}{8}{1}
  125. \setcell{\currentgeneration}{2}{8}{1}
  126. \setcell{\currentgeneration}{3}{8}{1}
  127. \setcell{\currentgeneration}{3}{9}{1}
  128. \setcell{\currentgeneration}{2}{10}{1}
  129.  
  130. \title{My Document}
  131. \author{Fufu Fang\\ University of East Anglia}
  132. \date{\today}
  133. \usetheme{CambridgeUS}
  134. \usecolortheme{beaver}
  135. \begin{document}
  136.    \maketitle
  137.    \begin{frame}
  138.    \frametitle{Introduction}
  139.        This is a document I created in UEA's \LaTeX\ class.
  140.    \end{frame}
  141.  
  142.    \begin{frame}
  143.    \frametitle{Iteration 0}
  144.    \drawcurrentgeneration
  145.    \end{frame}
  146.  
  147.    \foreach \iteration in {1,...,32}{
  148.    \begin{frame}
  149.    \frametitle{Iteration \iteration}
  150.    \updategenerations
  151.    \drawcurrentgeneration
  152.    \end{frame}
  153.    }
  154.  
  155. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement