[Tex/LaTex] The Spiral of Roots in TikZ

asymptotemetapostpstrickstikz-pgf

How can one create a spiral of roots as shown in the diagram below:

enter image description here

where one can use a command to draw more parts to the spiral. For example, a command like \sqrtspiral{1}, \sqrtspiral{2}, \sqrtspiral{3} would generate the image above.

NOTE

I am inviting all solutions in TikZ, PSTricks, Asymptote and possibly MetaPost. So many ways to achieve the same diagram.

Best Answer

Starting from here… pretty similar to @AEllet solution. (Adapted) Suggestion of Sigur, the optional argument pases everything to the tikzpicture environment and thanks to Sigur for the 90 degree angle mark ;)

By the way, this spiral is known as Spiral of Theodorus. Just in case you need some extra data.

\documentclass{scrartcl}

\usepackage{tikz,ifthen}
\usetikzlibrary{calc}

\newcommand*{\sqrtspiral}[2][scale=3]{
    \begin{tikzpicture}[#1]
        \def\sqrtlast{#2}
        \coordinate (A) at (0,0);
        \coordinate (B) at (1cm,0);
        \draw (A) edge node[auto, swap] {1} (B);
        \foreach \n in {1,...,\sqrtlast}{
            \ifthenelse{\equal{\n}{\sqrtlast}}
            {
                \def\currentcolor{red}
                \def\currentsqrt{x}
            }
            {
                \def\currentcolor{black}
                \pgfmathtruncatemacro{\currentsqrt}{\n+1}
            }
            \coordinate (C) at ($(B)!1cm!-90:(A)$);
            \draw[\currentcolor] (A) edge node[fill=white] {$\sqrt{\currentsqrt}$} (C);
            \draw[\currentcolor] (C) edge node[auto] {1} (B);
            \coordinate (w) at ($(B)!4pt!-90:(A)$);
            \coordinate (z) at ($(B)!4pt!0:(A)$);
            \coordinate (t) at ($(w)!4pt!-90:(B)$);
            \draw (w) -- (t) -- (z);
            \coordinate (B) at (C);
        }
    \end{tikzpicture}
}

\begin{document}

\sqrtspiral{1}
\sqrtspiral{2}
\sqrtspiral{3}

\sqrtspiral[scale=2]{12}

\end{document}

enter image description here