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.

Consider the following example:

\documentclass{article}

\usepackage{pst-coil}

\psset{dimen = middel}

\def\frekvensgenerator[#1,#2](#3,#4){
  \psframe(#3,#4)(!#3 #1 add #4 #2 add)
  \psline(!#3 #4 #2 2 div add)(!#3 #1 add #4 #2 2 div add)
  \psframe(!#3 #1 2 div add 0.2 sub #4)(!#3 #1 2 div add 0.2 add #4 #2 2 div add)
  \rput{270}(!#3 #1 2 div add #4){%
    \psCoil[
      linewidth = 1.5pt,
      coilwidth = \spolevidde,
      coilheight = \spolehoejde,
      coilarm = 0
    ]{0}{\drejning}%
  }
  \psframe[
    fillstyle = solid,
    fillcolor = yellow!70
  ](!#3 #1 2 div add 0.25 sub  #4 \drejning\space 480 div \spolevidde\space mul \spolehoejde\space mul sub 1 sub)(!#3 #1 2 div add 0.25 add  #4 \drejning\space 480 div \spolevidde\space mul \spolehoejde\space mul sub)
  \psline(!#3 #1 2 div add #4 \drejning\space 480 div \spolevidde\space mul \spolehoejde\space mul sub 1 sub)(!#3 #1 2 div add #4 \drejning\space 480 div \spolevidde\space mul \spolehoejde\space mul sub 3 sub)
}

\begin{document}

\def\spolevidde{0.5}
\def\spolehoejde{0.5}

\begin{pspicture}(3.5,6.2)
\def\drejning{810}
  \frekvensgenerator[1.5,1.5](0,4.7)
\def\drejning{3330}
  \frekvensgenerator[1.5,1.5](2,4.7)
\end{pspicture}

\end{document}

output

As can be seen, the weight is not connected to the same point on the spring (modulo an integer number of windings) if I change the total numbers of degrees that the spring is twisted.

How do I accomplish this?

P.S. If the code can be made simpler (which it almost certainly can), you are very welcome to improve it. :)

share|improve this question
    
The problem seems to lie with 480... –  Werner Dec 28 '13 at 7:29
    
As your question is not minimal, I also provide you with the same complex answer. My answer here has been made to answer your question. –  Oh my ghost Dec 28 '13 at 10:13
    
@StiffJokes Looks nice. Can I make you give an answer to this specific question none the less? :) (Code improvement is more than welcome.) –  Svend Tveskæg Dec 28 '13 at 10:22
    
The distance of one coil is dx=height x width. –  Herbert Dec 28 '13 at 10:41
    
The coil projection should be seen as a zigzag line rather than a coil. –  Oh my ghost Dec 28 '13 at 13:23

2 Answers 2

up vote 9 down vote accepted
\documentclass{article}
\usepackage{pst-coil}
%\psset{dimen = middel}

\def\frekvensgenerator[#1,#2](#3,#4)#5{%
  \psframe(#3,#4)(!#3 #1 add #4 #2 add)
  \psline(!#3 #4 #2 2 div add)(!#3 #1 add #4 #2 2 div add)
  \psframe(!#3 #1 2 div add 0.2 sub #4)(!#3 #1 2 div add 0.2 add #4 #2 2 div add)
  \rput{270}(!#3 #1 2 div add #4){%
    \psCoil[
      linewidth = 1.5pt,
      coilwidth = \spolevidde,
      coilheight = \spolehoejde,
      coilaspect=45,
    ]{0}{#5}%
  }%
  \psframe[fillstyle = solid,fillcolor = yellow!70]%
    (!#3 #1 2 div add 0.25 sub #4 #5 360 div \dxCoil mul sub 1 sub)%
    (!#3 #1 2 div add 0.25 add #4 #5 360 div \dxCoil mul sub  )
  \psline(!#3 #1 2 div add #4 #5 360 div \dxCoil mul sub 1 sub)%
    (!#3 #1 2 div add #4 #5 360 div \dxCoil mul sub 3 sub)%
}

\begin{document}
\def\spolevidde{0.5 }
\def\spolehoejde{0.5 }
\edef\dxCoil{ \spolehoejde \spolevidde mul 45 sin mul }

\begin{pspicture}(3.5,6.2)
  \frekvensgenerator[1.5,1.5](0,4.7){810}
  \frekvensgenerator[1.5,1.5](2,4.7){3330}
  \frekvensgenerator[1.5,1.5](4,4.7){6930}
\end{pspicture}

\end{document}

enter image description here

45 sin is needed for the 3d view. The coil is seen under an angle of 45, which is the value of coilaspect, which is by default 45. However, I suppose you are looking for something like this:

\documentclass{article}
\usepackage{pst-coil}

\makeatletter
\def\frekvensgenerator{\@ifnextchar[\frekvensgenerator@i{\frekvensgenerator[1.5,1.5]}}
\def\frekvensgenerator@i[#1,#2](#3,#4)#5{%
  \psframe(#3,#4)(!#3 #1 add #4 #2 add)
  \psline(!#3 #4 #2 2 div add)(!#3 #1 add #4 #2 2 div add)
  \psframe(!#3 #1 2 div add 0.2 sub #4)(!#3 #1 2 div add 0.2 add #4 #2 2 div add)
  \pnode(!#3 #1 2 div add #4){StartX}
  \psline[linewidth=1.5pt](StartX)(StartX|{!0 #4 \spolearm sub})
  \rput{270}(StartX|{!0 #4 \spolearm sub \dxCoil 2 div sub}){%
    \psCoil[
      linewidth = 1.5pt,
      coilwidth = \spolevidde,
      coilheight = \spolehoejde,
    ]{-90}{#5}%
  }%
  \psline[linewidth = 1.5pt]%
    (StartX|{!0 #4 #5 90 sub 360 div \dxCoilRel mul sub \spolearm sub \dxCoil sub})%
    (StartX|{!0 #4 #5 90 sub 360 div \dxCoilRel mul sub \spolearm dup add sub \dxCoil sub})
  \psframe[fillstyle = solid,fillcolor = yellow!70]%
    (!#3 #1 2 div add 0.25 sub #4 
      #5 90 sub 360 div \dxCoilRel mul sub \spolearm dup add sub \dxCoil sub)%
    (!#3 #1 2 div add 0.25 add #4 
      #5 90 sub 360 div \dxCoilRel mul sub \spolearm dup add sub \dxCoil sub 1 sub)
  \pnode(StartX|{!0 #4 #5 90 sub 360 div \dxCoilRel mul sub 
                  \spolearm dup add sub \dxCoil sub 3 sub}){End#3}
  \psline(StartX|{!0 #4 #5 90 sub 360 div \dxCoilRel mul sub 
                   \spolearm dup add sub \dxCoil sub 1 sub})(End#3)%
}
\makeatother
\begin{document}
\def\spolearm{0.2 }
\def\spolevidde{0.5 }
\def\spolehoejde{0.3 }
\def\dxCoil{ \spolehoejde \spolevidde mul }
\def\dxCoilRel{ \dxCoil 45 sin mul }

\begin{pspicture}(3.5,6.2)
  \frekvensgenerator(0,4.7){3690}
  \def\spolevidde{0.45 }\def\spolehoejde{0.6 }
  \frekvensgenerator(2,4.7){3690}
  \def\spolevidde{0.4 }\def\spolehoejde{1.2 }
  \frekvensgenerator(4,4.7){3690}
  \def\spolevidde{0.35 }\def\spolehoejde{2.4 }
  \frekvensgenerator(6,4.7){3690}
  \pscurve[linecolor=red,linewidth=1.5pt](End0)(End2)(End4)(End6)
\end{pspicture}

\end{document}

enter image description here

and as animation:

enter image description here

share|improve this answer

pszigzag provided by pst-coil does not seem to be reliable so I create my own zigzag as follows.

Make sure \CoilWidth never becomes negative in all frames when creating animations.

\FPeval\CoilWidth{1-1.4*Lambda}% make sure it never becomes negative, otherwise the orientation will change!

MWE

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

\FPset\CoilArm{.6}
\FPset\Windings{7}

\psset
{
    dimen=medusa,
    fillcolor=yellow,
    linejoin=1,
}

\def\System#1{% #1: total length includes the arms
    \FPeval\Lambda{(#1-2*CoilArm)/Windings}%
    \FPeval\CoilWidth{1-1.4*Lambda}% make sure it never becomes negative, otherwise the orientation will change!
    \FPeval\PlotPoints{trunc(4*Windings+1,0)}%
    \curvepnodes[plotpoints=\PlotPoints,algebraic]{0}{\Lambda\space \Windings\space mul}{\CoilWidth*sin(2*Pi*t/\Lambda)|-t-\CoilArm}{P}%
    \rput(-.5,0){\psframe(1,1)\psframe(1,.5)\psframe(!1 3 div 0)(!2 3 div .5)}%
    \pscustom
    {
        \psline(0,0)
        \psnline(0,\Pnodecount){P}
        \psline(0,-#1)
    }%
    \rput(!-.25 -#1 1 sub){\psframe[fillstyle=solid](.5,1)\psline(.25,0)(.25,-2)}%
    \ignorespaces
}

\def\func(#1){2*sin(2*pi*#1/1)+4}

\begin{document}
\multido{\n=.00+.05}{21}{%
\begin{pspicture}(1,-9)(3,1)
    \rput(2,0){\FPeval\y{\func(\n)}\System{\y}}
\end{pspicture}}
\end{document}

enter image description here

share|improve this answer

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.