%%%
% VerreDoseur
%%%
\def\filedateVerreDoseur{2026/04/16}%
\def\fileversionVerreDoseur{0.1a}%
\message{-- \filedateVerreDoseur\space v\fileversionVerreDoseur}%
%
\setKVdefault[VerreM]{Capa=1000,Reponse=false,Pas=50,Rayon=2,Hauteur=8,Fractions=false,PasGrad=100,PasFraction=1,Traces={}}

\makeatletter
\NewDocumentCommand\VerreMesureur{om}{%
  \useKVdefault[VerreM]%
  \setKV[VerreM]{#1}%
  \edef\PfC@DenoVerre{\useKV[VerreM]{Capa}}%
  \quotient{1000}{\PfC@DenoVerre}%
  \modulo{1000}{\PfC@DenoVerre}%
  \edef\PfCVMRetiensDeno{\the\intquotient}%
  \ifnum\remainder=0\relax%
    \PfC@BuildVerreMesureur{#2}
  \else%
    Le partage n'est pas adapté avec la capacité choisie.
  \fi%
}%

\NewDocumentCommand\PfC@BuildVerreMesureur{m}{%
  \mplibforcehmode%
  \begin{mplibcode}

    def Repere(expr ab)=gg shifted((ab/CapaTotale)[A,B]-A) enddef;    
    
    boolean Fraction,Reponse;
    Fraction=\useKV[VerreM]{Fractions};
    Reponse=\useKV[VerreM]{Reponse};
    Rayon=\useKV[VerreM]{Rayon};
    Hauteur=\useKV[VerreM]{Hauteur};
    Pasx=\useKV[VerreM]{Pas};
    PasGrad=\useKV[VerreM]{PasGrad};
    PasFrac=\useKV[VerreM]{PasFraction};
    CapaTotale=\useKV[VerreM]{Capa};
    Cible=#1;
    pair A,B,M[],N[];
    %%%%%%%%%%%%%
    path Cc,Cd,Ce,Cf,Cg,Ch,pg,mg,gg;
    Cc=cercles((0,0),Rayon*u) yscaled 0.1;
    Cd=Cc shifted (0,u*Hauteur);
    A=0[point(3*length Cc/4) of Cc,point(3*length Cd/4) of Cd];
    B=0.9[point(3*length Cc/4) of Cc,point(3*length Cd/4) of Cd];
    pg=(subpath(0.745*length Cc,0.755*length Cc) of Cc) shifted (A-point(3*length Cc/4) of Cc);
    mg=(subpath(0.73*length Cc,0.77*length Cc) of Cc) shifted (A-point(3*length Cc/4) of Cc);
    gg=(subpath(0.71*length Cc,0.79*length Cc) of Cc) shifted (A-point(3*length Cc/4) of Cc);
    Ce=(subpath(length Cc/2,length Cc) of Cc)--(point(0) of Cd)--reverse(subpath(length Cd/2,length Cd) of Cd)--cycle;
    Cf=(subpath(length Cc/2,length Cc) of Cc)--(point(0) of Cd)--(subpath(0,length Cd/2) of Cd)--cycle;
    Cg=Cc shifted(((#1/CapaTotale)[A,B])-point(3*length Cc/4) of Cc);
    Ch=(subpath(length Cc/2,length Cc) of Cc)--(point(0) of Cg)--reverse(subpath(length Cg/2,length Cg) of Cg)--cycle;
    trace Cc shifted((0,u*Hauteur));
    trace subpath(length Cc/2,length Cc) of Cc;
    trace segment(point(0) of Cc,point(0) of Cc shifted((0,u*Hauteur)));
    trace segment(point(length Cc/2) of Cc,point(length Cc/2) of (Cc shifted((0,u*Hauteur))));
    fill Cf withcolor 0.95white withtransparency(1,0.5);
    fill Ce withcolor 0.9white withtransparency(1,0.5);
    if Reponse:
      fill Ch withcolor PfCdblue withtransparency(1,0.5);
    fi;
    %%%%%%%
    trace chemin(A,B);
    for k=1 upto Pasx:
      trace pg shifted((k/Pasx)[A,B]-A);
    endfor;
    for k=5 step 10 until Pasx:
      trace mg shifted((k/Pasx)[A,B]-A);
    endfor;
    for k=10 step 10 until Pasx:
      trace gg shifted((k/Pasx)[A,B]-A);
    endfor;
    for k=1 upto (CapaTotale/PasGrad)-1:
      label.urt(TEX("\footnotesize"&decimal(k*PasGrad)),((k*PasGrad)/CapaTotale)[A,B]+u*(0.2,0));
    endfor;
    label.top(TEX("\Capa[mL]{\useKV[VerreM]{Capa}}"),B+u*(0.8,0));
    labeloffset:=labeloffset*2;
    label.top(TEX("Litre"),B+u*(-0.8,0));
    labeloffset:=labeloffset/2;
    if \PfCVMRetiensDeno>1:
      label.lft(TEX("$1/\PfCVMRetiensDeno$"),B+u*(-0.8,0));
    else:
      label.lft(TEX("1"),B+u*(-0.8,0));
    fi;
    if Fraction:
      for k=1 step PasFrac until (CapaTotale/PasGrad)-1:
      label.lft(TEX("\footnotesize\Simplification{"&decimal(k*PasGrad)&"}{1000}"),((k*PasGrad)/CapaTotale)[A,B]-u*(0.8,0));
      trace (((k*PasGrad)/CapaTotale)[A,B]-u*(0.8,0))--(((k*PasGrad)/CapaTotale)[A,B]) dashed evenly;
    endfor;
    fi;
    \ifemptyKV[VerreM]{Traces}{}{%
      \useKV[VerreM]{Traces};
    }%
  \end{mplibcode}
}%
\makeatother