Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to create an animation with a spring. The winding must be kept constant to make more realistic animation.

The following MWE illustrates the current situation of \pszigzag where it cannot produce a coil with reliable constant winding. I set 7 (a prime number) for the winding but the output oscillates; the output windings are sometimes 6.5 and sometimes 7. The red arrows also show the end corners changing from up to down. Please carefully see the image below and compare among them.

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-coil,multido}
\usepackage[nomessages]{fp}

\FPset\CoilArm{0.5}
\FPset\CoilWidth{.5}
\FPset\Windings{7}


\psset{coilarm=\CoilArm,coilwidth=\CoilWidth,linecolor=blue,linewidth=2\pslinewidth}

\def\SetFactorByLength#1{% #1: total length includes the arms
    \FPeval\Factor{round((#1-2*CoilArm)/(CoilWidth*Windings):3)}\ignorespaces
}

\begin{document}
\begin{pspicture}[showgrid](15,-11)
    \multido{\ix=5+1,\iy=-1+-1}{10}{%
    \SetFactorByLength{\ix}
    \pszigzag[coilheight=\Factor](0,\iy)(\ix,\iy)}
\end{pspicture}
\end{document}

Is it possible to get a coil with stable number of windings?

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

You should be truncating, not rounding the quantity in Factor:

Sample output

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-coil,multido}
\usepackage[nomessages]{fp}

\FPset\CoilArm{0.5}
\FPset\CoilWidth{.5}
\FPset\Windings{7}


\psset{coilarm=\CoilArm,coilwidth=\CoilWidth,linecolor=blue,linewidth=2\pslinewidth}

\def\SetFactorByLength#1{% #1: total length includes the arms
    \FPeval\Factor{trunc((#1-2*CoilArm)/(CoilWidth*Windings):3)}\ignorespaces
}

\begin{document}
\begin{pspicture}[showgrid](15,-11)
    \multido{\ix=5+1,\iy=-1+-1}{10}{%
    \SetFactorByLength{\ix}
    \pszigzag[coilheight=\Factor](0,\iy)(\ix,\iy)}
\end{pspicture}
\end{document}
share|improve this answer
add comment
\documentclass[pstricks]{standalone}
%\usepackage[pdf]{pstricks}% for pdflatex --shell-escape
\usepackage{pst-plot,pst-coil}
\begin{document}

\psset{coilarm=2mm,amplitude=0.5}
\multido{\rA=1+0.2}{20}{%
\begin{pspicture}(-0.5,0.75)(1.25,-5)
\psrotateright{%
\psaxes[linecolor=black!20,tickcolor=black!20,
        labelFontSize=\scriptscriptstyle]{->}(5mm,0)(4.5,1)
\pssin[periods=4,linecolor=red](0,0.5)(\rA,0.5)}%
\end{pspicture}}
\multido{\rA=5+-0.2}{20}{%
\begin{pspicture}(-0.5,0.75)(1.25,-5)
\psrotateright{%
\psaxes[linecolor=black!20,tickcolor=black!20,
        labelFontSize=\scriptscriptstyle]{->}(5mm,0)(4.5,1)
\pssin[periods=4,linecolor=red](0,0.5)(\rA,0.5)}%
\end{pspicture}}
\end{document}

enter image description here

share|improve this answer
    
Thank you for giving the alternate but I think Andrew Swann's answer suits my question best. –  Fifa Earth Cup 2014 Apr 21 '13 at 13:36
add comment

Rounding for the sixth decimal place can also fix this issue.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-coil,multido}
\usepackage[nomessages]{fp}

\FPset\CoilArm{0.5}
\FPset\CoilWidth{.5}
\FPset\Windings{7}


\psset{coilarm=\CoilArm,coilwidth=\CoilWidth,linecolor=blue,linewidth=2\pslinewidth}

\def\SetFactorByLength#1{% #1: total length includes the arms
    \FPeval\Factor{round((#1-2*CoilArm)/(CoilWidth*Windings):6)}\ignorespaces
}

\begin{document}
\begin{pspicture}[showgrid](15,-11)
    \multido{\ix=5+1,\iy=-1+-1}{10}{%
    \SetFactorByLength{\ix}
    \pszigzag[coilheight=\Factor](0,\iy)(\ix,\iy)}
\end{pspicture}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.