迭代版本

迭代版本

我是 TikZ 新手。我想在 TikZ 中绘制 BJK 连续体(桶柄)的后续构造步骤。我只有轮廓,但我不知道如何用颜色填充它们。我的代码:

\documentclass[11pt]{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[scale=5]

\draw (0,0) -- (1/3,0);
\draw (1,0) arc(0:180:1/2);
\draw (2/3,0) arc(0:180:1/6);
\draw (2/3,0) arc(0:180:1/6);
\draw (1,0) arc(0:-180:1/6);
\end{tikzpicture}

\begin{tikzpicture}[scale=5]
\draw (0,0) -- (1/9,0);
\draw (1,0) arc(0:180:1/2);
\draw (2/3,0) arc(0:180:1/6);
\draw (8/9,0) arc(0:180:7/18);
\draw (8/9,0) arc(0:180:7/18);
\draw (7/9,0) arc(0:180:5/18);
\draw (1,0) arc(0:-180:1/6);
\draw (8/9,0) arc(0:-180:1/18);
\draw (1/3,0) arc(0:-180:1/18);
\end{tikzpicture}

\begin{tikzpicture}[scale=5]
\draw (0,0) -- (1/27,0);
\draw (1,0) arc(0:180:1/2);
\draw (26/27,0) arc(0:180:25/54);
\draw (25/27,0) arc(0:180:23/54);
\draw (24/27,0) arc(0:180:21/54);
\draw (2/3,0) arc(0:180:1/6);
\draw (19/27,0) arc(0:180:11/54);
\draw (20/27,0) arc(0:180:13/54);
\draw (21/27,0) arc(0:180:15/54);
\draw (1,0) arc(0:-180:1/6);
\draw (26/27,0) arc(0:-180:7/54);
\draw (25/27,0) arc(0:-180:5/54);
\draw (24/27,0) arc(0:-180:1/18);
\draw (1/3,0) arc(0:-180:1/18);
\draw (8/27,0) arc(0:-180:1/54);
\draw (1/9,0) arc(0:-180:1/54);
\end{tikzpicture}

\end{document}

enter image description here

这应该如下图所示。我该怎么做?

enter image description here

答案1

迭代版本

经过五次迭代,得到完整的迭代解决方案:

enter image description here

\documentclass[tikz,margin=0mm]{standalone}
\gdef\curarcs{180/0/1/-2}
\gdef\den{2}
\def\nextiter{
  \gdef\startarcs{}
  \gdef\nedarcs{}
  \pgfmathtruncatemacro\nden{\den*3}
  \xdef\den{\nden}
  \foreach \sa/\ea/\num/\mod[count=\c] in \curarcs {
    \pgfmathtruncatemacro\snnum{\num*3}
    \pgfmathtruncatemacro\ennum{\num*3+\mod}
    \pgfmathtruncatemacro\enmod{-\mod}
    \pgfmathtruncatemacro\snmod{\mod}
    \edef\snarc{\sa/\ea/\snnum/\snmod}
    \edef\enarc{\ea/\sa/\ennum/\enmod}
    \ifnum\c=1 \xdef\startarcs{\snarc} \else \xdef\startarcs{\startarcs,\snarc} \fi
    \ifnum\c=1 \xdef\endarcs{\enarc} \else \xdef\endarcs{\enarc,\endarcs} \fi
  }
  \xdef\curarcs{\startarcs,0/-180/1/-2,\endarcs}
}
\begin{document}
\foreach \iter in {1,...,5}{
  \begin{tikzpicture}
    \nextiter
    \path[fill=red] (0,0)
    \foreach \sa/\ea/\num/\mod in \curarcs { arc(\sa:\ea:\num/\den) }
    -- cycle;
  \end{tikzpicture}
}
\end{document}

初始版本

这里有四次迭代...和一些注释来展示递归过程。

enter image description here

\documentclass[tikz]{standalone}
\begin{document}

\begin{tikzpicture}
\fill[blue]
(0,0)
arc(180:0:3/6)
%
arc(0:-180:1/6)
%
arc(0:180:1/6)   % -2
-- cycle; 
\end{tikzpicture}

\begin{tikzpicture}
\fill[blue]
(0,0)
arc(180:0:9/18)
arc(0:-180:3/18)
arc(0:180:3/18)
%
arc(0:-180:1/18)
%
arc(180:0:5/18)  % +2
arc(-180:0:1/18) %% -2
arc(0:180:7/18)  % -2
-- cycle;
\end{tikzpicture}

\begin{tikzpicture}
\fill[blue]
(0,0)
arc(180:0:27/54)
arc(0:-180:9/54)
arc(0:180:9/54)
arc(0:-180:3/54)
arc(180:0:15/54)
arc(-180:0:3/54)
arc(0:180:21/54)
%
arc(0:-180:1/54)
%
arc(180:0:23/54) % +2
arc(0:-180:5/54) % +2
arc(0:180:13/54) % -2
arc(-180:0:1/54) %% -2
arc(180:0:11/54) % +2
arc(-180:0:7/54) % -2
arc(0:180:25/54) % -2
-- cycle;
\end{tikzpicture}

\begin{tikzpicture}
\fill[blue]
(0,0)
arc(180:0:81/162)
arc(0:-180:27/162)
arc(0:180:27/162)
arc(0:-180:9/162)
arc(180:0:45/162)
arc(-180:0:9/162)
arc(0:180:63/162)
arc(0:-180:3/162)
arc(180:0:69/162)
arc(0:-180:15/162)
arc(0:180:39/162)
arc(-180:0:3/162)
arc(180:0:33/162)
arc(-180:0:21/162)
arc(0:180:75/162)
%
arc(0:-180:1/162)
%
arc(180:0:77/162)  % +2
arc(0:-180:23/162) % +2
arc(0:180:31/162)  % -2
arc(0:-180:5/162)  % +2
arc(180:0:41/162)  % +2
arc(-180:0:13/162) % -2
arc(0:180:67/162)  % -2
arc(-180:0:1/162)  %% -2
arc(180:0:65/162)  % +2
arc(0:-180:11/162) % +2
arc(0:180:43/162)  % -2
arc(-180:0:7/162)  % -2
arc(180:0:29/162)  % +2
arc(-180:0:25/162) % -2
arc(0:180:79/162)  % -2
-- cycle;
;
\end{tikzpicture}

\end{document}

每一步都以与上一步相同的路径开始(分母大三倍)。然后,您将得到一个半径为 1/分母的圆弧。然后,您将路径的第一部分还原,并将半径的分子略微修改(2 或 -2)。所有修改(2 或 -2)都是对称的(但相反),中心修改始终为 -2,并且修改的最后一部分与上一步相同。

答案2

有专门用于解决此类问题的库,如\usepgfplotslibrary{fillbetween}和类似库,但对于这样一个简单的例子,可能只需在白色和蓝色之间交替填充就足够了:

\documentclass[11pt]{article} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.13} 
\begin{document} 
    \begin{tikzpicture}[scale=5] 
        \draw (0,0) -- (1/3,0); 
        \draw[fill=blue] (1,0) arc(0:180:1/2); 
        \draw[fill=white] (2/3,0) arc(0:180:1/6); 
        \draw[] (2/3,0) arc(0:180:1/6); 
        \draw[fill=blue] (1,0) arc(0:-180:1/6); 
    \end{tikzpicture} 
\end{document}

enter image description here

相关内容