% a309873-images.tex, Kevin Ryde, September 2019
%
% This is some sample images to illustrate curves arising from the
% period-doubling turn sequence A309873.  It uses PGF tikzmath
% functions, and L-systems library.
%
% The same turn 60 degrees etc is used for the left a(n)=+1 and right
% a(n)=-1.  Different 60 left and 120 right are the Koch curve and
% other different amounts can be other different results.
% 

\documentclass[a4paper]{article}
\pagestyle{empty}         % no page numbers

% Title and author go into PDF (by hyperref pdfusetitle),
% but not actually displayed.
\title{A309873 Period-Doubling Turn Sequence Curves}
\author{Kevin Ryde}
\date{September 2019}

\usepackage[T1]{fontenc}  % T1 for accents, before babel
\usepackage{amsmath}
\usepackage{gensymb}      % for \degree
\usepackage[pdfusetitle,
            pdfsubject={OEIS A309873},
            pdfkeywords={OEIS, A309873, period-doubling},
            pdflang={en}, % RFC3066 ISO639 code
            pdfborderstyle={/W 0},  % no border on hyperlinks
           ]{hyperref}

\usepackage{tikz}
\usetikzlibrary{calc}     % for ($(...)$) coordinate calculations
\usetikzlibrary{lindenmayersystems}
\usetikzlibrary{math}     % for \tikzmath
\tikzset{>=latex,         % arrowhead type
         font=\small}     % same as text, not the display \normalfont

% % Uncomment this to see a box around each page text width and height.
% \usepackage{fancybox}
% \fancypage{\setlength{\fboxsep}{0pt}\fbox}{}


%------------------------------------------------------------------------------
\begin{document}
\hfil {\large
       A309873 Period-Doubling Turn Sequence Curves}
\hfil

\bigskip

\tikzmath{
  % a(n) returns A309873(n).
  %
  function a(\n) {   % A309873
    if \n <= 0 then { oopsShouldHaveNgreaterThanOrEqualToOne; };
    if mod(\n,2) then { return 1; }
    else { return - a(\n / 2); };
  };
  if a(1) != 1 then { abort; };   % self-tests
  if a(2) != -1 then { abort; };
  if a(3) != 1 then { abort; };
  if a(4) != 1 then { abort; };
  if a(5) != 1 then { abort; };
  if a(6) != -1 then { abort; };
  if a(7) != 1 then { abort; };
  if a(8) != -1 then { abort; };
  if a(9) != 1 then { abort; };
  %
  function d(\n) {   % A068639
    if \n then { return int(div(\n+1,2)) - d(int(div(\n,2))); }
    else { return 0; };
  };
  if d(0) != 0 then { abort; };   % self-tests
  for \n in {1,2,...,16}{
    if d(\n) != d(\n-1) + a(\n) then { abort; };
  };
}
\noindent
Turn sequence, for $n = 1 \text{ to } 16$ :

