%% $Id: pst-spirograph.tex 1233 2026-04-12 08:12:46Z herbert $
%%
%% This is file `pst-spirograph.tex',
%%
%% IMPORTANT NOTICE:
%%
%% Package `pst-spirograph.tex'
%%
%% COPYRIGHT 2026- by 
%%     Manuel Luque <manuel.luque27@gmail.com>
%%     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 CTAN:/macros/latex/base/lppl.txt.
%%
%% DESCRIPTION:
%%   `pst-spirograph' is a PSTricks package to show spirograph curves
%%
%%
\csname PSTSPIROGRAPHLoaded\endcsname
\let\PSTSPIROGRAPHLoaded\endinput
% Requires some packages
\RequirePackage{expl3}
\ExplSyntaxOn
  \cs_new_eq:NN \ps@Calc \fp_eval:n
\ExplSyntaxOff

\ifx\PSTricksLoaded\endinput \else \input pstricks \fi
\ifx\PSTXKeyLoaded\endinput  \else \input pst-xkey \fi
\ifx\PSTthreeDLoaded\endinput\else \input pst-3d   \fi
\ifx\PSTnodesLoaded\endinput \else \input pst-node \fi
\ifx\PSTplotLoaded\endinput  \else \input pst-plot \fi


\def\fileversion{0.52}
\def\filedate{2026/04/11}
\message{`PSTSPIROGRAPH' v\fileversion, \filedate\ (ml,hv)}

