%%%
% Représenter un entier dans une base
%%%
\def\filedateRepEntier{2024/08/04}%
\def\fileversionRepEntier{0.1}%
\message{-- \filedateRepEntier\space v\fileversionRepEntier}%
%
\setKVdefault[ClesRBase]{Base=10,APlat=false,Echelle=1,Unite=false,Compact=false,Impression=false,ListeCouleurs={Tomato,LightSteelBlue,LightGreen,Cornsilk}}%

\makeatletter
\newcommand\RepresenterEntier[2][]{%
  \useKVdefault[ClesRBase]%
  \setKV[ClesRBase]{#1}%
  \ifboolKV[ClesRBase]{Unite}{%
    \begin{Geometrie}[TypeTrace="Espace",CoinBG={u*(-10,-10)}]%
      Base=\useKV[ClesRBase]{Base};
      Echelle=\useKV[ClesRBase]{Echelle};
      Initialisation(5,30,15,50*Echelle);
      color A,B,C,D,E,F,G,H;
      pointilles:="non";
      trace Pave(A,B,C,D,E,F,G,H)(0.1,0.1,0.1);
    \end{Geometrie}%
  }{%
    % On décompose le nombre
    \xintifboolexpr{#2>\fpeval{\useKV[ClesRBase]{Base}**4-1}}{Le nombre est trop grand pour être représenté.}{%
      \edef\PfC@RetiensDecomp{}%
      \edef\PfC@ResteConversion{#2}%
      \whiledo{\PfC@ResteConversion>\fpeval{\useKV[ClesRBase]{Base}-1}}{%
        \quotient{\PfC@ResteConversion}{\useKV[ClesRBase]{Base}}%
        \modulo{\PfC@ResteConversion}{\useKV[ClesRBase]{Base}}%
        \edef\PfC@RetiensDecomp{\the\remainder,\PfC@RetiensDecomp}%
        \edef\PfC@ResteConversion{\the\intquotient}%
      }%
      \edef\PfC@RetiensDecomp{\PfC@ResteConversion,\PfC@RetiensDecomp}%
    }%
%    On obtient la décomposition de #2 sous la forme \PfC@RetiensDecomp\\
    % transformation en liste
    \setsepchar{,}\ignoreemptyitems%
    \readlist*\ListeDesChiffres{\PfC@RetiensDecomp}%
    \edef\PfC@LongueurNombreEntier{\ListeDesChiffreslen}%
    \edef\PfC@ListeAvantCouleurs{\useKV[ClesRBase]{ListeCouleurs}}%
    \readlist*\PfCListeCouleurEntier{\PfC@ListeAvantCouleurs}%
    \reademptyitems%
    \xintifboolexpr{\PfC@LongueurNombreEntier==4}{%
      \edef\PfCNombreMilliers{\ListeDesChiffres[1]}%
      \edef\PfCNombreCentaines{\ListeDesChiffres[2]}%
      \edef\PfCNombreDizaines{\ListeDesChiffres[3]}%
      \edef\PfCNombreUnites{\ListeDesChiffres[4]}%
    }{%
      \xintifboolexpr{\PfC@LongueurNombreEntier==3}{%
        \edef\PfCNombreMilliers{0}%
        \edef\PfCNombreCentaines{\ListeDesChiffres[1]}%
        \edef\PfCNombreDizaines{\ListeDesChiffres[2]}%
        \edef\PfCNombreUnites{\ListeDesChiffres[3]}%
      }{%
        \xintifboolexpr{\PfC@LongueurNombreEntier==2}{%
          \edef\PfCNombreMilliers{0}%
          \edef\PfCNombreCentaines{0}%
          \edef\PfCNombreDizaines{\ListeDesChiffres[1]}%
          \edef\PfCNombreUnites{\ListeDesChiffres[2]}%
        }{%
          \edef\PfCNombreMilliers{0}%
          \edef\PfCNombreCentaines{0}%
          \edef\PfCNombreDizaines{0}%
          \edef\PfCNombreUnites{\ListeDesChiffres[1]}%
        }%
      }%
    }%
    \ifboolKV[ClesRBase]{APlat}{%
%        Ici !
%        Echelle : \useKV[ClesRBase]{Echelle};
      \begin{Geometrie}[Cadre="aucun"]%
        Echelle=\useKV[ClesRBase]{Echelle};
        u:=Echelle*1cm;
        NM=\PfCNombreMilliers;
        NC=\PfCNombreCentaines;
        ND=\PfCNombreDizaines;
        NU=\PfCNombreUnites;
        Base=\useKV[ClesRBase]{Base};
        color ColorEntier[];
        ColorEntier1=\PfCListeCouleurEntier[1];
        ColorEntier2=\PfCListeCouleurEntier[2];
        ColorEntier3=\PfCListeCouleurEntier[3];
        ColorEntier4=\PfCListeCouleurEntier[4];
        boolean Compact,Print;
        Compact=\useKV[ClesRBase]{Compact};
        Print=\useKV[ClesRBase]{Impression};
        picture TypePave[];
        TypePave0=image(
          remplis (unitsquare scaled 10u) withcolor if Print : blanc else: ColorEntier1 fi;
          trace (unitsquare scaled 10u);
          for k=0 upto Base-1:
            for l=0 upto Base-1:
              trace (unitsquare scaled u) shifted(k*u,l*u);
            endfor;
          endfor;
        );
      TypePave1=image(
        remplis (unitsquare xscaled u yscaled 10u) withcolor if Print : blanc else: ColorEntier2 fi;
        trace (unitsquare xscaled u yscaled 10u);
        for l=0 upto Base-1:
          trace (unitsquare scaled u) shifted(0,l*u);
        endfor;
      );
      TypePave2=image(
        remplis (unitsquare scaled u) withcolor if Print : blanc else: ColorEntier3 fi;
        trace (unitsquare scaled u);
      );
      if Compact:
      for k=0 upto (NM-1):
      %trace TypePave0 shifted ((Projette((k/Base,0,0))-Projette((0,0,0))));
      endfor;
      for k=0 upto (NC-1):
      %trace TypePave0 shifted(10*u*(k+0.1,0));
      endfor;
      for k=0 upto (ND-1):
      %trace TypePave1 shifted ((NC-1)*(10+0.1)+k*(1+0.1,0));
      endfor;
      for k=0 upto (NU-1):
      %trace TypePave3 shifted ((Projette((k/Base,2+4/Base,0))-Projette((0,0,0))));
      endfor;
      else:
      %for k=0 upto (NM-1):
      %trace TypePave0 shifted ((Projette((0,(1+1/Base)*(k-1),0))-Projette((0,0,0))));
      %endfor;
      for k=0 upto (NC-1):
      trace TypePave0 shifted(11*u*(k,0));
      endfor;
      for k=0 upto (ND-1):
      trace TypePave1 shifted(11*u*(NC,0)+k*u*(2,0));
      endfor;
      for k=0 upto (\PfCNombreUnites-1):
      trace TypePave2  shifted(11*u*(NC,0)+ND*u*(2,0)+u*(0,k));
      endfor;
      fi;
    \end{Geometrie}%
    }{%
    \begin{Geometrie}[TypeTrace="Espace",CoinBG={u*(-10,-10)},CoinHD={u*(20,20)}]
      Echelle=\useKV[ClesRBase]{Echelle};
      NM=\PfCNombreMilliers;
      NC=\PfCNombreCentaines;
      ND=\PfCNombreDizaines;
      NU=\PfCNombreUnites;
      Base=\useKV[ClesRBase]{Base};
      color ColorEntier[];
      ColorEntier1=\PfCListeCouleurEntier[1];
      ColorEntier2=\PfCListeCouleurEntier[2];
      ColorEntier3=\PfCListeCouleurEntier[3];
      ColorEntier4=\PfCListeCouleurEntier[4];
      boolean Compact,Print;
      Compact=\useKV[ClesRBase]{Compact};
      Print=\useKV[ClesRBase]{Impression};
      Initialisation(5,30,20,50*Echelle);
      color A,B,C,D,E,F,G,H;
      picture TypePave[];
      TypePave0=image(
      trace Pave(A,B,C,D,E,F,G,H)(1,1,1);
      currentpicture:=nullpicture;
      remplis polygone(A,B,C,H,E,F) withcolor if Print : blanc else: ColorEntier1 fi;
      for k=0 upto Base:
      trace chemin((k/Base)[A,B],(k/Base)[F,G],(k/Base)[E,H]);
      endfor;
      for k=0 upto Base:
      trace chemin((k/Base)[B,C],(k/Base)[G,H],(k/Base)[F,E]);
      endfor;
      for k=0 upto Base:
      trace chemin((k/Base)[A,F],(k/Base)[B,G],(k/Base)[C,H]);
      endfor;
      );
      color A,B,C,D,E,F,G,H;
      TypePave1=image(
      trace Pave(A,B,C,D,E,F,G,H)(1/Base,1,1);
      currentpicture:=nullpicture;
      remplis polygone(A,B,C,H,E,F) withcolor if Print : blanc else: ColorEntier2 fi;
      trace chemin(C,H,E);
      for k=0 upto Base:
      trace chemin((k/Base)[A,B],(k/Base)[F,G],(k/Base)[E,H]);
      endfor;
      for k=0 upto Base:
      trace chemin((k/Base)[A,F],(k/Base)[B,G],(k/Base)[C,H]);
      endfor;
      );
      color A,B,C,D,E,F,G,H;
      TypePave2=image(
      trace Pave(A,B,C,D,E,F,G,H)(1/Base,1/Base,1);
      currentpicture:=nullpicture;
      remplis polygone(A,B,C,H,E,F) withcolor if Print : blanc else: ColorEntier3 fi;
      trace chemin(C,H,E);
      trace chemin(A,F,E);
      trace chemin(B,G,F);
      for k=0 upto Base:
      trace chemin((k/Base)[A,F],(k/Base)[B,G],(k/Base)[C,H]);
      endfor;
      );
      color A,B,C,D,E,F,G,H;
      TypePave3=image(
      trace Pave(A,B,C,D,E,F,G,H)(1/Base,1/Base,1/Base);
      currentpicture:=nullpicture;
      remplis polygone(A,B,C,H,E,F) withcolor if Print : blanc else: ColorEntier4 fi;
      trace chemin(C,H,E);
      trace chemin(A,F,E);
      trace chemin(A,B,C);
      trace chemin(F,G,H);
      trace chemin(B,G);
      );
      if Compact:
      for k=0 upto (NM-1):
      trace TypePave0 shifted ((Projette((k/Base,0,0))-Projette((0,0,0))));
      endfor;
      for k=0 upto (NC-1):
      trace TypePave1 shifted ((Projette((k/Base,1+1/Base,0))-Projette((0,0,0))));
      endfor;
      for k=0 upto (ND-1):
      trace TypePave2 shifted ((Projette((k/Base,2+2/Base,0))-Projette((0,0,0))));
      endfor;
      for k=0 upto (NU-1):
      trace TypePave3 shifted ((Projette((k/Base,2+4/Base,0))-Projette((0,0,0))));
      endfor;
      else:
      for k=0 upto (NM-1):
      trace TypePave0 shifted ((Projette((0,(1+1/Base)*(k-1),0))-Projette((0,0,0))));
      endfor;
      for k=0 upto (NC-1):
      trace TypePave1 shifted ((Projette((0,(1+1/Base)*NM+(1+1/Base)*(k-1),0))-Projette((0,0,0))));
      endfor;
      for k=0 upto (ND-1):
      trace TypePave2 shifted ((Projette((0,(1+1/Base)*NM+(1+1/Base)*(NC-1)+(2/Base)+(2/Base)*(k-1),0))-Projette((0,0,0))));
      endfor;
      for k=0 upto (\PfCNombreUnites-1):
      trace TypePave3 shifted ((Projette((0,(1+1/Base)*NM+(1+1/Base)*(NC-1)+(2/Base)+(2/Base)*(ND-1),(2/Base)*(k)))-Projette((0,0,0))));
      endfor;
      fi;
    \end{Geometrie}
    }%
  }%
}%
\makeatother