\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (0,0) grid (6,4);
\draw[decoration={aspect=0.2, segment length=1.5mm, amplitude=1mm,coil},decorate,ultra thick,domain=0:6,smooth] plot[id=sin] function{0.5*sin(2*x)+2};
\end{tikzpicture}
\end{document}
答案1
装饰和平滑路径效果不佳。相反,应增加样本数量。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,fpu}
\begin{document}
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (0,0) grid (6,4);
\draw[
decoration={
aspect=0.2,
segment length=1.5mm,
amplitude=1mm,
coil,
},
decorate,
ultra thick,
domain=0:6,
samples=55,
%smooth
] plot[id=sin] function{0.5*sin(2*x)+2};
\end{tikzpicture}
\end{document}
或者更好的是,使用sin
和cos
路径构造运算符(这里我只画了一个句点)。这样速度会快得多,因为涉及的低级操作少得多,而且准确率要高得多。
\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,fpu}
\begin{document}
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (0,0) grid (6,4);
\draw[
decoration={
aspect=0.2,
segment length=1.5mm,
amplitude=1mm,
coil,
},
decorate,
ultra thick,
] (0,2) sin (1*pi/4,2.5)
cos (2*pi/4,2.0)
sin (3*pi/4,1.5)
cos (4*pi/4,2.0);
\end{tikzpicture}
\end{document}