%%%
% Guide-âne
%%%
\def\filedateGuideAne{2026/01/17}%
\def\fileversionGuideAne{0.1}%
\message{-- \filedateGuideAne\space v\fileversionGuideAne}%
%
\setKVdefault[ClesAne]{Angle=50,Hauteur=5cm,NomSegment={$u$},Couleur={},Graine={},LongueurSegment=5cm,Traces={},Marque={},Vide=false,ColBack=white,Repere={},ColRepere=PfCdred}

\makeatletter
\NewDocumentCommand\GuideAne{om}{%
  \useKVdefault[ClesAne]%
  \setKV[ClesAne]{#1}%
  \PfC@MPGuideAne{#2}%
}%

\NewDocumentCommand\PfC@MPGuideAne{m}{%
  \mplibforcehmode%
  \begin{Geometrie}[CoinBG={u*(-10,-10)},CoinHD={u*(10,10)}]
    \ifemptyKV[ClesAne]{Graine}{}{randomseed:=\useKV[ClesAne]{Graine};}
    LongueurSegment=\useKV[ClesAne]{LongueurSegment};
    Angle=\useKV[ClesAne]{Angle};
    boolean Marques,Vide,Reperes;
    \ifemptyKV[ClesAne]{Repere}{Reperes=false;Repere:=#1;}{Reperes=true;Repere:=\useKV[ClesAne]{Repere};}
    \ifemptyKV[ClesAne]{Marque}{Marques=false;}{Marques=true;Marque=\useKV[ClesAne]{Marque};}%
    Vide=\useKV[ClesAne]{Vide};
    color couleurtrace,colbackground;
    colbackground=\useKV[ClesAne]{ColBack};
    \ifemptyKV[ClesAne]{Couleur}{couleurtrace=black;}{couleurtrace=\useKV[ClesAne]{Couleur};}%
    pair O,C,D,E,F,K,M,N;
    path cc,base,cd;
    picture Retiens;
    % On trace le cercle d'abord :)
    O=(0,0);
    cc=cercles(O,LongueurSegment);
    % segment défini 0--C
    alpha=-30+uniformdeviate(60);
    C=pointarc(cc,alpha);
    %pour le guide-âne
    D=rotation(C,O,-alpha+Angle);
    E=(1/#1)[O,C];
    F=(Repere/#1)[O,C];
    base=droite(C,D);
    drawoptions(withcolor couleurtrace);
    for k=-100 upto 100:
      trace base shifted((k/#1)[O,C]);
    endfor;
    drawoptions();
    % pour cliper à la fin
    bboxmargin:=1.5cm;
    if Repere>#1:
      clip currentpicture to bbox chemin(O,F);
    else:
      clip currentpicture to bbox chemin(O,C);
    fi;
    bboxmargin:=0;
    Retiens:=currentpicture;
    if Vide:
      currentpicture:=nullpicture;
      trace bbox Retiens withcolor colbackground;
    fi;
    % le segment final
    drawoptions(withpen pencircle scaled 1.25);
    trace chemin(O,C);
    trace marquesegment(O,C);
    Mylabel(TEX("\useKV[ClesAne]{NomSegment}"),C--O,0.5);
    drawoptions();
    if Marques:
      marque_my:=15;
      marque_s:=0.5*marque_s;
      denosimp=#1 div Marque;
      for k=0 step Marque until (Repere-Marque):
        M:=(k/#1)[O,C];
        N:=((k+Marque)/#1)[O,C];
        trace marquesegment(M,N);
        Mylabel(TEX("$\displaystyle\frac{1}{"&decimal(denosimp)&"}$"),M--N,0.5);
      endfor;
      if Reperes:
        if Vide=false:
          trace O--F withpen pensquare scaled 1.25 withcolor PfCdred;
          trace marquesegment(O,F);
        fi;
      fi;  
    fi;
    \ifemptyKV[ClesAne]{Traces}{}{\useKV[ClesAne]{Traces};}%
  \end{Geometrie}
}%
\makeatother