%%%
% Jeton
%%%
\def\filedateDivCourte{2026/02/16}%
\def\fileversionDivCourte{0.1a}%
\message{-- \filedateDivCourte\space v\fileversionDivCourte}%
%
\setKVdefault[DivCourte]{CouleurC=PfCdblue,CouleurD=PfCdred,CouleurU=PfCdgreen,Etape={},EtapeR={}}

\makeatletter
\NewDocumentCommand\DivisionCourte{om}{%
  \useKVdefault[DivCourte]%
  \setKV[DivCourte]{#1}%
  \StrCut{#2}{/}{\PfCNume}{\PfCDeno}%
  \StrLen{\PfCNume}[\PfCLongA]%
  \StrLen{\PfCDeno}[\PfCLongB]%
  \PfC@MPBuildDivCourt{\PfCNume}{\PfCDeno}{\PfCLongA}{\PfCLongB}%
}%

\NewDocumentCommand\PfC@MPBuildDivCourt{mmmm}{%
  \mplibforcehmode%
  \mplibnumbersystem{double}%
  \begin{mplibcode}
    boolean AvecEtapes;
    \ifemptyKV[DivCourte]{Etape}{AvecEtapes=false;}{%
      AvecEtapes=true;
      Etape=\useKV[DivCourte]{Etape};
      \ifemptyKV[DivCourte]{EtapeR}{EtapeR=Etape}{EtapeR=\useKV[DivCourte]{EtapeR}};
    }%
    color Coul[];
    Coul0=\useKV[DivCourte]{CouleurD};
    Coul1=\useKV[DivCourte]{CouleurC};
    Coul2=\useKV[DivCourte]{CouleurU};
    Toto=#1;
    nb:=0;
    % Affichage Nume
    picture Nume[];
    numeric PE[];
    for k=#3-1 downto 1:
      alpha:=1 for l=1 upto k:*10 endfor;
      PE[nb]:=Toto div alpha;
      Nume[nb]=image(
        label.top(decimal(PE[nb]),u*(0.5*nb,0));
      );
      draw Nume[nb] withcolor Coul[(#3+1-nb) mod 3];
      nb:=nb+1;
      Toto:=Toto mod alpha;
    endfor;
    PE[nb]=Toto mod 10;
    Nume[nb]=image(  
      label.top(decimal(Toto mod 10),u*(0.5*nb,0));
    );
    draw Nume[nb] withcolor Coul[(#3+1-nb) mod 3];
    % Pour la barre
    %trace u*(-0.5,0)--u*0.5*(nb+1,0);
    pair A,B,M;
    A=iso(u*(-0.5,0),u*0.5*(nb+1,0));
    B=u*0.5*(nb+1,0);
    %% Affichage Deno
    Toto:=#2;
    nb:=0;
    for k=#4-1 downto 1:
      alpha:=1 for l=1 upto k:*10 endfor;
      label.bot(decimal(Toto div alpha),A+u*(-(#4 div 2)*0.25+nb*0.5,0));
      nb:=nb+1;
      Toto:=Toto mod alpha;
    endfor;
    label.bot(decimal(Toto mod 10),A+u*0.5*(-(#4 div 2)*0.25+nb*0.5,0));
    % signe égal
    draw (B+u*(0.15,0.05))--(B+u*(0.3,0.05));
    draw (B+u*(0.15,-0.05))--(B+u*(0.3,-0.05));
    % résultat
    dividende=PE[0];
    diviseur=#2;
    if AvecEtapes:
      % phantom
      M:=iso(u*0.5*(0+0.75+1,1),center(Nume[0]));
      trace ((fullcircle xscaled 0.4u yscaled 0.75u) shifted M) rotatedabout(M,15) withcolor white;
    %
      if EtapeR>0:
        for k=0 upto min(#3-1,EtapeR-1):
          beta:=dividende div diviseur;
          gamma:=dividende mod diviseur;
          drawoptions(withcolor Coul[(#3+1-k) mod 3]);
          label.rt(decimal(beta),B+u*(0.3+k*0.5,0));
          dividende:=PE[k+1]+gamma*10;
        endfor;
        drawoptions();
      fi;
      if EtapeR=#3+1:
        if #1 mod #2>0:
          label.rt(TEX("reste : "&decimal(#1 mod #2)),B+(u*(0.3+#3*0.5,0)));
        fi;
      fi;
      k:=0;
      dividende:=PE[0];
      diviseur:=#2;
      if Etape>0:
        trace (fullcircle scaled 0.35u) shifted center(Nume[k]) withcolor Coul[(#3+1-k) mod 3];
        forever: exitif k>Etape-2;
          beta:=dividende mod diviseur;
          if beta>0:
            M:=iso(u*0.5*(k+0.75+1,1),center(Nume[k]));
            trace ((fullcircle xscaled 0.4u yscaled 0.75u) shifted M) rotatedabout(M,15) withcolor Coul[(#3-k) mod 3];
          else:
            trace (fullcircle scaled 0.4u) shifted center(Nume[k+1]) withcolor Coul[(#3-k) mod 3];
          fi;
          label(TEX("\scriptsize"& decimal(beta)),u*0.5*(k+0.75,1.05));
          k:=k+1;
          dividende:=PE[k]+beta*10;
        endfor;
      fi;
    else:
      for k=0 upto #3-1:
        beta:=dividende div diviseur;
        gamma:=dividende mod diviseur;
        drawoptions(withcolor Coul[(#3+1-k) mod 3]);
        label.rt(decimal(beta),B+u*(0.3+k*0.5,0));
        dividende:=PE[k+1]+gamma*10;
      endfor;
      drawoptions();
      if #1 mod #2>0:
        label.rt(TEX("reste : "&decimal(#1 mod #2)),B+(u*(0.3+#3*0.5,0)));
      fi;
      % 
      k:=0;
      dividende:=PE[0];
      diviseur:=#2;
      trace (fullcircle scaled 0.35u) shifted center(Nume[k]) withcolor Coul[(#3+1-k) mod 3];
      forever: exitif k>#3-2;
        beta:=dividende mod diviseur;
        if beta>0:
          M:=iso(u*0.5*(k+0.75,1),center(Nume[k+1]));
          trace ((fullcircle xscaled 0.4u yscaled 0.75u) shifted M) rotatedabout(M,15) withcolor Coul[(#3-k) mod 3];;
        else:
          trace (fullcircle scaled 0.4u) shifted center(Nume[k+1]) withcolor Coul[(#3-k) mod 3];
        fi;
        label(TEX("\scriptsize"& decimal(beta)),u*0.5*(k+0.75,1.05));
        k:=k+1;
        dividende:=PE[k]+beta*10;
      endfor;
      beta:=#1 mod #2;
      if beta>0:
        label(TEX("\scriptsize"& decimal(beta)),u*0.5*(k+0.75,1.05));
      fi;
    fi;
    % Affichage Barre
    trace u*(-0.5,0)--u*0.5*(#3,0);
  \end{mplibcode}
  \mplibnumbersystem{scaled}%
}%
\makeatother