\edef\PstAtCode{\the\catcode`\@} 
\catcode`\@=11\relax 
\pst@addfams{pst-spirograph}
\pstheader{pst-spirograph.pro}
\define@key[psset]{pst-spirograph}{Z1}[20]{\def\psk@ZA{#1 }}
\psset[pst-spirograph]{Z1=20}
\define@key[psset]{pst-spirograph}{Z2}[10]{\def\psk@ZB{#1 }}
\psset[pst-spirograph]{Z2=10}
\define@key[psset]{pst-spirograph}{m}[0.5]{\def\psk@m{#1 }}
\psset[pst-spirograph]{m=0.5}
\define@key[psset]{pst-spirograph}{ap}[20]{\def\psk@ap{#1 }}
\psset[pst-spirograph]{ap=20}
\define@key[psset]{pst-spirograph}{polarangle}[0]{\def\psk@polarangle{#1 }}
\psset[pst-spirograph]{polarangle=0}
\define@key[psset]{pst-spirograph}{holenumber}[1]{\def\psk@holenumber{#1 }}
\psset[pst-spirograph]{holenumber=1}
\define@key[psset]{pst-spirograph}{thetamax}[360]{\def\psk@thetamax{#1 }}
\psset[pst-spirograph]{thetamax=360}

\define@key[psset]{pst-spirograph}{Rarct}{\def\psk@Rarct{#1 }}
\psset[pst-spirograph]{Rarct=0.1}
\define@key[psset]{pst-spirograph}{wheelrotation}{\def\psk@wheelrotation{#1 }}
\psset[pst-spirograph]{wheelrotation=0}

\define@boolkey[psset]{pst-spirograph}[Pst@spirograph@]{int}[true]{}
\psset[pst-spirograph]{int=false}

\define@boolkey[psset]{pst-spirograph}[Pst@spirograph@]{notdrawWheel}[true]{}
\psset[pst-spirograph]{notdrawWheel=false}

%
\define@key[psset]{pst-spirograph}{color1}[{[rgb]{0.625 0.75 1}}]{\pst@getcolor{#1}\pscolora}
\psset[pst-spirograph]{color1={[rgb]{0.625 0.75 1}}}
\define@key[psset]{pst-spirograph}{color2}[{[rgb]{0.75 1 0.75}}]{\pst@getcolor{#1}\pscolorb}
\psset[pst-spirograph]{color2={[rgb]{0.75 1 0.75}}}
\define@key[psset]{pst-spirograph}{circlescolor}[red]{\pst@getcolor{#1}\pscolorc}
\psset[pst-spirograph]{circlescolor=red}
\define@key[psset]{pst-spirograph}{curvecolor}[red]{\pst@getcolor{#1}\pscolord}
\psset[pst-spirograph]{curvecolor=red}
%
\newdimen\pscurvewidth
\define@key[psset]{pst-spirograph}{curvewidth}[1pt]{\pssetlength\pscurvewidth{#1}}
\psset[pst-spirograph]{curvewidth=1pt}
%% === Option pour dessiner le type d'engrenage ---------------------
\define@boolkey[psset]{pst-spirograph}[Pst@spirograph@]{inner}[true]{}
\psset[pst-spirograph]{inner}
%% === pour dessiner cercle de base et cercle primitif
\define@boolkey[psset]{pst-spirograph}[Pst@spirograph@]{circles}[true]{}
\psset[pst-spirograph]{circles=false}

%% hole position
\define@key[psset]{pst-spirograph}{HolePos}{\def\psk@HolePos{#1 }}
\psset[pst-spirograph]{HolePos=1.5} % 1<HolePos<2

\define@boolkey[psset]{pst-spirograph}[Pst@spirograph@]{gearwheels}[true]{}
\psset[pst-spirograph]{gearwheels}

\define@boolkey[psset]{pst-spirograph}[Pst@spirograph@]{TypeA}[true]{}
\psset[pst-spirograph]{TypeA}

\define@boolkey[psset]{pst-spirograph}[Pst@spirograph@]{RubberBand}[true]{}
\psset[pst-spirograph]{RubberBand=false}

\define@key[psset]{pst-spirograph}{theta1}{\def\psk@thetaA{#1}}
\define@key[psset]{pst-spirograph}{theta2}{\def\psk@thetaB{#1}}
\psset[pst-spirograph]{theta1=-90,theta2=90}


\def\setSpirographDefaults{%
  \psset[pst-spirograph]{Z1=20,Z2=10,
,m=0.5,,ap=20,,polarangle=0,,holenumber=1,,thetamax=360,
,Rarct=0.1,,wheelrotation=0,,int=false,,notdrawWheel=false
,color1={[rgb]{0.625 0.75 1}},color2={[rgb]{0.75 1 0.75}},circlescolor=red
,curvecolor=red,curvewidth=1pt,inner,circles=false,HolePos=1.5
,gearwheels,TypeA,RubberBand=false,theta1=-90,theta2=90}}

\def\psSpirograph{\def\pst@par{}\pst@object{psSpirograph}}
\def\psSpirograph@i{\@ifnextchar({\psSpirograph@ii}{\psSpirograph@ii(0,0)}}
\def\psSpirograph@ii(#1){%
  \begin@SpecialObj
  \ifPst@spirograph@RubberBand \psSpirograph@iv(#1)\else\psSpirograph@iii(#1)\fi
}  
\def\psSpirograph@iii(#1){%
  \pst@@getcoor{#1}%
  \addto@pscode{
    tx@spirographDict begin
    \pst@coor /t@@y ED /t@@x ED
    1 setlinejoin
    /cm { \pst@number\psunit mul } bind def
    /Z1 \psk@ZA def
    /m1 \psk@m def
    /Z2 \psk@ZB def
    /m2 \psk@m def
    /ap \psk@ap def
    /polarAngle  \psk@polarangle def
    /setlinedash { [ \psk@dash\space ] 0 setdash } def 
    /ni \psk@holenumber def ni 8 gt { /ni 8 def } if
    /thetamax \psk@thetamax def
%
    /ifinner \ifPst@spirograph@inner true \else false \fi def
    /ifcircles \ifPst@spirograph@circles true \else false \fi def 
    /iffill \ifx\psk@fillstyle\psfs@none false \else true \fi def
    /Fill { \tx@setTransparency fill stroke } def
    % on teste si le nombre de dents est pair
      Z1 2 mod 1 eq { /Z1 Z1 1 sub def } if
      Z2 2 mod 1 eq { /Z2 Z2 1 sub def } if
      /HolePos \psk@HolePos def
      HolePos 0 lt HolePos 2 ge or { /HolePos 1.5 def } if
      /Fill { \psk@opacityalpha .setfillconstantalpha fill } def
      /R1 Z1 m1 mul 2 div def
      /R2 Z2 m1 mul 2 div def
      /ki 1 ni 9 div sub def
      /r2 m1 Z2 mul 2 div def
      /qi Z1 Z2 div 1 sub def
    /qi2 Z1 Z2 div 1 add def
    thetamax 0 le {/pas -1 def}{/pas 1 def} ifelse
% tableau des points de l'hypocycloide
    /tabSpirograph [ 0 pas thetamax {/i_ exch def [i_ coorPen]} for ] def
% tableau des points de l'epicycloide
    /tabSpirograph2 [ 0 pas thetamax {/i_ exch def [i_ coorPen2]} for ] def
    /nPts tabSpirograph length 1 sub def
    /nPts2 tabSpirograph2 length 1 sub def
    /color1 {\pst@usecolor\pscolora } def
    /color2 {\pst@usecolor\pscolorb } def
    /circlescolor {\pst@usecolor\pscolorc } def
    /curvecolor {\pst@usecolor\pscolord } def
    /linecolor  {\pst@usecolor\pslinecolor} def
    /fillopacity \psk@opacityalpha def
    /GetCurveWidth { \pst@number\pscurvewidth } def
    /SetCurveWidth { \pst@number\pscurvewidth SLW } def
%
    \ifPst@spirograph@TypeA
      Roue1
      Roue2
    \else
      %%%% definition de la roue dentee %%%%%%
      thetamax 0 le { /pas -1 def }{ /pas 1 def } ifelse
      /tabSpirograph [ 0 pas thetamax {/i_ exch def [ i_ coorPen3 ] } for ] def
      %%% Les dessins de l'engrenage %%%%%%
      %%%%%%%%%% Roue N�2 %%%%%%%%%%%
      %%%%%%%%%% roue fixe %%%%%%%%%%
      \ifPst@spirograph@gearwheels
        /wheel 2 def
        gsave
        m2 Z2 Datas1
        Calculs
        Beta_ rotate
        Roue3
        closepath
%        \ifx\psk@fillstyle\empty\else color2 Fill \fi
        iffill { color2 Fill } if
        Roue3
        closepath
        linecolor
        stroke
        grestore
        gsave
        %% la vis de fixation de la roue sur la feuille
        0 0 0.5 cm 0 360 arc
        0.8 0.8 0  setrgbcolor
        fill
        0 setgray
        0 0 0.5 cm 0 360 arc
        stroke
        1 setlinewidth
        -0.5 cm 0 moveto 0.5 cm 0 lineto
        0 -0.5 cm moveto 0 0.5 cm lineto
        stroke
        grestore
        \ifPst@spirograph@circles Circles \fi
      \fi
      gsave
      tabSpirograph 0 get aload pop moveto
      1 1 tabSpirograph length 1 sub {/nP exch def tabSpirograph nP get aload pop lineto } for
      curvecolor
      \pst@number\pscurvewidth SLW
      stroke
      grestore
      %%%%%%%%%% Roue N�1 %%%%%%%%%%%%%%%%%
      %%%%%%%%%% roue tournante %%%%%%%%%%
      \ifPst@spirograph@gearwheels
        /wheel 1 def
        gsave
        m2 Z2 Datas1
        Calculs
        /a@ex m2 Z1 Z2 add mul 2 div cm def % entraxe engrenage exterieur
        /a@in m2 Z1 Z2 sub mul 2 div cm def % entraxe engrenage interieur
        a@in polarAngle cos mul a@in polarAngle sin mul translate
        polarAngle Z1 Z2 sub Z1 div mul rotate
        m1 Z1
        Datas2
        Calculs
        Beta_ rotate
        Roue3
        COURONNE
        closepath
	iffill { color1 Fill } if
%        \ifx\psk@fillstyle\@empty\else color1 Fill \fi
        Roue3
        closepath
        linecolor
        stroke
        COURONNE
        closepath
        linecolor
        stroke
        Roue3
        closepath
        linecolor
        stroke
        plaquette
        drawHole
        \ifPst@spirograph@circles Circles \fi
        grestore
      \else
        gsave
        m2 Z2 Datas1
        Calculs
        /a@ex m2 Z1 Z2 add mul 2 div cm def % entraxe engrenage exterieur
        /a@in m2 Z1 Z2 sub mul 2 div cm def % entraxe engrenage interieur
        a@in polarAngle cos mul a@in polarAngle sin mul translate
        polarAngle Z1 Z2 sub Z1 div mul rotate
        m1 Z1
        Datas2
        Calculs
        Beta_ rotate
        Beta_ neg rotate
        drawHole
        grestore
      \fi
    \fi
    end
  }%
  \end@SpecialObj
  \ignorespaces}
%
\def\psSpirograph@iv(#1){%
  \def\RA{\ps@Calc{\psk@ZA*\psk@m/2}}%
  \def\RB{\ps@Calc{\psk@ZB*\psk@m/2}}% les rayons
  \def\OB{\ps@Calc{\RA+\RB}}%
  \def\RAp{\ps@Calc{(\RA*2-2.5*0.2)/2}}% cercles de pied
  \def\RBp{\ps@Calc{(\RB*2-2.5*0.2)/2}}%
  % la vitesse de rotation
  \def\OMEGAA{-1}%
  \def\OMEGAB{\ps@Calc{(-\OMEGAA)*\psk@ZA/\psk@ZB}}%
  \def\ANGLE{\ps@Calc{\psk@wheelrotation}}% degrees
  \def\ANGLErad{\ps@Calc{\ANGLE*3.14/180}}% radians
  \def\nombrePoints{\ps@Calc{trunc(2*\ANGLE+5,0)}}%
  % positionnement angulaire des 2 points de fixation de l'�lastique
  \def\thetaA{\ps@Calc{(\psk@thetaA)*3.14/180}}%
  \def\thetaB{\ps@Calc{(\psk@thetaB)*3.14/180}}%
  % les coordonn�es des 2 points
  \def\xA{\ps@Calc{0.9*\RAp*cos(\thetaA+\OMEGAA*\ANGLErad)}}%
  \def\yA{\ps@Calc{sin(\thetaA+\OMEGAA*\ANGLErad)*\RAp*0.9}}%
  \def\xB{\ps@Calc{cos(\thetaB+\OMEGAB*\ANGLErad)*\RBp*0.9}}%
  \def\yB{\ps@Calc{sin(\thetaB+\OMEGAB*\ANGLErad)*\RBp*0.9+\OB}}%
  % le milieu
  \def\xM{\ps@Calc{(\xA+\xB)/2}}\def\yM{\ps@Calc{(\yA+\yB)/2}}%
  \ThreeDput[normal=0 0 1](0,0,0){%
    \psgrid[subgriddiv=0,gridlabels=0pt]%\psframe(-5,-5)(5,7)
    \rput(0.05,-0.05){\psSpirographB[circles=false,polarangle=90,fillstyle=solid,color1=black,color2=black]}%
    \psSpirographB[circles=false,polarangle=90,fillstyle=solid]%
    \parametricplot[linecolor=red,plotpoints=\nombrePoints,algebraic,linewidth=0.1]{0}{\ANGLErad}{
                      (\RAp*0.9*cos(\thetaA+\OMEGAA*t)+\RBp*0.9*cos(\thetaB+\OMEGAB*t))/2|
                      (\RAp*0.9*sin(\thetaA+\OMEGAA*t)+\RBp*0.9*sin(\thetaB+\OMEGAB*t)+\OB)/2}%
    \psline{->}(0,0)(0,1)\psline{->}(0,0)(1,0)%
    % les cercles de contact
    \pscircle[linestyle=dotted](0,0){\RA}\pscircle[linestyle=dotted](0,\OB){\RB}%
    % les cercles de pied
    %\pscircle[linestyle=dotted](0,0){\RAp}
    %\pscircle[linestyle=dotted](0,\OB){\RBp}
  }%
  % les tiges de fixation des extr�mit�s de l'�lastique
  \ThreeDput[normal=0 1 0](\xA,\yA,0){\psline[linewidth=0.1]{-*}(0,0)(0,1)\pnode(0,1){P1}}%
  \ThreeDput[normal=0 1 0](\xB,\yB,0){\psline[linewidth=0.1]{-*}(0,0)(0,1)\pnode(0,1){P2}}%
  % le milieu
  \ThreeDput[normal=0 1 0](\xM,\yM,0){\pnodes(0,1){P3}(0,0){P4}}%
  \psline[linecolor=blue](P1)(P2)%
  \psline[linecolor=red]{->}(P3)(P4)%
  \psdot[linecolor=blue](P3)%
  \end@SpecialObj
  \ignorespaces}
%
\def\psSpirographB{\def\pst@par{}\pst@object{psSpirographB}}
\def\psSpirographB@i{\@ifnextchar({\psSpirographB@ii}{\psSpirographB@ii(0,0)}}
\def\psSpirographB@ii(#1){%
  \begin@SpecialObj
  \pst@@getcoor{#1}%
  \addto@pscode{%
    \pst@coor /t@@y ED /t@@x ED
    /cm {\pst@number\psunit mul } bind def
    /Z1 \psk@ZA def
    /m1 \psk@m def
    /Z2 \psk@ZB def
    /m2 \psk@m def
    /ap \psk@ap def
    /facteurRayonRaccord {\psk@Rarct mul} def
    /polarAngle  \psk@polarangle def
    /color1 {\pst@usecolor\pscolora } def
    /color2 {\pst@usecolor\pscolorb } def
    /colorcircles {\pst@usecolor\pscolorc } def
    /curvecolor  {\pst@usecolor\pscolord} def
    /linecolor  {\pst@usecolor\pslinecolor} def
    /ni \psk@holenumber def ni 8 gt { /ni 8 def } if
    /thetamax \psk@thetamax def
      Z1 2 mod 1 eq { /Z1 Z1 1 sub def } if
      Z2 2 mod 1 eq { /Z2 Z2 1 sub def } if
      /HolePos \psk@HolePos def
      HolePos 0 lt HolePos 2 ge or { /HolePos 1.5 def } if
      /Fill { \psk@opacityalpha .setfillconstantalpha fill } def
      /R1 Z1 m1 mul 2 div def
      /R2 Z2 m1 mul 2 div def
      /ki 1 ni 9 div sub def
      /r2 m1 Z2 mul 2 div def
      /qi Z1 Z2 div 1 sub def
    /qi2 Z1 Z2 div 1 add def
    %
    tx@spirographDict begin
    %%%% definition de la roue dentee %%%%%%
    /Roue3d {
      % arc de d�veloppante
      /tabArcDev [ 0 1 ThetaTdeg { /i@ exch def [i@ devCercle] } for ] def
      /n@ tabArcDev length def
      /tabDent [
	% l'arc de developpante initial
	tabArcDev aload pop
	% l'arc ce cercle de tete
	DeltaT 0.1 2Beta DeltaT sub {/i@ exch def [Rt cm i@ cos mul Rt cm i@ sin mul] } for 
	% le symetrique de l'arc de developpante par rapport a l'axe de la dent
	n@ 1 sub -1 0  { /compteur exch def [tabArcDev compteur get aload pop symAxe] } for 
      ] def
      % trac� de la dent
      /n2@ tabDent length def
      newpath
      ptC moveto
      0 1 Z@ 1 sub 
        { /i@ exch AngleDent mul def
	  \ifPst@spirograph@int
	    wheel 2 eq { ptA RotDent ptB RotDent Rarct arct ptB RotDent lineto }
            	       { ptA RotDent lineto ptB RotDent lineto } ifelse
	  \else
	    Rp Rb eq { ptA RotDent lineto ptB RotDent lineto }
            	     { ptA RotDent ptB RotDent Rarct arct ptB RotDent lineto } ifelse
	  \fi
	  0 1 n2@ 1 sub { /compteur exch def tabDent compteur get aload pop RotDent lineto } for
	  \ifPst@spirograph@int
	    wheel 2 eq { Rp Rb eq { ptA' RotDent lineto ptC' RotDent lineto }
                                  { ptA' RotDent ptC' RotDent Rarct arct ptC' RotDent lineto } ifelse } 
                                  { ptA' RotDent lineto ptC' RotDent lineto } ifelse
	  \else
	    Rp Rb eq { ptA' RotDent lineto ptC' RotDent lineto }
        	     { ptA' RotDent ptC' RotDent Rarct arct ptC' RotDent lineto } ifelse
	  \fi
	} for
    } def % Roue3d
    %
%%%%%%%%%%%%%%%%%5
      thetamax 0 le { /pas -1 def }{ /pas 1 def } ifelse
      /tabSpirograph [ 0 pas thetamax {/i_ exch def [ i_ coorPen3 ] } for ] def
%      gsave
%      tabSpirograph 0 get aload pop moveto
%      1 1 tabSpirograph length 1 sub {/nP exch def tabSpirograph nP get aload pop lineto } for
%      curvecolor
%      \pst@number\pscurvewidth SLW
%      stroke
%      grestore
%%%%%%%%%%%%%%%
    /AngleRotation \psk@wheelrotation def
    %%% Les dessins de l'engrenage %%%%%%
    %%%%%%%%%% Roue N�1 %%%%%%%%%%%%%%%%%
    \ifPst@spirograph@notdrawWheel
    \else
        /wheel 1 def
        gsave
        t@@x t@@y translate
        m1 Z1
        \ifPst@spirograph@int
	  Datas2 Calculs Beta_ AngleRotation sub rotate Roue3d COURONNE closepath
	  \ifx\psk@fillstyle\relax\else color1 fill \fi
	  Roue3d closepath linecolor stroke
	  COURONNE closepath linecolor stroke
	  \ifPst@spirograph@circles Circles \fi
        \else
	  Datas1 Calculs Beta_ AngleRotation sub rotate Roue3d AXE closepath
	  \ifx\psk@fillstyle\relax\else color1 fill \fi
	  Roue3d closepath linecolor stroke
	  AXE closepath
	  \ifx\psk@fillstyle\relax\else 0.8 setgray fill \fi
	  AXE closepath linecolor stroke
  	  CLAVETTE
	  \ifx\psk@fillstyle\relax\else 0 0.125 0.25 0.25 setcmykcolor fill \fi
	  CLAVETTE linecolor stroke
	  \ifPst@spirograph@circles  Circles \fi
        \fi
        grestore
    \fi
    %%%%%%%%%% Roue N�2 %%%%%%%%%%%%%%%%%
    /wheel 2 def
    gsave
    m2 Z2 Datas1
    Calculs
    /a@ex m2 Z1 Z2 add mul 2 div cm def % entraxe engrenage exterieur
    /a@in m2 Z1 Z2 sub mul 2 div cm def % entraxe engrenage interieur
    \ifPst@spirograph@int
      a@in polarAngle cos mul t@@x add a@in polarAngle sin mul t@@y add translate
      Beta_ Z1 Z2 div AngleRotation mul sub polarAngle Z1 Z2 sub Z2 div mul sub rotate
    \else
      a@ex polarAngle cos mul t@@x add a@ex polarAngle sin mul t@@y add translate
      DeltaSdeg DeltaP add neg 180 Z2 div add 180 add Z1 Z2 div AngleRotation mul add polarAngle Z1 Z2 add Z2 div mul add rotate
    \fi
    Roue3d
    AXE
    closepath
    \ifx\psk@fillstyle\relax\else color2 fill \fi
    Roue3d
    closepath
    linecolor
    stroke
    AXE
    closepath
    \ifx\psk@fillstyle\relax\else 0.8 setgray fill \fi
    AXE
    closepath
    linecolor
    stroke
    CLAVETTE
    \ifx\psk@fillstyle\relax\else 0 0.125 0.25 0.25 setcmykcolor fill \fi
    CLAVETTE
    linecolor
    stroke
    \ifPst@spirograph@circles Circles \fi
    grestore
    end
  }%
  \end@SpecialObj
  \ignorespaces
}% % fin de la commande PSTricks
%
\catcode`\@=\PstAtCode\relax
%
\endinput 