This is a slightly adapted version of my answer to Nicer wavy line with TikZ. This version doesn't insist on drawing a full sine wave, but will be happy with half periods as well (i.e. it can start and end with an upward arch, while the previous version would always start with an upward and end with a downward arch):
\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}
Here's an extended version of the complete sines
decoration, which can now be controlled using start up
/start down
and end up
/end down
.
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\begin{document}
\newif\ifstartcompletesineup
\newif\ifendcompletesineup
\pgfkeys{
/pgf/decoration/.cd,
start up/.is if=startcompletesineup,
start up=true,
start up/.default=true,
start down/.style={/pgf/decoration/start up=false},
end up/.is if=endcompletesineup,
end up=true,
end up/.default=true,
end down/.style={/pgf/decoration/end up=false}
}
\pgfdeclaredecoration{complete sines}{initial}
{
\state{initial}[
width=+0pt,
next state=upsine,
persistent precomputation={
\ifstartcompletesineup
\pgfkeys{/pgf/decoration automaton/next state=upsine}
\ifendcompletesineup
\pgfmathsetmacro\matchinglength{
0.5*\pgfdecoratedinputsegmentlength / (ceil(0.5* \pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength) )
}
\else
\pgfmathsetmacro\matchinglength{
0.5 * \pgfdecoratedinputsegmentlength / (ceil(0.5 * \pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength ) - 0.499)
}
\fi
\else
\pgfkeys{/pgf/decoration automaton/next state=downsine}
\ifendcompletesineup
\pgfmathsetmacro\matchinglength{
0.5* \pgfdecoratedinputsegmentlength / (ceil(0.5 * \pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength ) - 0.4999)
}
\else
\pgfmathsetmacro\matchinglength{
0.5 * \pgfdecoratedinputsegmentlength / (ceil(0.5 * \pgfdecoratedinputsegmentlength / \pgfdecorationsegmentlength ) )
}
\fi
\fi
\setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
}] {}
\state{downsine}[width=\pgfdecorationsegmentlength,next state=upsine]{
\pgfpathsine{\pgfpoint{0.5\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
\pgfpathcosine{\pgfpoint{0.5\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
}
\state{upsine}[width=\pgfdecorationsegmentlength,next state=downsine]{
\pgfpathsine{\pgfpoint{0.5\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
\pgfpathcosine{\pgfpoint{0.5\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
\state{final}{}
}
\begin{tikzpicture}
\draw [gray!30] (0,-7) grid [step=0.5] (2.5,0);
\node at (1.25cm,0) [align=center,anchor=south] {\texttt{start up}\\\texttt{end up}};
\begin{scope}[
every path/.style={
decoration={
complete sines,
segment length=0.5cm,
amplitude=0.5cm,
mirror,
start up,
end up
},
decorate,
thick
}]
\foreach \length [count=\n] in {1,1.25,...,2.5}
\draw [yshift=-\n cm + 0.5cm] (0,0) node [anchor=east] {\length cm} -- (\length cm,0);
\end{scope}
\begin{scope}[xshift=3cm]
\draw [gray!30] (0,-7) grid [step=0.5] (2.5,0);
\node at (1.25cm,0) [align=center,anchor=south] {\texttt{start up}\\\texttt{end down}};
\begin{scope}[
every path/.style={
decoration={
complete sines,
segment length=0.5cm,
amplitude=0.5cm,
mirror,
start up,
end down
},
decorate,
thick
}]
\foreach \length [count=\n] in {1,1.25,...,2.5}
\draw [yshift=-\n cm + 0.5cm] (0,0) -- (\length cm,0);
\end{scope}
\end{scope}
\begin{scope}[xshift=9cm]
\draw [gray!30] (0,-7) grid [step=0.5] (2.5,0);
\node at (1.25cm,0) [align=center,anchor=south] {\texttt{start down}\\\texttt{end up}};
\begin{scope}[
every path/.style={
decoration={
complete sines,
segment length=0.5cm,
amplitude=0.5cm,
mirror,
start down,
end up
},
decorate,
thick
}]
\foreach \length [count=\n] in {1,1.25,...,2.5}
\draw [yshift=-\n cm + 0.5cm] (0,0) -- (\length cm,0);
\end{scope}
\end{scope}
\begin{scope}[xshift=6cm]
\draw [gray!30] (0,-7) grid [step=0.5] (2.5,0);
\node at (1.25cm,0) [align=center,anchor=south] {\texttt{start down}\\\texttt{end down}};
\begin{scope}[
every path/.style={
decoration={
complete sines,
segment length=0.5cm,
amplitude=0.5cm,
mirror,
start down,
end down
},
decorate,
thick
}]
\foreach \length [count=\n] in {1,1.25,...,2.5}
\draw [yshift=-\n cm + 0.5cm] (0,0) -- (\length cm,0);
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
Best Answer
Edit 2: Adapted code using
\makeatletter
and\makeatother
to avoid the bad practice of midifying a package's file.Edit: Changed code to make the coils end at the middle of the line.
If I understand correctly, you can get the result you are looking for by changing the predefined
coil
decoration. Although, the solution I propose is not prefect. The problem is that you need to change thesegment length
value a little bit to avoid too much (or too little) space at the end of the coil. Here are some examples, the blue coils correspond to the modified decoration: