%%%
% Ceinture
%%%
\def\filedateCeinture{2025/10/07}%
\def\fileversionCeinture{0.1}%
\message{-- \filedateCeinture\space v\fileversionCeinture}%
%
% https://www.youtube.com/watch?v=7F2cVfYAWD4
\setKVdefault[Ceinture]{Complete=false,Couleurs={},Echelle=1,EchelleBis={},Angle={},Niveau={}}

\makeatletter
\NewDocumentCommand\PfCCeinture{so}{%
  \useKVdefault[Ceinture]%
  \setKV[Ceinture]{#2}%
  \IfBooleanTF{#1}{\setKV[Ceinture]{Complete}}{}%
  \PfM@BuildCeinture
}%

\NewDocumentCommand\PfM@BuildCeinture{}{%
  \mplibforcehmode%
  \begin{mplibcode}
    u:=\useKV[Ceinture]{Echelle}*1cm;
    %
    input PfCSketch;
    %
    extra_endfig:="";
    %
    Dan=0;
    %
    color CoulCein,CoulBande;
    CoulCein=white;CoulBande=white;
    boolean Bicolore,Complete;
    Bicolore:=false;
    Complete=\useKV[Ceinture]{Complete};
    \ifemptyKV[Ceinture]{Niveau}{%
      \ifemptyKV[Ceinture]{Couleurs}{%
      }{%
        nb:=0;
        for p_=\useKV[Ceinture]{Couleurs}:
          nb:=nb+1;
          if nb=1:
            CoulCein:=p_;
          else:
            CoulBande:=p_;
            Bicolore:=true;
          fi;
        endfor;
      }%
    }{Dan:=\useKV[Ceinture]{Niveau};%   
    }%
    pair Ce[],Cf[];
    Ce1=u*(-2,0.20);
    Ce2-Ce1=u*(-0.075,-0.4);
    Ce4-Ce1=u*(4,0);
    Ce3-Ce4=u*(0.075,-0.4);
    path Ceint[],Bande[];
    Ceint1=Ce1{dir-95}..{dir-90}Ce2{dir-10}..{dir10}Ce3{dir90}..{dir105}Ce4{dir-170}..{dir170}cycle;
    Cf1=0.3[Ce1,Ce2];
    Cf2=0.7[Ce1,Ce2];
    Cf3=point(2.3) of Ceint1;%0.275[Ce3,Ce4];
    Cf4=point(2.7) of Ceint1;%0.725[Ce3,Ce4];
    Bande1=Cf1{dir-95}..{dir-90}Cf2{dir-10}..{dir10}Cf3{dir90}..{dir105}Cf4{dir-170}..{dir170}cycle;
    Ceint2=Ce1{dir20}..{dir-20}Ce4;
    Ce5=point(3.175) of Ceint1;
    Ce6=point(3.825) of Ceint1;
    Ce70=point(3.475) of Ceint1;
    Ce71=point(3.525) of Ceint1;
    Ce8-Ce71=u*(-1.5,-1.25);
    Ce9-Ce70=u*(1.5,-1.25);
    Ce100=point(1.525) of Ceint1;
    Ce101=point(1.475) of Ceint1;
    Ce102=(point(1.5) of Ceint1)+2*unitvector((0,-1));
    Ce103=(point(1.5) of Ceint1)+2*unitvector((0,-1))+3unitvector((-1,0));
    Ce11=Ce8+u*(0.25,-0.25);
    Ce12=Ce9+u*(-0.25,-0.25);
    Ce13=(subpath(3,4) of Ceint1) intersectionpoint (Ce12--symetrie(Ce12,Ce100));
    Ce104=Ce100+3.25*unitvector(Ce13-Ce100);
    Ce105=(Ce102--10[Ce102,Ce103]) intersectionpoint (Ce11{dir (angle(1.15[Ce11,Ce101]-Ce11)+5)}..(1.15[Ce11,Ce101]));
    Ce106=(Ce11{dir (angle(1.15[Ce11,Ce101]-Ce11)+5)}..(1.15[Ce11,Ce101])) intersectionpoint (Ce100--Ce13);
    Ce14=(Ce9--symetrie(Ce9,Ce70)) intersectionpoint (Ce8--symetrie(Ce8,Ce71));
    Ce15=Ce14+1.5*unitvector(Ce70-Ce9);
    Ce142=Ce14;
    Ce141=Ce142+0.5*unitvector(Ce71-Ce8);
    Ce16=1.4[Ce14,Ce70];
    Ce17=point(3.395) of Ceint1;
    Ce18=parallele(Ce16,Ce100,Ce17) intersectionpoint (subpath(1,2) of Ceint1);
    path boucleg,pang,boucled,pand,petiteboucle,grandeceintureh,grandeceintureb;
    boucleg=(0.10[Ce100,Ce13]--Ce13{dir20}..{dir0}Ce15--Ce16{dir-100}..0.10[Ce100,Ce13]--cycle);
    Cf15=point(1.25) of boucleg;
    Cf16=point(1.75) of boucleg;
    Cf17=0.25[point(3) of boucleg,point(4) of boucleg];
    Cf18=0.75[point(3) of boucleg,point(4) of boucleg];
    Bande4=Cf15--Cf16--Cf17--Cf18--cycle;
    pang=(reverse((Ce11{dir (angle(1.15[Ce11,Ce101]-Ce11)+5)}..(1.15[Ce11,Ce101])) cutafter (Ce12--symetrie(Ce12,Ce100)))--(Ce11--Ce8{dir (angle(Ce71-Ce8)+5)}..Ce71) cutafter (Ce12--symetrie(Ce12,Ce100)))--cycle;
    boucled=(Ce142--Ce141{dir0}..Ce17--Ce18..Ce102--((Ce102--Ce13) cutafter (Ce104--Ce103))--Ce104--0.10[Ce100,Ce13]--reverse(Ce16{dir-100}..0.10[Ce100,Ce13])--cycle);
    Cf20=point(1.4) of boucled;
    Cf21=point(1.75) of boucled;
    Cf22=point(3.3) of boucled;
    Cf23=point(3.65) of boucled;
    Bande5=Cf20--Cf21--Cf22--Cf23--cycle;
    pand=(Ce18--reverse((Ce12{dir (angle(Ce100-Ce12)-5)}..Ce100) cutafter (Ce18--Ce102))--(Ce12--Ce9{dir (angle(Ce70-Ce9)-5)}..Ce70) cutafter (Ce17--Ce18))--cycle;
    petiteboucle=Ce104--Ce103--Ce105--Ce106--cycle;
    grandeceintureb=Ceint1;
    grandeceintureh=Ceint2--((subpath(3,4) of Ceint1) cutafter (Ce5--Ce5))--reverse(Ce6{dir15}..{dir-15}Ce5)--((subpath(3,4) of Ceint1) cutbefore (Ce6--Ce6))--cycle;
    Cf5=0.3[point(2) of pang,point(3) of pang];
    Cf6=0.7[point(2) of pang,point(3) of pang];
    Cf7=0.3[point(4) of pang,point(5) of pang];
    Cf8=0.7[point(4) of pang,point(5) of pang];
    Bande2=Cf5--Cf6{dir (angle(Ce71-Ce8)+5)}..Cf7--Cf8--reverse(Cf5{dir (angle(1.15[Ce11,Ce101]-Ce11)+5)}..Cf8)--cycle;
    Cf10=0.25[point(3) of pand,point(4) of pand];
    Cf11=0.75[point(3) of pand,point(4) of pand];
    Cf12=0.25[point(5) of pand,point(7) of pand];
    Cf13=0.75[point(5) of pand,point(7) of pand];
    Bande3=Cf10--Cf11{dir (angle(Ce70-Ce9)-5)}..Cf12--reverse(Cf10{dir (angle(Ce100-Ce12)-5)}..Cf13)--cycle;
    % Tracé
    if Complete:
      fill grandeceintureh withcolor CoulCein;
      trace grandeceintureh;
    fi;
    fill grandeceintureb withcolor CoulCein;
    if Bicolore:
      fill Bande1 withcolor CoulBande;
      trace Bande1;
    fi;
    fill pang withcolor CoulCein;
    if Bicolore:
      fill Bande2 withcolor CoulBande;
      trace Bande2;
    fi;
    fill pand withcolor CoulCein;
    if Bicolore:
      fill Bande3 withcolor CoulBande;
      trace Bande3;
    fi;
    fill boucleg withcolor CoulCein;
    if Bicolore:
      fill Bande4 withcolor CoulBande;
      trace Bande4;
    fi;
    fill boucled withcolor CoulCein;
    if Bicolore:
      fill Bande5 withcolor CoulBande;
      trace Bande5;
    fi;
    fill petiteboucle withcolor CoulCein;
    picture fond;
    fond=image(
      trace pang;
      trace boucled;
      trace boucleg;
      trace pand;
      trace petiteboucle;
      trace (subpath(0,2) of Ceint1) cutafter (Ce8{dir (angle(Ce71-Ce8)+5)}..Ce71);
      trace (subpath(1,3) of Ceint1) cutbefore (Ce9{dir (angle(Ce70-Ce9)-5)}..Ce70);
      if Complete:
        trace (subpath(3.015,4) of Ceint1) cutafter (Ce17--Ce18);
        trace (subpath(3.015,3.985) of Ceint1) cutbefore (Ce13--Ce13);
      else:
        trace (subpath(3,4) of Ceint1) cutafter (Ce17--Ce18);
        trace (subpath(3,4) of Ceint1) cutbefore (Ce13--Ce13);
      fi;
      trace (0.10[Ce100,Ce13])--(0.10[Ce100,Ce13]+1.5*unitvector(Ce100-Ce16));
    );
    if Dan>0:
      for k=0 upto Dan-1:
        trace (point(0.75-k/15) of pang)--(point(3.25+k/15) of pang) withpen pencircle scaled 2;
      endfor;
    fi;
    trace fond;
    trace (fond randomized 1) withpen pencircle scaled 0.25 withcolor 0.25white;
    \ifemptyKV[Ceinture]{Angle}{}{%
      picture Retiens;
      Retiens:=currentpicture;
      currentpicture:=nullpicture;
      draw Retiens rotatedabout(center Retiens,\useKV[Ceinture]{Angle});
    }%
    \ifemptyKV[Ceinture]{EchelleBis}{}{%
      picture Retiens;
      Retiens:=currentpicture;
      currentpicture:=nullpicture;
      draw Retiens scaled \useKV[Ceinture]{EchelleBis};
    }%
  \end{mplibcode}%
}%
\makeatother