% Copyright 2014, 2019 Kevin Ryde
%
% This file is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the Free
% Software Foundation; either version 3, or (at your option) any later
% version.
%
% This file is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
% or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
% for more details.
%
% You should have received a copy of the GNU General Public License along
% with this file. If not, see .
% Usage: latex dragon-pgf-direction.tex
% xdvi dragon-pgf-direction.dvi
%
% This a dragon curve drawn with PGF (http://sourceforge.net/projects/pgf/)
% by segment directions.
%
% The direction calculation is in tikz maths and is touch on the slow
% side for a longer curve, such as 512 segments.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math} % for \tikzmath
\begin{document}
\tikzmath{
% Return the direction of dragon curve segment number \n, in degrees.
% \n = 0 is the first segment and it is at 0 degrees.
% \n = 1 is the next segment and is 90 degrees.
%
% The direction is a count of bit transitions, so look at adjacent
% bit pairs (overlapping) and count how many are either 01 or 10.
% Here \p is the low bit and \b is the second bit. The recursion
% drops the low bit of \n and repeats the check on the next pair.
% On reaching \n==0 there are no more bits so no more transitions.
%
function MyDragonDir(\n) {
if \n <= 0 then {
return 0;
} else {
integer \p,\b;
\p = isodd(\n);
\n = (\n - \p)/2;
\b = isodd(\n);
return MyDragonDir(\n) + 90*(\b != \p);
};
};
}
\begin{tikzpicture}
\draw (0,0) foreach \n in {0,...,63} { -- ++({MyDragonDir(\n)}:1) };
\node at (0,0) [below] { start };
\end{tikzpicture}
\end{document}