\href{http://oeis.org/A309873}{A309873} $=$
\phantom{0, }
\tikzmath{
  int \n, \a;
  for \n in {1,2,...,16}{
    \a = a(\n);
    print {\a, };
  };
} \dots

\smallskip

\noindent
Partial sums are net direction of segment, for $n = 0 \text{ to } 16$ :

\href{http://oeis.org/A068639}{A068639} $=$
\tikzmath{
  int \n, \d;
  for \n in {0,1,...,17}{
    \d = d(\n);
    print {\d, };
  };
} \dots

\medskip

%------------------------------------------------------------------------------
% Bend vs Turn

The turn sequence for given turn angle $\alpha$ is followed by drawing
a unit segment forward then turning \kern.05em
$\alpha \operatorname{.} a(n)$, and repeat.

Davis and Knuth take \href{http://oeis.org/A309873}{A309873} and
similar as folds in a long strip of a paper which is unfolded to a
``bending'' angle $\theta$ at each fold.  Bending, or unfolding, by
$\theta$ corresponds to turns by
 $\alpha = 180\degree - \theta$.

\begin{center}
\begin{tikzpicture}[scale=1.2]

  \draw[->] (0,0) -- (1,0) arc[start angle=-90,end angle=90,radius=.05]
               -- ++(-1,0)
    node[pos=.5,above=1ex] {folded paper};

  \begin{scope}[shift={(3,0)}]
    \draw[->] (0,0) -- (1,0) -- ++(60:1);
    \draw[dashed] (1.2,0) -- (1.95,0);
    \draw[<-,dashed] (1,0) ++(70:.5) arc[start angle=70,end angle=180,radius=.5]
      node[pos=.3,above left] {bend $120\degree$};
    \draw[->,dashed] (1,0) ++(10:.5) arc[start angle=10,end angle=50,radius=.5]
      node[pos=.25,above right] {turn $60\degree$};
  \end{scope}

\end{tikzpicture}
\end{center}

\smallskip

%------------------------------------------------------------------------------
% Turns by 90

Davis and Knuth draw turns by $90\degree$ ($=$ bend by $90\degree$)
which is merely 4 unit squares repeatedly re-traversed.
\begin{center}
\begin{tikzpicture}[scale=.4]

  % PGF has a "[turn]" which draws relative to the direction of the
  % last segment.  But circa its version 3.0.3 it doesn't seem to like
  % user-defined functions for the angle.  Some macros could probably
  % help that, or there's a recent turtle graphics library, but for
  % now draw by directions, even though the aim here is to illustrate
  % turns A309873 rather than directions (A068639).
  %
  % Also circa 3.0.3, something fishy seems to happen to [turn] inside
  % a foreach making it not relative to the last tangent but rather to
  % an angle from outside the foreach, or something.
  %
  \draw[->] (0,0) foreach \n in {0,1,...,4} { -- ++({90*d(\n)}:1) }
                             -- ++({90*d(5)}:.65);
  \fill (0,0) circle (.15);

  \begin{scope}[shift={(6,0)}]
    \draw (0,0) foreach \n in {0,1,...,32} { -- ++({90*d(\n)}:1) };
    \fill (0,0) circle (.15);

    \node at (0,0) [below right,font=\tiny] {origin};
    \node at (4,0) [right] {turns by $90\degree$};
  \end{scope}

\end{tikzpicture}
\end{center}

\smallskip


%------------------------------------------------------------------------------
% Turns by 120

% The PGF lindenmayersystems library takes an L-system specification
% in a fairly usual form.  See the a309873.l file (which is for
% Xfractint) for some notes on how the rules work.  The string
% expansions etc here are all done in TeX so requires nothing extra,
% though it's not particularly fast when the expansion order reaches a
% few thousand symbols.
%
% The curves generated here variously re-traverse segments.  Suspect
% that comes out from PGF as simply segments drawn 2 or more times,
% which is a bit wasteful.  Re-drawing tends to come out a bit poor
% from "anti-alias" on-screen drawing which re-adds greyness, making
% re-drawn lines look a tiny bit darker or thicker than they will be
% in a final printout (typical laser printers having much higher
% resolution).
%
\pgfdeclarelindenmayersystem{A309873 turns}{
  \symbol{S}{\pgflsystemdrawforward}
  \rule{F -> F+S--}
  \rule{S -> F+F++}
}

The period doubling sequence is a morphism (see
\href{http://oeis.org/A096268}{A096268}) and can be thought of by an
expansion ``level'' $k$ comprising $2^k$ segments.  Of course drawing
any number of segments is possible.

Davis and Knuth draw an example $120\degree$ turns ($=$ bend by
$60\degree$) of 4096 points which is $k = 12$ and which they call
``Fido''.  The resemblance to something canine is slightly improved in
their drawing where segments traversed multiple times are darker.
These are in the middle something like a nose (not done here).

\begin{center}
\begin{tikzpicture}[scale=.8]
  \newcommand\MyAngle{120}

  \begin{scope}
    \newcommand\MyLevel{2}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node (text) at (0,0) [left,font=\tiny] {start};
    \node at (0em, -.5em) [below,font=\scriptsize] {$k = \MyLevel$};
    \node at (text.west |- 0,0) [left=1em,align=center]
      {turns \\ by $\MyAngle\degree$};
  \end{scope}

  \begin{scope}[shift={(7em,0em)}]
    \newcommand\MyLevel{3}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny] {start};
    \node at (.7em, -.5em) [below,font=\scriptsize] {$k = \MyLevel$};
  \end{scope}

  \begin{scope}[shift={(15em,0em)}]
    \newcommand\MyLevel{4}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny] {start};
    \node at (0em, -1em) [below left,font=\scriptsize] {$k = \MyLevel$};
  \end{scope}

  \begin{scope}[shift={(25em,0em)}]
    \newcommand\MyLevel{6}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny] {start};
    \node at (0em, -1em) [below left,font=\scriptsize] {$k = \MyLevel$};
  \end{scope}

\end{tikzpicture}
\hspace{2em} % bias to the left
\end{center}
\vspace{-2ex}
\begin{center}
\begin{tikzpicture}[scale=.6]

  \newcommand\MyAngle{120}
  \newcommand\MyLevel{12}
  \draw [lindenmayer system={A309873 turns,
                             step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
        lindenmayer system;
  \node at (0,0) [left,font=\tiny] {start};
  \node at (16em,0) [right,font=\tiny] {end};
  \node at (22em, -1em) [right,align=center]
    {turns $\MyAngle\degree$, $k = \MyLevel$ \\[.5ex]
     ``Fido'' \\ of Davis and Knuth};

\end{tikzpicture}
\end{center}

\bigskip

%------------------------------------------------------------------------------
% Turns by 60

Davis and Knuth draw $60\degree$ turns too ($=$ bend by $120\degree$)
which is a similar Fido shape but opened up more.  Curve overlapping
begins in level $k{=}5$ for this angle.

\begin{center}
\begin{tikzpicture}[scale=.55]
  \newcommand\MyAngle{60}

  \begin{scope}
    \newcommand\MyLevel{4}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny,inner sep=.2em] {start};
    \node at (0, -1em) [below] {$k = \MyLevel$};
    \node at (-6.5em, 2em) [left=1em,align=center]
      {turns \\ by $\MyAngle\degree$};
  \end{scope}

  \begin{scope}[shift={(16em,-1em)}]
    \newcommand\MyLevel{5}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny,inner sep=.2em] {start};
    % \node at (-6em, 3.4em) [below,font=\tiny] {end};
    \node at (-2em, -1em) [below] {$k = \MyLevel$};
  \end{scope}

  \begin{scope}[shift={(32em,-1em)}]
    \newcommand\MyLevel{6}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny,inner sep=.2em] {start};
    \node at (-2em, -1em) [below] {$k = \MyLevel$};
  \end{scope}

\end{tikzpicture}
\hspace{1.5em} % bias to the left
\end{center}

\begin{center}
\hspace{2em} % bias to the right
\begin{tikzpicture}[scale=.33]

  \newcommand\MyAngle{60}
  \newcommand\MyLevel{12}
  \draw [lindenmayer system={A309873 turns,
                             step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
        lindenmayer system;
  \node at (0,0) [below=.1em,font=\tiny] {start};
  \node at (0em, 28em) [above,font=\tiny] {end};
  \node at (20em, 14em) [right,align=center]
    {turns $\MyAngle\degree$, $k = \MyLevel$ \\[.5ex]
      Fido variant \\
      of Davis and Knuth};

\end{tikzpicture}
\end{center}

\bigskip

%------------------------------------------------------------------------------
% Turns by 45 and 135

\begin{center}
\begin{tikzpicture}[scale=.35]

  \begin{scope}
    \newcommand\MyAngle{45}
    \newcommand\MyLevel{8}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny,yshift=-.5ex,inner sep=.2em] {start};

    \draw[dashed] (4.5em,0) -- ++(3/2*\MyAngle:22em)
      node[at end,right,font=\scriptsize,align=center]
          {slope \\[.5ex]
           $\medmuskip=.2\medmuskip
            \frac32 \times \MyAngle\degree$};

    \draw[dashed] (-3.5em,2em) -- ++(5/2*\MyAngle:28em)
      node[pos=.6,below left,inner sep=.2em,font=\scriptsize,align=center]
          {slope \\[.5ex]
           $\medmuskip=.2\medmuskip
            \frac52 \times \MyAngle\degree$};

    \node at (10em, 6em) [right,align=center]
      {turns $\MyAngle\degree$ \\ $k = \MyLevel$};

    % dots on the right-most points which make the 67.5 degree slope
    \begin{scope}[x={(1em,0)},y={(0,1em)}]
      \fill ({2 + 2/sqrt(2)}, {    2/sqrt(2)}) circle (.25)
            ({4 + 2/sqrt(2)}, {2 + 6/sqrt(2)}) circle (.25)
            ({6 + 2/sqrt(2)}, {4 + 10/sqrt(2)}) circle (.25)
            ({8 + 2/sqrt(2)}, {6 + 14/sqrt(2)}) circle (.25);
    \end{scope}

  \end{scope}

  \begin{scope}[shift={(34em,15em)},scale=3.5]
    \newcommand\MyAngle{135}
    \newcommand\MyLevel{8}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny,inner sep=.2em] {start};

    \node at (5em, -2.3em) [below left,align=center]
      {turns $\MyAngle\degree$ \\ $k = \MyLevel$};

  \end{scope}

\end{tikzpicture}
\end{center}

\smallskip

%------------------------------------------------------------------------------
% Turns by 30 and 150

\begin{center}
\begin{tikzpicture}[scale=.45]

  \begin{scope}
    \newcommand\MyAngle{30}
    \newcommand\MyLevel{8}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;

    \node at (12.2em, 0em) [right,align=center]
      {turns $\MyAngle\degree$ \\[.2ex]
       $k = \MyLevel$, \\[.5ex]
       origin \\ in the \\ middle};
  \end{scope}

  \begin{scope}[shift={(39em,0em)},scale=4]
    \newcommand\MyAngle{150}
    \newcommand\MyLevel{8}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;

    \node at (3.5em, -.5em) [right,align=center]
      {turns $\MyAngle\degree$ \\ $k = \MyLevel$};
  \end{scope}

\end{tikzpicture}
\end{center}

\smallskip

%------------------------------------------------------------------------------
% Turns by 10 and 15

\begin{center}
\hspace{2em} % bias to the right
\begin{tikzpicture}[scale=.17]

  \begin{scope}
    \newcommand\MyAngle{10}
    \newcommand\MyLevel{8}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny,yshift=-.5ex,inner sep=.2em] {start};

    \node at (25em, 12em) [right,align=center]
      {turns $\MyAngle\degree$ \\ $k = \MyLevel$};
  \end{scope}

  \begin{scope}[shift={(95em,0em)},scale=1.3]
    \newcommand\MyAngle{15}
    \newcommand\MyLevel{8}
    \draw [lindenmayer system={A309873 turns,
                               step=1em,angle=\MyAngle,axiom=F,order=\MyLevel}]
          lindenmayer system;
    \node at (0,0) [left,font=\tiny,yshift=-.5ex,inner sep=.2em] {start};

    \node at (15.5em, 10em) [right,align=center]
      {turns $\MyAngle\degree$ \\ $k = \MyLevel$};
  \end{scope}

\end{tikzpicture}
\end{center}

%------------------------------------------------------------------------------
\end{document}
