在 TikZ 中,当我应用路径变形装饰(例如“蛇”)并指定段长度选项时,我通常会在路径末尾添加一条直线段,例如
这通常不是理想的行为。如果路径末尾没有空间容纳整个装饰段,就会发生这种情况。如果我不指定段长度,那就没问题,但我希望对装饰参数有一定的控制。
您是否知道是否有一种方法可以柔和地设置段长度,以便自动进行一些修改以摆脱这些直线段?
该图片由以下代码生成:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\draw[decorate, decoration = {snake, segment length = .4cm}] (0,0) -- (2,0);
\end{tikzpicture}
\end{document}
答案1
这是我对这个问题的回答的一个稍微改编的版本使用 TikZ 绘制更漂亮的波浪线。此版本不坚持绘制完整的正弦波,但对半周期也满意(即它可以以向上的拱形开始和结束,而先前的版本总是以向上的拱形开始并以向下的拱形结束):
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\begin{document}
\pgfdeclaredecoration{complete sines}{initial}
{
\state{initial}[
width=+0pt,
next state=upsine,
persistent precomputation={\pgfmathsetmacro\matchinglength{
\pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
\setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
}] {}
\state{upsine}[width=\pgfdecorationsegmentlength,next state=downsine]{
\pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
\pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
}
\state{downsine}[width=\pgfdecorationsegmentlength,next state=upsine]{
\pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
\pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
\state{final}{}
}
\begin{tikzpicture}[
every path/.style={
decoration={
complete sines,
segment length=1cm,
amplitude=1cm
},
decorate,
thick
}]
\draw (0,0) -- (2,0);
\draw [yshift=-1.2cm] (0,0) -- (2.5,0);
\draw [yshift=-2.4cm] (0,0) -- (3,0);
\draw [yshift=-3.6cm] (0,0) -- (3.5,0);
\end{tikzpicture}
\end{document}