%%%
% Représenter des tuiles
%%%
\def\filedateAireSimple{2026/06/14}%
\def\fileversionAireSimple{0.1b}%
\message{-- \filedateAireSimple\space v\fileversionAireSimple}%
%
\NewDocumentCommand\SachetTuile{om}{%
  \useKVdefault[TuileLibre]%
  \setKV[TuileLibre]{#1}%
  \mplibforcehmode%
  \begin{mplibcode}
    color couldiz,coulunit;
    couldiz=\useKV[TuileLibre]{CouleurSachetDizaine};
    coulunit=\useKV[TuileLibre]{CouleurSachetUnite};
    u:=3mm;
    dizaine=#2 div 10;
    unite=#2 mod 10;
    path reglettediz, regletteunit;
    reglettediz = (0,0)--u*(1,0)--u*(1,5)--u*(0,5)--cycle;
    regletteunit = (0,0)--u*(1,0)--u*(1,1)--u*(0,1)--cycle;
    for k=1 upto dizaine:
      fill (reglettediz shifted(u*(k-1)*(1.1,0))) withcolor couldiz;
      trace (reglettediz shifted(u*(k-1)*(1.1,0)));
    endfor;
    for k=1 upto unite:
      if (k mod 2)=1:
        fill regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-1) div 2)+u*(0,0.1*(k-1))) withcolor coulunit;
        trace regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-1) div 2)+u*(0,0.1*(k-1)));
      else:
        fill regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-2) div 2)+u*(1.15,0.1*(k-2))) withcolor coulunit;
        trace regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-2) div 2)+u*(1.15,0.1*(k-2)));
      fi;
    endfor;
    path Sachet;
    pair Msa[];
    Msa1=(llcorner currentpicture+u*(-2,-1));
    Msa2=(lrcorner currentpicture+u*(2,-1));
    Msa3=(urcorner currentpicture+u*(2,1));
    Msa4=(ulcorner currentpicture+u*(-2,2));
    Msa5=(ulcorner currentpicture+u*(-2,1));
    Msa6=Msa3+u*(0,0.2);
    Msa7=Msa5+u*(0,0.2);
    Sachet=Msa1{dir5}..{dir-5}Msa2{dir93}..{dir87}Msa3--(urcorner currentpicture+u*(2,2))--Msa4--Msa5{dir-87}..{dir-93}cycle;
    fill Sachet withcolor LightGray withtransparency(1,0.25);
    trace Sachet withcolor \mpcolor{LightGray!15!black};
    fill polygone(Msa3,Msa6,Msa7,Msa5) withcolor \mpcolor{NavyBlue!50!white};
    trace Msa5--Msa3 withpen pensquare scaled 1.25 withcolor NavyBlue;
    trace Msa6--Msa7 withpen pensquare scaled 1.25 withcolor NavyBlue;
  \end{mplibcode}%
}%

\NewDocumentCommand\SachetTuileRegroupe{om}{%
  \useKVdefault[TuileLibre]%
  \setKV[TuileLibre]{#1}%
  \StrCut{#2}{*}{\PfMFacteurSachet}{\PfMContenuSachet}%
  \mplibforcehmode%
  \begin{mplibcode}
    color couldiz,coulunit;
    couldiz=\useKV[TuileLibre]{CouleurSachetDizaine};
    coulunit=\useKV[TuileLibre]{CouleurSachetUnite};
    u:=3mm;
    dizaine=\PfMContenuSachet div 10;
    unite=\PfMContenuSachet mod 10;
    path reglettediz, regletteunit;
    reglettediz = (0,0)--u*(1,0)--u*(1,5)--u*(0,5)--cycle;
    regletteunit = (0,0)--u*(1,0)--u*(1,1)--u*(0,1)--cycle;
    for k=1 upto dizaine:
      fill (reglettediz shifted(u*(k-1)*(1.1,0))) withcolor couldiz;
      trace (reglettediz shifted(u*(k-1)*(1.1,0)));
    endfor;
    for k=1 upto unite:
      if (k mod 2)=1:
        fill regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-1) div 2)+u*(0,0.1*(k-1))) withcolor coulunit;
        trace regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-1) div 2)+u*(0,0.1*(k-1)));
      else:
        fill regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-2) div 2)+u*(1.15,0.1*(k-2))) withcolor coulunit;
        trace regletteunit shifted(u*(dizaine*1.1+0.15,0)+u*(0,(k-2) div 2)+u*(1.15,0.1*(k-2)));
      fi;
    endfor;
    picture VersionUne;
    VersionUne=currentpicture;
    for k=2 upto \PfMFacteurSachet:
      trace VersionUne shifted((k-1)*(abs(lrcorner VersionUne-llcorner VersionUne)+3mm),0);
    endfor;
  \end{mplibcode}%
}%

