%% $Id: pst-coil.tex 930 2024-08-13 18:19:44Z herbert $
%% BEGIN pst-coil.tex
%%
%% Coils and zigzags with PSTricks
%% See the PSTricks User's Guide for documentation.
%% For use with the PostScript header file `pst-coil.pro'.
%%
%%
%% COPYRIGHT 1993, 1994, 1999 by Timothy Van Zandt, tvz@nwu.edu.
%%           2006-2024 Herbert Voss, hvoss@tug.org
%%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN
%% archives in directory macros/latex/base/lppl.txt.
%%
\message{ v\fileversion, \filedate}
\csname PSTcoilsLoaded\endcsname
\let\PSTcoilsLoaded\endinput
%
\ifx\PSTricksLoaded\endinput  \else\input pstricks.tex\fi
\ifx\PSTnodeLoaded\endinput   \else\input pst-node.tex\fi
\ifx\PSTXKeyLoaded\endinput   \else\input pst-xkey \fi
%
\def\fileversion{1.08}
\def\filedate{2024/08/13}
%
\edef\TheAtCode{\the\catcode`\@}
\catcode`\@=11
%
\pst@addfams{pst-coil}
\pstheader{pst-coil.pro}
\edef\pst@theheaders{\pst@theheaders,pst-coil.pro}
\def\pst@CoilDict{tx@CoilDict begin }
%
\def\tx@CoilLoop  {\pst@CoilDict CoilLoop   end }
\def\tx@Coil      {\pst@CoilDict Coil       end }
\def\tx@AltCoil   {\pst@CoilDict AltCoil    end }
\def\tx@ZigZag    {\pst@CoilDict ZigZag     end }
\def\tx@ZigZagCirc{\pst@CoilDict ZigZagCirc end }
\def\tx@Sin       {\pst@CoilDict Sin        end }
%
\define@key[psset]{pst-coil}{coilwidth}[1cm]{\pst@getlength{#1}\psk@coilwidth}
\define@key[psset]{pst-coil}{coilheight}[1]{\pst@checknum{#1}\pscoilheight}
\define@key[psset]{pst-coil}{coilarmA}[0.5cm]{\pst@getlength{#1}\psk@coilarmA}
\define@key[psset]{pst-coil}{coilarmB}[0.5cm]{\pst@getlength{#1}\psk@coilarmB}
\define@key[psset]{pst-coil}{coilarm}[0.5cm]{%
  \pst@getlength{#1}\psk@coilarmA%
  \let\psk@coilarmB\psk@coilarmA}
\define@key[psset]{pst-coil}{coilaspect}[45]{\pst@getangle{#1}\psk@coilaspect}
\define@key[psset]{pst-coil}{coilinc}[10]{\pst@getangle{#1}\psk@coilinc}
%%%%%% alternative macro
\define@key[psset]{pst-coil}{N}[10]{\def\psk@NoOfSpirals{#1 }}
\define@key[psset]{pst-coil}{R}[1]{\def\psk@InitialWidth{#1 }}%  in cm
\define@key[psset]{pst-coil}{l0}[10]{\def\psk@InitialLength{#1 }}
\define@key[psset]{pst-coil}{alpha}[15]{\def\psk@coil@Alpha{#1 }}
\define@key[psset]{pst-coil}{arm}[0]{\def\psk@coil@arm{#1 }}
\define@key[psset]{pst-coil}{nS}[25]{\def\psk@coil@NoOfPointsPerCoil{#1 }}
\psset[pst-coil]{N=10,R=0.5,alpha=15,arm=0,l0=5,nS=25}

\psset[pst-coil]{coilaspect=45,coilarm=.5cm,coilheight=1,coilwidth=1cm,coilinc=10}
%
\def\pscoil{\def\pst@par{}\pst@object{pscoil}}
\def\pscoil@i{\pst@getarrows\pscoil@ii}
\def\pscoil@ii(#1){\@ifnextchar({\pscoil@iii{1}(#1)}{\pscoil@iii{\z@}(0,0)(#1)}}
\def\pscoil@iii#1(#2)(#3){%
  \begin@OpenObj
  \pst@getcoor{#2}\pst@tempa
  \pst@getcoor{#3}\pst@tempb
  \pst@optcp{#1}\pst@tempa
  \addto@pscode{%
    \pst@tempa \pst@tempb
    \psk@coilwidth \pscoilheight
    \psk@coilarmA \psk@coilarmB
    \psk@coilaspect \psk@coilinc
    \tx@Coil }%
    \showpointsfalse
  \end@OpenObj}
%
\def\psCoil{\def\pst@par{}\pst@object{psCoil}}
\def\psCoil@i#1#2{%
  \begin@AltOpenObj
  \showpointsfalse
  \pst@getangle{#1}\pst@tempa
  \pst@getangle{#2}\pst@tempb
  \addto@pscode{%
    \pst@tempa
    \pst@tempb
    \psk@coilwidth
    \pscoilheight
    \psk@coilaspect
    \psk@coilinc
    \tx@AltCoil  
    \@nameuse{psls@\pslinestyle} }%
  \end@OpenObj}
%
\define@key[psset]{pst-coil}{bow}[0]{%
  \pst@getlength{#1}\psk@bow% 
  \pst@dimm=\psk@bow pt%
  \pst@absdim{\pst@dimm}{\pst@dimn}%
  \ifdim\pst@dimn<1pt \def\psk@bow{0}\fi}%
\psset[pst-coil]{bow=0}
%
\def\pszigzag{\def\pst@par{}\pst@object{pszigzag}}
\def\pszigzag@i{\pst@getarrows\pszigzag@ii}
\def\pszigzag@ii(#1){\@ifnextchar({\pszigzag@iii{1}(#1)}{\pszigzag@iii{\z@}(0,0)(#1)}}
\def\pszigzag@iii#1(#2)(#3){%
  \addbefore@par{bow=0}%
  \begin@OpenObj%
  \pst@getcoor{#2}\pst@tempA%
  \pst@getcoor{#3}\pst@tempB%
  \pst@optcp{#1}\pst@tempA%
  \addto@pscode{%
    \pst@tempA
    \pst@tempB
    \pscoilheight
    \psk@coilwidth
    \psk@coilarmA
    \psk@coilarmB 
    \ifdim\psk@bow pt=\z@ \tx@ZigZag \else \psk@bow\space \tx@ZigZagCirc \fi
    \psline@iii
    \tx@Line }%
  \end@OpenObj}
%
\def\nccoil{\pst@object{nccoil}}
\def\nccoil@i{\check@arrow{\nccoil@ii}}
\def\nccoil@ii#1#2{\nc@object{Open}{#1}{#2}{.5}{%
  \tx@NCCoor
  tx@Dict begin
% DG/SR modification begin - Mar. 19, 1998 - Patch 2
  4 2 roll
% DG/SR modification end
  \psk@coilwidth \pscoilheight
  \psk@coilarmA \psk@coilarmB
  \psk@coilaspect \psk@coilinc
  \tx@Coil 
  end }}
%
\def\pccoil{\def\pst@par{}\pst@object{pccoil}}
\def\pccoil@i{\pc@object\nccoil@ii}
%
\def\nczigzag{\pst@object{nczigzag}}
\def\nczigzag@i{\check@arrow{\nczigzag@ii}}
\def\nczigzag@ii#1#2{\nc@object{Open}{#1}{#2}{.5}{%
  \tx@NCCoor
  tx@Dict begin
% DG/SR modification begin - Mar. 19, 1998 - Patch 2
  4 2 roll
% DG/SR modification end
  \pscoilheight
  \psk@coilwidth
  \psk@coilarmA
  \psk@coilarmB
  \ifdim\psk@bow pt=\z@\tx@ZigZag\else\psk@bow\space\tx@ZigZagCirc\fi 
  \psline@iii
  \tx@Line
  end }}
%
\def\pczigzag{\def\pst@par{}\pst@object{pczigzag}}
\def\pczigzag@i{\pc@object\nczigzag@ii}
%
\def\pst@checkUnit#1#2{\expandafter\pst@checkUnit@i#1!!#2}
% 3.5 relative value                       \def\pst@roundValue{-1}
% 3.5mm absolute value                                        {-1}
% *3.5mm absolute value, with real->int conversion (truncate) {0}
% **3.5mm absolute value, with real->int conversion (round)   {1}
\def\pst@checkUnit@i{\@ifnextchar*%
  {\def\pst@roundValue{0 }\pst@checkUnit@ii}%
  {\def\pst@roundValue{-1 }\pst@checkUnit@iii**}}
\def\pst@checkUnit@ii*{\@ifnextchar*%
  {\def\pst@roundValue{1 }\pst@checkUnit@iii*}%
  {\pst@checkUnit@iii**}}
\def\pst@checkUnit@iii**#1!!#2{%
  \edef\ps@next{#1}%
  \ifx\ps@next\@empty\let\pst@num\z@%
  \else\expandafter\pst@@checknum\ps@next..\@nil%
  \fi%
  \ifnum\pst@num=\z@\pst@getlength{#1}{#2}\def\pst@relativePeriod{false }%
  \else%
    \def\pst@relativePeriod{true }%
    \edef#2{\ifnum\pst@num=\tw@-\fi\the\pst@cntg.%
    \expandafter\@gobble\the\pst@cnth\space}%
  \fi}
%
\define@key[psset]{pst-coil}{periods}[1]{\pst@checkUnit{#1}{\psk@periods}}
\define@key[psset]{pst-coil}{amplitude}[1]{\def\psk@amplitude{#1 }}
\define@key[psset]{pst-coil}{ppoints}[360]{\def\psk@ppoints{#1 }}
\define@key[psset]{pst-coil}{function}[sin]{\def\psk@function{#1 }}
\psset[pst-coil]{periods=1,amplitude=1,ppoints=360,function=sin}
%
\def\pssin{\pst@object{pssin}}
\def\pssin@i{\pst@getarrows\pssin@ii}
\def\pssin@ii(#1){\@ifnextchar({\pssin@iii{1}(#1)}{\pssin@iii{\z@}(0,0)(#1)}}
\def\pssin@iii#1(#2)(#3){%
  \begin@OpenObj
  \pst@getcoor{#2}\pst@tempa
  \pst@getcoor{#3}\pst@tempb
  \pst@optcp{#1}\pst@tempa
  \addto@pscode{%
    \pst@tempa \pst@tempb
    \psk@periods 
    \pst@relativePeriod 
    \pst@roundValue
    \psk@amplitude \pst@number\psyunit mul
    \psk@coilarmA \psk@coilarmB 
    \psk@ppoints
    { \psk@function }
    \tx@Sin
  }%
  \showpointsfalse
  \end@OpenObj}
%
\def\ncsin{\pst@object{ncsin}}
\def\ncsin@i{\check@arrow{\ncsin@ii}}
\def\ncsin@ii#1#2{\nc@object{Open}{#1}{#2}{.5}{%
  \tx@NCCoor
  tx@Dict begin
  4 2 roll
  \psk@periods 
  \pst@relativePeriod 
  \pst@roundValue
  \psk@amplitude \pst@number\psyunit mul
  \psk@coilarmA \psk@coilarmB 
  \psk@ppoints
  { \psk@function }
  \tx@Sin 
  end }}
%
\def\pcsin{\def\pst@par{}\pst@object{pcsin}}
\def\pcsin@i{\pc@object\ncsin@ii}
%
\def\psAltCoil{\def\pst@par{}\pst@object{psAltCoil}}
\def\psAltCoil@i{\pst@getarrows\psAltCoil@ii}
\def\psAltCoil@ii(#1){\@ifnextchar({\psAltCoil@iii{1}(#1)}{\psAltCoil@iii{\z@}(0,0)(#1)}}
\def\psAltCoil@iii#1(#2)(#3){%
  \begin@OpenObj
  \pst@getcoor{#2}\pst@tempa
  \pst@getcoor{#3}\pst@tempb
  \addto@pscode{%
    \pst@tempa \pst@tempb
    /y1 ED /x1 ED /y0 ED /x0 ED
     x0 y0 translate
     y1 y0 sub x1 x0 sub 2 copy Pyth
    /Longueur ED
     Atan rotate
    /NoOfSpirals \psk@NoOfSpirals def % nombre de spires
    /Alpha \psk@coil@Alpha def
    /R_0 \psk@InitialWidth \pst@number\psunit mul def
    /arm \psk@coil@arm \pst@number\psunit mul def
    /l_0 \psk@InitialLength \pst@number\psunit mul def
    /nS \psk@coil@NoOfPointsPerCoil def
    /Pas_0 l_0 NoOfSpirals div def
    /Pas Longueur 2 arm mul sub NoOfSpirals div def
    % longueur de l'hélice sur un tour
    % = sqrt(4*pi^2*R^2+p^2)
    /Longueur_Helice_pas 39.47842 R_0 dup mul mul Pas_0 dup mul add sqrt def
    % R=1/(2*pi)*sqrt(L^2-p^2)
    /Radius 0.1591549 Longueur_Helice_pas dup mul Pas dup mul sub sqrt mul def
    /xH {Radius 360 t mul cos mul Radius sub} def
    /yH {Radius 360 t mul sin mul} def
    /zH {Pas t mul arm add} def
    /Inc 1 NoOfSpirals nS mul div def % nS(50) points par spires
     1 setlinejoin
      1 0 0 0 ArrowA arm 0 lineto
     0 Inc NoOfSpirals {
       /t ED
       zH xH Alpha tan mul sub
       yH
       lineto
     } for
     Longueur 0
     ArrowB lineto
   }%
  \end@OpenObj}
%
\catcode`\@=\TheAtCode\relax
\endinput
%%
%% END pst-coil.tex
