![我怎样在 tikz 中绘制这个螺旋(带有垂直边)?](https://linux22.com/image/395577/%E6%88%91%E6%80%8E%E6%A0%B7%E5%9C%A8%20tikz%20%E4%B8%AD%E7%BB%98%E5%88%B6%E8%BF%99%E4%B8%AA%E8%9E%BA%E6%97%8B%EF%BC%88%E5%B8%A6%E6%9C%89%E5%9E%82%E7%9B%B4%E8%BE%B9%EF%BC%89%EF%BC%9F.png)
答案1
我希望没有人因为我重新提出这个问题而生气,但我觉得对于第一个问题,可以有例外。除了复制,您还可以使用foreach
。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\vlength{3}
\def\hstep{0.6}
\def\Xmax{9}
\fill[gray!40] ({-(\Xmax+3)*\hstep},-\vlength)
rectangle (\hstep/2,\vlength);
\draw[thick,dashed] (-1*\hstep,\vlength)
-- (-1*\hstep,-\vlength)
arc[start angle=180,end angle=360,x radius={1.5*\hstep},
y radius=0.75*\hstep]
foreach \X in {1,...,\Xmax}
{\ifodd\X
-- ({\hstep*(\X+1)},\vlength)
arc[start angle=0,end angle=180,x radius={(\X+1.5)*\hstep},
y radius={0.5*(\X+1.5)*\hstep}]
\else
-- ({-\hstep*(\X+1)},-\vlength)
arc[start angle=180,end angle=360,x radius={(\X+1.5)*\hstep},
y radius={0.5*(\X+1.5)*\hstep}]
\fi}
-- ({-\hstep*(\Xmax+2)},-1*\vlength);
\draw[thick] (-1*\hstep,\vlength)
arc[start angle=180,end angle=0,x radius={0.5*\hstep},
y radius=0.25*\hstep]
-- (0,-\vlength)
arc[start angle=180,end angle=360,x radius={0.5*\hstep},
y radius=0.25*\hstep]
(\hstep,-1*\vlength)
foreach \X in {1,...,\Xmax}
{\ifodd\X
-- (\hstep*\X,\vlength)
arc[start angle=0,end angle=180,x radius={(\X+0.5)*\hstep},
y radius={0.5*(\X+0.5)*\hstep}]
\else
-- ({-\hstep*\X},-1*\vlength)
arc[start angle=180,end angle=360,x radius={(\X+0.5)*\hstep},
y radius={0.5*(\X+0.5)*\hstep}]
\fi} -- ({-\hstep*(\Xmax+1)},-1*\vlength)
arc[start angle=0,end angle=-180,x radius={0.5*\hstep},
y radius=0.25*\hstep];
\end{tikzpicture}
\end{document}
或者使用 pgf 键。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[hstep/.initial=0.6,vlength/.initial=3,
Xmax/.store in=\Xmax,Xmax=9]
\fill[gray!40] ({-(\Xmax+3)*\pgfkeysvalueof{/tikz/hstep}},-\pgfkeysvalueof{/tikz/vlength})
rectangle (\pgfkeysvalueof{/tikz/hstep}/2,\pgfkeysvalueof{/tikz/vlength});
\draw[thick,dashed] (-1*\pgfkeysvalueof{/tikz/hstep},\pgfkeysvalueof{/tikz/vlength})
-- (-1*\pgfkeysvalueof{/tikz/hstep},-\pgfkeysvalueof{/tikz/vlength})
arc[start angle=180,end angle=360,x radius={1.5*\pgfkeysvalueof{/tikz/hstep}},
y radius=0.75*\pgfkeysvalueof{/tikz/hstep}]
foreach \X in {1,...,\Xmax}
{\ifodd\X
-- ({\pgfkeysvalueof{/tikz/hstep}*(\X+1)},\pgfkeysvalueof{/tikz/vlength})
arc[start angle=0,end angle=180,x radius={(\X+1.5)*\pgfkeysvalueof{/tikz/hstep}},
y radius={0.5*(\X+1.5)*\pgfkeysvalueof{/tikz/hstep}}]
\else
-- ({-\pgfkeysvalueof{/tikz/hstep}*(\X+1)},-\pgfkeysvalueof{/tikz/vlength})
arc[start angle=180,end angle=360,x radius={(\X+1.5)*\pgfkeysvalueof{/tikz/hstep}},
y radius={0.5*(\X+1.5)*\pgfkeysvalueof{/tikz/hstep}}]
\fi}
-- ({-\pgfkeysvalueof{/tikz/hstep}*(\Xmax+2)},-1*\pgfkeysvalueof{/tikz/vlength});
\draw[thick] (-1*\pgfkeysvalueof{/tikz/hstep},\pgfkeysvalueof{/tikz/vlength})
arc[start angle=180,end angle=0,x radius={0.5*\pgfkeysvalueof{/tikz/hstep}},
y radius=0.25*\pgfkeysvalueof{/tikz/hstep}]
-- (0,-\pgfkeysvalueof{/tikz/vlength})
arc[start angle=180,end angle=360,x radius={0.5*\pgfkeysvalueof{/tikz/hstep}},
y radius=0.25*\pgfkeysvalueof{/tikz/hstep}]
(\pgfkeysvalueof{/tikz/hstep},-1*\pgfkeysvalueof{/tikz/vlength})
foreach \X in {1,...,\Xmax}
{\ifodd\X
-- (\pgfkeysvalueof{/tikz/hstep}*\X,\pgfkeysvalueof{/tikz/vlength})
arc[start angle=0,end angle=180,x radius={(\X+0.5)*\pgfkeysvalueof{/tikz/hstep}},
y radius={0.5*(\X+0.5)*\pgfkeysvalueof{/tikz/hstep}}]
\else
-- ({-\pgfkeysvalueof{/tikz/hstep}*\X},-1*\pgfkeysvalueof{/tikz/vlength})
arc[start angle=180,end angle=360,x radius={(\X+0.5)*\pgfkeysvalueof{/tikz/hstep}},
y radius={0.5*(\X+0.5)*\pgfkeysvalueof{/tikz/hstep}}]
\fi} -- ({-\pgfkeysvalueof{/tikz/hstep}*(\Xmax+1)},-1*\pgfkeysvalueof{/tikz/vlength})
arc[start angle=0,end angle=-180,x radius={0.5*\pgfkeysvalueof{/tikz/hstep}},
y radius=0.25*\pgfkeysvalueof{/tikz/hstep}];
\end{tikzpicture}
\end{document}