TikZ 中的根螺旋

TikZ 中的根螺旋

如何创建如下图所示的根螺旋:

在此处输入图片描述

可以使用命令绘制螺旋的更多部分。例如,像\sqrtspiral{1}\sqrtspiral{2}、 这样的命令\sqrtspiral{3}将生成上面的图像。

笔记

我邀请 TikZ、PSTricks、Asymptote 和可能的 MetaPost 中的所有解决方案。有这么多方法可以实现相同的图表。

答案1

从...开始这里… 与 @AEllet 解决方案非常相似。(改编)Sigur 的建议,可选参数将所有内容传递到环境中tikzpicture,感谢 Sigur 提供的 90 度角标记 ;)

顺便说一下,这个螺旋被称为狄奥多罗斯螺旋。以防你需要一些额外的数据。

\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}

在此处输入图片描述

答案2

在此处输入图片描述

在此处输入图片描述

从 开始看起来更自然\sqrt{1}。这使用环境中定义的Asymptote MWE 函数:spiralOfRoots(n)asydef

% spiralsq.tex :
%
\documentclass[10pt,a4paper]{article}
\usepackage{lmodern}
\usepackage{subcaption}
\usepackage[inline]{asymptote}
\begin{asydef}
unitsize(2cm);
import fontsize;
defaultpen(fontsize(9pt));
pen linepen=deepblue+0.8bp;
pen labelpen=black;
pen markpen=gray+0.6bp;

void spiralOfRoots(int n){
  assert(n>0);
  real w=0.15;
  pair O=0E,a=E,b;
  pair p,q,r;
  for(int i=1;i<=n;++i){
    draw(O--a,linepen);
    label("$\sqrt{"+string(i)+"}$",O--a,O,labelpen,UnFill);
    b=a+dir(degrees(a)+90);
    draw(a--b,linepen);
    p= w*dir(b-a);
    r=-w*dir(a);
    q=p+r;
    draw(a+p--a+q--a+r,markpen);
    label("$1$",a--b,dir(degrees(a-b)+90),labelpen);
    a=b;
  }
  draw(O--a,linepen);
  label("$\sqrt{x}$",O--a,O,labelpen,UnFill);
}
\end{asydef}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
%
\begin{document}
%
\begin{figure}
\captionsetup[subfigure]{justification=centering}
\centering
\begin{subfigure}{0.3\textwidth}
\centering
\begin{asy}
spiralOfRoots(1);
\end{asy}
%
\caption{1}
\label{fig:1a}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\centering
\begin{asy}
spiralOfRoots(2);
\end{asy}
%
\caption{2}
\label{fig:1b}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\centering
\begin{asy}
spiralOfRoots(3);
\end{asy}
%
\caption{3}
\label{fig:1c}
\end{subfigure}
%
\caption{1,2,3}
\label{fig:1}
\end{figure}
%
\begin{figure}
\centering
\begin{asy}
unitsize(15mm);
spiralOfRoots(15);
\end{asy}
\caption{15}
\label{fig:2}
\end{figure}

\end{document}
%
% Process:
%
% pdflatex spiralsq.tex
% asy spiralsq-*.asy
% pdflatex spiralsq.tex

答案3

虽然我没有标注侧面,但这里有一些东西:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\def\mylast{6}
\begin{document}

\begin{tikzpicture}
  \coordinate (ORIGIN) at (0,0) ;
  \coordinate (LAST)   at (1,0) ;
  \draw (ORIGIN) -- (LAST) ;
  \foreach \myitem in {2,3,...,\mylast}
  {
    \coordinate (NEXT) at ($(LAST)!1cm!-90:(ORIGIN)$) ;
    \draw (LAST) -- (ORIGIN) -- (NEXT) -- cycle;
    \coordinate (LAST) at (NEXT);
  }
\end{tikzpicture}

\end{document}

在此处输入图片描述

更新

这是添加一些标签的初步尝试。不是最好的,但我稍后会再仔细研究一下。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\def\mylast{12}
\begin{document}

\begin{tikzpicture}
  \coordinate (ORIGIN) at (0,0) ;
  \coordinate (LAST)   at (2,0) ;
  \foreach \myitem in {1,2,3,...,\mylast}
  {
    \coordinate (NEXT) at ($(LAST)!2cm!-90:(ORIGIN)$) ;
    \draw (LAST) -- (ORIGIN) -- (NEXT) -- cycle;
    \node (MID) at ($(LAST)!0.5!(ORIGIN)$)   {};
    \expandafter\def\csname mylbl\mylast\endcsname{$\sqrt{\myitem}$}
    \node (LBL\myitem) at ($(MID)!0.75em!90:(LAST)$) {\footnotesize\csname mylbl\mylast\endcsname};
    \node at ($($(LAST)!0.5!(NEXT)$)!0.5em!90:(LAST)$) {1};
    %% right angle
    \coordinate (perpA) at ($(LAST)!0.5em!(NEXT)$);
    \coordinate (perpB) at ($(perpA)!0.5em!-90:(LAST)$);
    \coordinate (perpC) at ($(LAST)!0.5em!(ORIGIN)$);
    \draw (perpA) -- (perpB) -- (perpC);
    %% reset "LAST" for next iteration
    \coordinate (LAST) at (NEXT);
  }
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案4

这只是另一种做事方式:

\documentclass[tikz,border=0.125cm]{standalone}
\usetikzlibrary{math}
\begin{document}

\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
  int \n;
  \b = 0; \d = 1; \N = #2;
  for \n in {1,...,\N}{
    \l = (\n == \N && \N > 1) ? "red" : "black";
    \e = (\n == 1) ? " -- cycle" : "";
    {
      \path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
        node [\l, midway, anchor=\b+180] {1}
        \e (\d/2, 0)  node [fill=white] {$\sqrt{\n}$};
      \path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
    };
    \d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
  };
  {
    \path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
  };
}}}

\sqrtspiral{10}

\end{document}

在此处输入图片描述

相关内容