\NewDocumentCommand\SachetTuileDegroupe{om}{%
  \useKVdefault[TuileLibre]%
  \setKV[TuileLibre]{#1}%
  \StrCut{#2}{*}{\PfMFacteurSachet}{\PfMContenuSachet}%
  \mplibforcehmode%
  \begin{mplibcode}
    color couldiz,coulunit;
    couldiz=\useKV[TuileLibre]{CouleurSachetDizaine};
    coulunit=\useKV[TuileLibre]{CouleurSachetUnite};
    u:=3mm;
    dizaine=\PfMContenuSachet div 10;
    unite=\PfMContenuSachet mod 10;
    path reglettediz, regletteunit;
    reglettediz = (0,0)--u*(1,0)--u*(1,5)--u*(0,5)--cycle;
    regletteunit = (0,0)--u*(1,0)--u*(1,1)--u*(0,1)--cycle;
    for l=1 upto \PfMFacteurSachet:
      for k=1 upto dizaine:
        fill (reglettediz shifted(u*(k-1)*(1.1,0)+u*(l-1)*(dizaine+0.2,0))) withcolor couldiz;
        trace (reglettediz shifted(u*(k-1)*(1.1,0)+u*(l-1)*(dizaine+0.2,0)));
      endfor;
    endfor;
    picture Unites;
    Unites=image(
      for k=1 upto unite:
        if (k mod 2)=1:
          fill regletteunit shifted(u*(0,(k-1) div 2)+u*(0,0.1*(k-1))) withcolor coulunit;
          trace regletteunit shifted(u*(0,(k-1) div 2)+u*(0,0.1*(k-1)));
        else:
          fill regletteunit shifted(u*(0,(k-2) div 2)+u*(1.15,0.1*(k-2))) withcolor coulunit;
          trace regletteunit shifted(u*(0,(k-2) div 2)+u*(1.15,0.1*(k-2)));
        fi;
      endfor;
    );
    for l=1 upto \PfMFacteurSachet:
      trace Unites shifted(\PfMFacteurSachet*u*(dizaine+0.2,0)+u*(0.15,0)+(l-1)*(abs(lrcorner Unites-llcorner Unites)+u*0.15,0));
    endfor;
  \end{mplibcode}%
}%

\setKVdefault[TuileLibre]{EcartH=2mm,EcartV=2mm,Unite=5mm,CouleurPositif=LightGreen,CouleurNegatif=LightCoral,Affichage,CouleurSachetDizaine=red,CouleurSachetUnite=Evidence}

\makeatletter
\NewDocumentCommand\TuileLibre{som}{%
  \useKVdefault[TuileLibre]%
  \setKV[TuileLibre]{#2}%
  \IfBooleanT{#1}{\setKV[TuileLibre]{Affichage=false}}%
  \edef\PfCArobaseChaineATuiler{}%
  \edef\PfC@ExpressionTest{#3}%
  % On enleve le + initial
  \StrChar{\PfC@ExpressionTest}{1}[\PfC@CharUn]%
  \IfStrEq{\PfC@CharUn}{+}{\StrGobbleLeft{\PfC@ExpressionTest}{1}[\PfC@ExpressionTest]}{}%
  % On substitue les + par *
  \StrSubstitute{\PfC@ExpressionTest}{+}{*}[\PfC@ExpressionTest]%
  % On substitue les - par *-
  \StrSubstitute{\PfC@ExpressionTest}{-}{*-}[\PfC@ExpressionTest]%
  % On supprime * si elle est en premier (cela indiquant qu'il y a un -)
  \StrChar{\PfC@ExpressionTest}{1}[\PfC@CharUn]%
  \IfStrEq{\PfC@CharUn}{*}{\StrGobbleLeft{\PfC@ExpressionTest}{1}[\PfC@ExpressionTest]}{}%
  %Finalement, ExpressionTest : $\PfC@ExpressionTest$.\par
  % On découpe
  \StrCount{\PfC@ExpressionTest}{*}[\PfC@NbSigneEtoile]%
  \ifnum\PfC@NbSigneEtoile=0\relax%
    \edef\PfCArobaseChaineATuiler{\PfCArobaseChaineATuiler"\PfC@ExpressionTest",}%
  \else%
    \ifnum\PfC@NbSigneEtoile=1\relax%
      \StrCut{\PfC@ExpressionTest}{*}{\PfC@PartieA}{\PfC@PartieB}%
%      PartieA : $\PfC@PartieA$\par%
%      PartieB : $\PfC@PartieB$\par
      \edef\PfCArobaseChaineATuiler{\PfCArobaseChaineATuiler,"\PfC@PartieA","\PfC@PartieB"}%
    \else%
      \edef\PfC@SupportExpression{\PfC@ExpressionTest}%
      \xintFor* ##1 in {\xintSeq{1}{\PfC@NbSigneEtoile}}\do{%
        \StrCut{\PfC@SupportExpression}{*}{\PfC@ExpressionAjouteeA}{\PfC@SupportExpression}%
%        Ajout ##1 : $\PfC@ExpressionAjouteeA$\par%
        \edef\PfCArobaseChaineATuiler{\PfCArobaseChaineATuiler,"\PfC@ExpressionAjouteeA"}%
      }%
      \edef\PfCArobaseChaineATuiler{\PfCArobaseChaineATuiler,"\PfC@SupportExpression"}%
    \fi%
  \fi%
%  Expression ajoutée :$\PfCArobaseChaineATuiler$\par
  \PfC@BuildTuile{\PfCArobaseChaineATuiler}%
}%

\NewDocumentCommand\PfC@BuildTuile{m}{%
  \mplibforcehmode%
  \setbox1=\hbox{\scriptsize$3^2$}
  \begin{mplibcode}
    u:=\useKV[TuileLibre]{Unite};
    ecartv:=\useKV[TuileLibre]{EcartV};
    ecarth:=\useKV[TuileLibre]{EcartH};
    %
    boolean Affichage;
    Affichage:=\useKV[TuileLibre]{Affichage};
    %
    decalage:=0;
    color CouleurP,CouleurN,CouleurXCarreP,CouleurXCarreN,CouleurXP,CouleurXN,CouleurConstanteP,CouleurConstanteN;
    CouleurP=\useKV[TuileLibre]{CouleurPositif};
    CouleurN=\useKV[TuileLibre]{CouleurNegatif};
    path TConstante,TX,TXCarre;
    TXCarre= unitsquare scaled 3u;
    TX=unitsquare xscaled u yscaled 3u;
    TConstante=unitsquare scaled u;
    picture ITConstanteP,ITConstanteN,ITXP,ITXN,ITXCarreP,ITXCarreN;
    ITXCarreP=image(
      fill TXCarre withcolor CouleurP;
      draw TXCarre;
    );
    ITXCarreN=image(
      fill TXCarre withcolor CouleurN;
      draw TXCarre;
    );
    ITXP=image(
      fill TX withcolor 0.85[CouleurP,white];
      draw TX;
    );
    ITXN=image(
      fill TX withcolor 0.85[CouleurN,white];
      draw TX;
    );
    ITConstanteP=image(
      fill TConstante withcolor 0.7[CouleurP,white];
      draw TConstante;
    );
    ITConstanteN=image(
      fill TConstante withcolor 0.7[CouleurN,white];
      draw TConstante;
    );
    lgxcarre=abs(lrcorner ITXCarreP-llcorner ITXCarreP);
    lgx=abs(lrcorner ITXP-llcorner ITXP);
    lgconstante=abs(lrcorner ITConstanteP-llcorner ITConstanteP);
    string labelisation;
    pair Accog,Accod;
    for p_=#1:
      alpha:=scantokens p_;
      if known alpha:
        for k=1 upto abs(alpha):
          trace if alpha>0:ITConstanteP else: ITConstanteN fi shifted (decalage,(k-1)*(lgconstante+ecartv));
        endfor;
        Accog:=(decalage-ecarth/3,0);
        decalage:=decalage+lgconstante+ecarth;
        if Affichage:
          Accod:=(decalage-2*ecarth/3,0);
          label.bot(TEX("$\underbrace{\hbox to"&decimal(abs(Accod-Accog))&"pt{}}_{\rule{0pt}{\ht1}"&p_&"}$"),iso(Accog,Accod));
        fi;
      else:
        if substring(-1+length p_,length p_) of p_ = "2":
          if length p_=3:
            beta:=1;
          elseif substring(0,length p_-3) of p_="-":
            beta:=-1;
          else:
            beta:=scantokens(substring(0,length p_-3) of p_);
          fi;
          for k=1 upto abs(beta):
            trace if beta>0:ITXCarreP else: ITXCarreN fi shifted (decalage+(k-1)*(lgxcarre+ecarth),0);
          endfor;
          Accog:=(decalage-ecarth/3,0);
          decalage:=decalage+abs(beta)*(lgxcarre+ecarth);
          if Affichage:
            Accod:=(decalage-2*ecarth/3,0);
            label.bot(TEX("$\underbrace{\hbox to"&decimal(abs(Accod-Accog))&"pt{}}_{"&p_&"}$"),iso(Accog,Accod));
          fi;
        else:
          if length p_=1:
            gamma:=1;
          elseif substring(0,length p_-1) of p_="-":
            gamma:=-1;
          else:
            gamma:=scantokens(substring(0,length p_-1) of p_);
          fi;
          for k=1 upto abs(gamma):
            trace if gamma>0:ITXP else: ITXN fi shifted (decalage+(k-1)*(lgx+ecarth),0);
          endfor;
          Accog:=(decalage-ecarth/3,0);
          decalage:=decalage+abs(gamma)*(lgx+ecarth);
          if Affichage:
            Accod:=(decalage-2*ecarth/3,0);
            label.bot(TEX("$\underbrace{\hbox to"&decimal(abs(Accod-Accog))&"pt{}}_{\rule{0pt}{\ht1}"&p_&"}$"),iso(Accog,Accod));
          fi;
        fi;
      fi;
    endfor;
  \end{mplibcode}%
}%
\makeatother

% Changelog
% 14/06/2026 : possibilité de modifier les couleurs des briques dans les sachets + Différentiation légère des couleurs selon x^2, x ou nombre dans la commande \TuileLibre