我怎样在 tikz 中绘制这个螺旋(带有垂直边)?

我怎样在 tikz 中绘制这个螺旋(带有垂直边)?

我怎样在 tikz 中绘制这个螺旋(带有垂直边)?

我知道的唯一方法是复制并粘贴代码来绘制不同半径的半圆,然后一次连接一个半圆,这将花费很长时间。

螺旋状的东西

答案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}

相关内容