%%%
% Fractions
%%%
\setKVdefault[ClesFraction]{Rayon=2cm,Disque,Regulier=false,Segment=false,Rectangle=false,Longueur=5cm,Largeur=2cm,Cotes=5,Triangle=false,Parts=3,Couleur=green,Reponse=false,Multiple=1,Hachures=false,Epaisseur=1}

\def\MPFractionTriangle#1#2#3#4#5{%
  % #1 longueur du c\^ot\'e
  % #2 partage sur le c\^ot\'e
  % #3 num
  % #4 d\'eno (attention : = #2^2)
  % #5 couleur
  \ifluatex
   \mplibforcehmode
   \begin{mplibcode}
     nbtriangle=0;

     vardef Ligne(expr longueur)=
     for k=0 upto 2*(longueur-1):
     nbtriangle:=nbtriangle+1;
     if (k mod 2)=0:
     M[nbtriangle]=(Tria shifted(0.5*k*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     else:
     M[nbtriangle]=(Trir shifted(0.5*(k-1)*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     fi;
     endfor;
     enddef;

     pair A,B,C;
     A=u*(0.5,0.5);
     B-A=(#1,0);
     C=rotation(B,A,60);

     nbparts:=#2;

     path M[];

     path Tria,Trir;
     Tria=polygone(A,(1/nbparts)[A,B],(1/nbparts)[A,C]);
     Trir=symetrie(Tria,(1/nbparts)[A,B],(1/nbparts)[A,C]);

     for k=nbparts downto 1:
     Ligne(k);
     endfor;
     
     m:=#3 div #4;

     drawoptions(shifted(m*(#1+1cm,0)));
     for l=1 upto (#3 mod #4):
     fill M[l] withcolor #5;
     for k=1 upto nbparts:
     trace segment((k/nbparts)[A,B],(k/nbparts)[A,C]);
     trace segment((k/nbparts)[B,A],(k/nbparts)[B,C]);
     trace segment((k/nbparts)[C,A],(k/nbparts)[C,B]);
     endfor;
     endfor;
     
     for l=0 upto (m-1):
     drawoptions(shifted(l*(#1+1cm,0)));
     remplis polygone(A,B,C) withcolor #5;
     for k=1 upto nbparts:
     trace segment((k/nbparts)[A,B],(k/nbparts)[A,C]);
     trace segment((k/nbparts)[B,A],(k/nbparts)[B,C]);
     trace segment((k/nbparts)[C,A],(k/nbparts)[C,B]);
     endfor;
     endfor;
   \end{mplibcode}
     \else
     \begin{mpost}
            nbtriangle=0;

     vardef Ligne(expr longueur)=
     for k=0 upto 2*(longueur-1):
     nbtriangle:=nbtriangle+1;
     if (k mod 2)=0:
     M[nbtriangle]=(Tria shifted(0.5*k*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     else:
     M[nbtriangle]=(Trir shifted(0.5*(k-1)*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     fi;
     endfor;
     enddef;

     pair A,B,C;
     A=u*(0.5,0.5);
     B-A=(#1,0);
     C=rotation(B,A,60);

     nbparts:=#2;

     path M[];

     path Tria,Trir;
     Tria=polygone(A,(1/nbparts)[A,B],(1/nbparts)[A,C]);
     Trir=symetrie(Tria,(1/nbparts)[A,B],(1/nbparts)[A,C]);

     for k=nbparts downto 1:
     Ligne(k);
     endfor;
     
     m:=#3 div #4;

     drawoptions(shifted(m*(#1+1cm,0)));
     for l=1 upto (#3 mod #4):
     fill M[l] withcolor #5;
     for k=1 upto nbparts:
     trace segment((k/nbparts)[A,B],(k/nbparts)[A,C]);
     trace segment((k/nbparts)[B,A],(k/nbparts)[B,C]);
     trace segment((k/nbparts)[C,A],(k/nbparts)[C,B]);
     endfor;
     endfor;
     
     for l=0 upto (m-1):
     drawoptions(shifted(l*(#1+1cm,0)));
     remplis polygone(A,B,C) withcolor #5;
     for k=1 upto nbparts:
     trace segment((k/nbparts)[A,B],(k/nbparts)[A,C]);
     trace segment((k/nbparts)[B,A],(k/nbparts)[B,C]);
     trace segment((k/nbparts)[C,A],(k/nbparts)[C,B]);
     endfor;
     endfor;
     \end{mpost}
   \fi
 }

 \def\MPFractionTriangleH#1#2#3#4#5#6{
  % #1 longueur du c\^ot\'e
  % #2 partage sur le c\^ot\'e
  % #3 num
  % #4 d\'eno (attention : = #2^2)
  % #5 couleur
  % #6 \'epaisseur
  \ifluatex
   \mplibforcehmode
   \begin{mplibcode}
          nbtriangle=0;

     vardef Ligne(expr longueur)=
     for k=0 upto 2*(longueur-1):
     nbtriangle:=nbtriangle+1;
     if (k mod 2)=0:
     M[nbtriangle]=(Tria shifted(0.5*k*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     else:
     M[nbtriangle]=(Trir shifted(0.5*(k-1)*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     fi;
     endfor;
     enddef;

     pair A,B,C;
     A=u*(0.5,0.5);
     B-A=(#1,0);
     C=rotation(B,A,60);

     nbparts:=#2;

     path M[];

     path Tria,Trir;
     Tria=polygone(A,(1/nbparts)[A,B],(1/nbparts)[A,C]);
     Trir=symetrie(Tria,(1/nbparts)[A,B],(1/nbparts)[A,C]);

     for k=nbparts downto 1:
     Ligne(k);
     endfor;
     
     m:=#3 div #4;

     for l=1 upto (#3 mod #4):
     drawoptions(withpen pencircle scaled #6);
     trace hachurage(M[l] shifted(m*(#1+1cm,0)),90,0.2,0) withcolor #5;%M[l] withcolor #5;
     for k=1 upto nbparts:
     drawoptions(withpen pencircle scaled #6);
     trace segment((k/nbparts)[A,B],(k/nbparts)[A,C]) shifted(m*(#1+1cm,0));
     trace segment((k/nbparts)[B,A],(k/nbparts)[B,C]) shifted(m*(#1+1cm,0));
     trace segment((k/nbparts)[C,A],(k/nbparts)[C,B]) shifted(m*(#1+1cm,0));
     endfor;
     endfor;
     
     for l=0 upto (m-1):
     drawoptions(shifted(l*(#1+1cm,0)) withpen pencircle scaled #6);
     trace hachurage(polygone(A,B,C),90,0.2,0) withcolor #5;%polygone(A,B,C) withcolor #5;
          drawoptions(shifted(l*(#1+1cm,0)) withpen pencircle scaled #6);
     for k=1 upto nbparts:
     trace segment((k/nbparts)[A,B],(k/nbparts)[A,C]);
     trace segment((k/nbparts)[B,A],(k/nbparts)[B,C]);
     trace segment((k/nbparts)[C,A],(k/nbparts)[C,B]);
     endfor;
     endfor;
   \end{mplibcode}
     \else
     \begin{mpost}
       nbtriangle=0;

     vardef Ligne(expr longueur)=
     for k=0 upto 2*(longueur-1):
     nbtriangle:=nbtriangle+1;
     if (k mod 2)=0:
     M[nbtriangle]=(Tria shifted(0.5*k*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     else:
     M[nbtriangle]=(Trir shifted(0.5*(k-1)*(1/nbparts)*(B-A)))  shifted((nbparts-longueur)*(1/nbparts)*(C-A));
     fi;
     endfor;
     enddef;

     pair A,B,C;
     A=u*(0.5,0.5);
     B-A=(#1,0);
     C=rotation(B,A,60);

     nbparts:=#2;

     path M[];

     path Tria,Trir;
     Tria=polygone(A,(1/nbparts)[A,B],(1/nbparts)[A,C]);
     Trir=symetrie(Tria,(1/nbparts)[A,B],(1/nbparts)[A,C]);

     for k=nbparts downto 1:
     Ligne(k);
     endfor;

     diversite=floor(uniformdeviate(#2**2-#3-1));
     
     for k=(1+diversite) upto (#3+diversite):
     drawoptions(withpen pencircle scaled #6);
     trace hachurage(M[k],90,0.2,0) withcolor #5;
     endfor;
     drawoptions(withpen pencircle scaled #6);
     
     for k=1 upto nbparts:
     trace segment((k/nbparts)[A,B],(k/nbparts)[A,C]);
     trace segment((k/nbparts)[B,A],(k/nbparts)[B,C]);
     trace segment((k/nbparts)[C,A],(k/nbparts)[C,B]);
     endfor;
     \end{mpost}
   \fi
 }

\def\MPFractionRegulier#1#2#3#4#5{%
  % #1 rayon, #2 nb c\^ot\'es, #3 num, #4 deno, #5 couleur
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair O,A[],B[];
    O=u*(0,0);
    path cc,cd;
    cc=cercles(O,#1);
    for k=0 upto #2:
    A[k]=pointarc(cc,k*(360/#2));
    endfor;
    cd=polygone(A0 for k=1 upto #2-1:,A[k] endfor);
    for k=0 upto #4-1:
    B[k]=point(k*(#2/#4)) of cd;
    endfor;
    picture fondcolore;
    fondcolore=image(
    remplis O--arccercle(B[0],B[#3 mod #4],O)--cycle withcolor #5;
    clip currentpicture to cd;
    for k=0 upto #4-1:
    draw segment(O,B[k]) cutafter cd;
    endfor;
    trace cd;
    );
    currentpicture:=nullpicture;
    m=#3 div #4;
    if (#3 mod #4)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    fill cd shifted(l*(#1*2+0.5cm,0)) withcolor #5;
    trace cd shifted(l*(#1*2+0.5cm,0));
    if #4>1:
    for k=0 upto #4-1:
    draw (segment(O,B[k]) cutafter cd) shifted(l*(#1*2+0.5cm,0));
    endfor;
    fi;
    endfor;
    fi;
    draw fondcolore shifted(m*(#1*2+0.5cm,0));
    draw cd shifted(m*(#1*2+0.5cm,0));
    if #4>1:
    for k=0 upto #4-1:
    draw (segment(O,B[k]) cutafter cd) shifted(m*(#1*2+0.5cm,0));
    endfor;
    fi;
  \end{mplibcode}
  \else
  \begin{mpost}
        pair O,A[],B[];
    O=u*(0,0);
    path cc,cd;
    cc=cercles(O,#1);
    for k=0 upto #2:
    A[k]=pointarc(cc,k*(360/#2));
    endfor;
    cd=polygone(A0 for k=1 upto #2-1:,A[k] endfor);
    for k=0 upto #4-1:
    B[k]=point(k*(#2/#4)) of cd;
    endfor;
    picture fondcolore;
    fondcolore=image(
    remplis O--arccercle(B[0],B[#3 mod #4],O)--cycle withcolor #5;
    clip currentpicture to cd;
    for k=0 upto #4-1:
    draw segment(O,B[k]) cutafter cd;
    endfor;
    trace cd;
    );
    currentpicture:=nullpicture;
    m=#3 div #4;
    if (#3 mod #4)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    fill cd shifted(l*(#1*2+0.5cm,0)) withcolor #5;
    trace cd shifted(l*(#1*2+0.5cm,0));
    if #4>1:
    for k=0 upto #4-1:
    draw (segment(O,B[k]) cutafter cd) shifted(l*(#1*2+0.5cm,0));
    endfor;
    fi;
    endfor;
    fi;
    draw fondcolore shifted(m*(#1*2+0.5cm,0));
    draw cd shifted(m*(#1*2+0.5cm,0));
    if #4>1:
    for k=0 upto #4-1:
    draw (segment(O,B[k]) cutafter cd) shifted(m*(#1*2+0.5cm,0));
    endfor;
    fi;
  \end{mpost}
  \fi
}

\def\MPFractionRegulierH#1#2#3#4#5#6{%
  % #1 rayon, #2 nb c\^ot\'es, #3 num, #4 deno, #5 couleur, #6 épaisseur
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair O,A[],B[];
    O=u*(0,0);
    path cc,cd;
    cc=cercles(O,#1);
    for k=0 upto #2:
    A[k]=pointarc(cc,k*(360/#2));
    endfor;
    cd=polygone(A0 for k=1 upto #2-1:,A[k] endfor);
    for k=0 upto #4-1:
    B[k]=point(k*(#2/#4)) of cd;
    endfor;
    picture fondcolore;
    fondcolore=image(%
    drawoptions(withpen pencircle scaled #6);
    trace hachurage(O--arccercle(B[0],B[#3 mod #4],O)--cycle,1.5*360/#2,0.25,0) withcolor #5;
    clip currentpicture to cd;
    drawoptions(withpen pencircle scaled #6);
    for k=0 upto #4-1:
    draw segment(O,B[k]) cutafter cd;
    endfor;
    trace cd;
    );
    currentpicture:=nullpicture;
    m=#3 div #4;
    if (#3 mod #4)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    drawoptions(withpen pencircle scaled #6);
    trace hachurage(cd shifted(l*(#1*2+0.5cm,0)),1.5*360/#2,0.25,0) withcolor #5;
    drawoptions(withpen pencircle scaled #6);
    trace cd shifted(l*(#1*2+0.5cm,0));
    if #4>1:
    for k=0 upto #4-1:
    drawoptions(withpen pencircle scaled #6);
    draw (segment(O,B[k]) cutafter cd) shifted(l*(#1*2+0.5cm,0));
    endfor;
    fi;
    endfor;
    fi;
    draw fondcolore shifted(m*(#1*2+0.5cm,0));
    drawoptions(withpen pencircle scaled #6);
    draw cd shifted(m*(#1*2+0.5cm,0));
    if #4>1:
    drawoptions(withpen pencircle scaled #6);
    for k=0 upto #4-1:
    draw (segment(O,B[k]) cutafter cd) shifted(m*(#1*2+0.5cm,0));
    endfor;
    fi;
  \end{mplibcode}
  \else
\begin{mpost}
    pair O,A[],B[];
    O=u*(0,0);
    path cc,cd;
    cc=cercles(O,#1);
    for k=0 upto #2:
    A[k]=pointarc(cc,k*(360/#2));
    endfor;
    cd=polygone(A0 for k=1 upto #2-1:,A[k] endfor);
    for k=0 upto #4-1:
    B[k]=point(k*(#2/#4)) of cd;
    endfor;
    picture fondcolore;
    fondcolore=image(%
    drawoptions(withpen pencircle scaled #6);
    trace hachurage(O--arccercle(B[0],B[#3 mod #4],O)--cycle,1.5*360/#2,0.25,0) withcolor #5;
    clip currentpicture to cd;
    drawoptions(withpen pencircle scaled #6);
    for k=0 upto #4-1:
    draw segment(O,B[k]) cutafter cd;
    endfor;
    trace cd;
    );
    currentpicture:=nullpicture;
    m=#3 div #4;
    if (#3 mod #4)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    drawoptions(withpen pencircle scaled #6);
    trace hachurage(cd shifted(l*(#1*2+0.5cm,0)),1.5*360/#2,0.25,0) withcolor #5;
    drawoptions(withpen pencircle scaled #6);
    trace cd shifted(l*(#1*2+0.5cm,0));
    if #4>1:
    for k=0 upto #4-1:
    drawoptions(withpen pencircle scaled #6);
    draw (segment(O,B[k]) cutafter cd) shifted(l*(#1*2+0.5cm,0));
    endfor;
    fi;
    endfor;
    fi;
    draw fondcolore shifted(m*(#1*2+0.5cm,0));
    drawoptions(withpen pencircle scaled #6);
    draw cd shifted(m*(#1*2+0.5cm,0));
    if #4>1:
    drawoptions(withpen pencircle scaled #6);
    for k=0 upto #4-1:
    draw (segment(O,B[k]) cutafter cd) shifted(m*(#1*2+0.5cm,0));
    endfor;
    fi;
  \end{mpost}
  \fi
}

\def\MPFractionRectangle#1#2#3#4#5#6{%
  % #1 longueur, #2 largeur, #3 num, #4 deno, #5 couleur, #6 multiple
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair A,B,C,D,M[],N[],R[],S[];
    A=(1,1);
    B-A=(#1,0);
    C-B=(0,#2);
    D-C=A-B;
    numeric parts;
    parts=(#4 div #6);
    for k=0 upto parts:
    M[k]=(k/parts)[A,B];
    N[k]=(k/parts)[D,C];
    endfor;
    if #6>1:
    for k=0 upto #6:
    R[k]=(k/#6)[A,D];
    S[k]=(k/#6)[B,C];
    endfor;
    fi;
    picture FondRectangle;
    FondRectangle=image(%
    draw polygone(A,B,C,D);
    for k=1 upto (parts-1):
    draw segment(M[k],N[k]);
    endfor;
    if #6>1:
    for k=1 upto (#6-1):
    draw segment(R[k],S[k]);
    endfor;
    fi;
    );
    picture FondRectangleColorie;
    FondRectangleColorie=image(%
    if #6=1:
    remplis polygone(A,M[#3 mod #4],N[#3 mod #4],D) withcolor #5;
    else:
    DDiv=(#3 mod #4) div parts;
    MMod=(#3 mod #4) mod parts;
    remplis polygone(A,B,S[DDiv],R[DDiv]) withcolor #5;
    remplis polygone(R[DDiv],(xpart(M[MMod]),ypart(R[DDiv])),(xpart(M[MMod]),ypart(R[DDiv+1])),R[DDiv+1]) withcolor #5;
    fi;
    );    
    m=#3 div #4;
    if (#3 mod #4)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto m-1:
    remplis (polygone(A,B,C,D) shifted(l*(#1+1cm,0))) withcolor #5;
    trace FondRectangle shifted(l*(#1+1cm,0));
    endfor;
    fi;
    if (#3 mod #4)<>0:
    trace FondRectangleColorie shifted(m*(#1+1cm,0));
    else:
    remplis (polygone(A,B,C,D) shifted(m*(#1+1cm,0))) withcolor #5;
    fi;
    trace FondRectangle shifted(m*(#1+1cm,0));
  \end{mplibcode}
  \else
\begin{mpost}
      pair A,B,C,D,M[],N[],R[],S[];
    A=(1,1);
    B-A=(#1,0);
    C-B=(0,#2);
    D-C=A-B;
    numeric parts;
    parts=(#4 div #6);
    for k=0 upto parts:
    M[k]=(k/parts)[A,B];
    N[k]=(k/parts)[D,C];
    endfor;
    if #6>1:
    for k=0 upto #6:
    R[k]=(k/#6)[A,D];
    S[k]=(k/#6)[B,C];
    endfor;
    fi;
    picture FondRectangle;
    FondRectangle=image(%
    draw polygone(A,B,C,D);
    for k=1 upto (parts-1):
    draw segment(M[k],N[k]);
    endfor;
    if #6>1:
    for k=1 upto (#6-1):
    draw segment(R[k],S[k]);
    endfor;
    fi;
    );
    picture FondRectangleColorie;
    FondRectangleColorie=image(%
    if #6=1:
    remplis polygone(A,M[#3 mod #4],N[#3 mod #4],D) withcolor #5;
    else:
    DDiv=(#3 mod #4) div parts;
    MMod=(#3 mod #4) mod parts;
    remplis polygone(A,B,S[DDiv],R[DDiv]) withcolor #5;
    remplis polygone(R[DDiv],(xpart(M[MMod]),ypart(R[DDiv])),(xpart(M[MMod]),ypart(R[DDiv+1])),R[DDiv+1]) withcolor #5;
    fi;
    );    
    m=#3 div #4;
    if (#3 mod #4)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto m-1:
    remplis (polygone(A,B,C,D) shifted(l*(#1+1cm,0))) withcolor #5;
    trace FondRectangle shifted(l*(#1+1cm,0));
    endfor;
    fi;
    if (#3 mod #4)<>0:
    trace FondRectangleColorie shifted(m*(#1+1cm,0));
    else:
    remplis (polygone(A,B,C,D) shifted(m*(#1+1cm,0))) withcolor #5;
    fi;
    trace FondRectangle shifted(m*(#1+1cm,0));
  \end{mpost}
  \fi
}

\def\MPFractionRectangleH#1#2#3#4#5#6#7{%
  % #1 longueur, #2 largeur, #3 num, #4 deno, #5 couleur, #6 multiple
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair A,B,C,D,M[],N[],R[],S[];
    A=(1,1);
    B-A=(#1,0);
    C-B=(0,#2);
    D-C=A-B;
    numeric parts;
    parts=(#4 div #6);
    for k=0 upto parts:
    M[k]=(k/parts)[A,B];
    N[k]=(k/parts)[D,C];
    endfor;
    if #6>1:
    for k=0 upto #6:
    R[k]=(k/#6)[A,D];
    S[k]=(k/#6)[B,C];
    endfor;
    fi;
    picture FondRectangle;
    FondRectangle=image(%
    draw polygone(A,B,C,D);
    for k=1 upto (parts-1):
    draw segment(M[k],N[k]);
    endfor;
    if #6>1:
    for k=1 upto (#6-1):
    draw segment(R[k],S[k]);
    endfor;
    fi;
    );
    picture FondRectangleColorie;
    FondRectangleColorie=image(%
    if #6=1:
    drawoptions(withpen pencircle scaled#7);
    trace hachurage(polygone(A,M[#3 mod #4],N[#3 mod #4],D),45,0.25,0) withcolor #5;
    else:
    DDiv=(#3 mod #4) div parts;
    MMod=(#3 mod #4) mod parts;
    drawoptions(withpen pencircle scaled#7);
    trace hachurage(polygone(A,B,S[DDiv],R[DDiv]),45,0.25,0) withcolor #5;
        drawoptions(withpen pencircle scaled#7);
    trace hachurage(polygone(R[DDiv],(xpart(M[MMod]),ypart(R[DDiv])),(xpart(M[MMod]),ypart(R[DDiv+1])),R[DDiv+1]),45,0.25,0) withcolor #5;
    fi;
    );    
    m=#3 div #4;
    if (#3 mod #4)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto m-1:
        drawoptions(withpen pencircle scaled#7);
        trace hachurage(polygone(A,B,C,D) shifted(l*(#1+1cm,0)),45,0.25,0) withcolor #5;
            drawoptions(withpen pencircle scaled#7);
    trace FondRectangle shifted(l*(#1+1cm,0));
    endfor;
    fi;
    if (#3 mod #4)<>0:
        drawoptions(withpen pencircle scaled#7);
    trace FondRectangleColorie shifted(m*(#1+1cm,0));
    else:
        drawoptions(withpen pencircle scaled#7);
    trace hachurage(polygone(A,B,C,D) shifted(m*(#1+1cm,0)),45,0.25,0) withcolor #5;
    fi;
        drawoptions(withpen pencircle scaled#7);
    trace FondRectangle shifted(m*(#1+1cm,0));
  \end{mplibcode}
  \else
\begin{mpost}
    pair A,B,C,D,M[],N[],R[],S[];
    A=(1,1);
    B-A=(#1,0);
    C-B=(0,#2);
    D-C=A-B;
    numeric parts;
    parts=(#4 div #6);
    for k=0 upto parts:
    M[k]=(k/parts)[A,B];
    N[k]=(k/parts)[D,C];
    endfor;
    if #6>1:
    for k=0 upto #6:
    R[k]=(k/#6)[A,D];
    S[k]=(k/#6)[B,C];
    endfor;
    fi;
    if #6=1:
    drawoptions(withpen pencircle scaled#7);
    draw hachurage(polygone(A,M[#3],N[#3],D),45,0.25,0) withcolor #5;
    else:
    DDiv=#3 div parts;
    MMod=#3 mod parts;
    drawoptions(withpen pencircle scaled#7);
    draw hachurage(polygone(A,B,S[DDiv],R[DDiv]),45,0.25,0) withcolor #5;
    drawoptions(withpen pencircle scaled#7);
    draw hachurage(polygone(R[DDiv],(xpart(M[MMod]),ypart(R[DDiv])),(xpart(M[MMod]),ypart(R[DDiv+1])),R[DDiv+1]),45,0.25,0) withcolor #5;
    fi;
    drawoptions(withpen pencircle scaled#7);
    draw polygone(A,B,C,D);
    for k=1 upto (parts-1):
    draw segment(M[k],N[k]);
    endfor;
    if #6>1:
    for k=1 upto (#6-1):
    draw segment(R[k],S[k]);
    endfor;
    drawoptions();
    fi;
  \end{mpost}
  \fi
}

\def\MPFractionDisque#1#2#3#4{%
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair A,B[];
    A=(0,0);
    path cc;
    cc=cercles(A,#1);
    for k=0 upto #3:
    B[k]=pointarc(cc,(360/#3)*k);
    endfor;
    m=(#2 div #3);
    if (#2 mod #3)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    fill cc shifted(l*(2*#1+1cm,0)) withcolor #4;
    endfor;
    fi;
    fill ((A--B0--arccercle(B[0],B[#2 mod #3],A)--cycle) shifted (m*(2*#1+1cm,0))) withcolor #4;
    for l=0 upto m:
    draw cc shifted(l*(2*#1+1cm,0));
    for k=0 upto (#3-1):
    draw segment(A,B[k]) shifted(l*(2*#1+1cm,0));
    endfor;
    endfor;
  \end{mplibcode}
  \else
  \begin{mpost}
    pair A,B[];
    A=(0,0);
    path cc;
    cc=cercles(A,#1);
    for k=0 upto #3:
    B[k]=pointarc(cc,(360/#3)*k);
    endfor;
    m=#2 div #3;
    if (#2 mod #3)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    fill cc shifted(l*(2*#1+1cm,0)) withcolor #4;
    endfor;
    fi;
    fill ((A--B0--arccercle(B[0],B[#2 mod #3],A)--cycle) shifted (m*(2*#1+1cm,0))) withcolor #4;
    for l=0 upto m:
    draw cc shifted(l*(2*#1+1cm,0));
    for k=0 upto (#3-1):
    draw segment(A,B[k]) shifted(l*(2*#1+1cm,0));
    endfor;
    endfor;
  \end{mpost}
  \fi
}

\def\MPFractionDisqueH#1#2#3#4#5{%
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair A,B[];
    A=(0,0);
    path cc;
    cc=cercles(A,#1);
    for k=0 upto #3:
    B[k]=pointarc(cc,(360/#3)*k);
    endfor;
    m=#2 div #3;
    if (#2 mod #3)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    drawoptions(withpen pencircle scaled#5);
    draw hachurage(cc shifted(l*(2*#1+1cm,0)),1.5*360/#3,0.25,0) withcolor #4;
    endfor;
    fi;
    drawoptions(withpen pencircle scaled#5);
    draw hachurage((A--B0--arccercle(B[0],B[#2 mod #3],A)--cycle) shifted(m*(2*#1+1cm,0)),1.5*360/#3,0.25,0) withcolor #4;
    drawoptions(withpen pencircle scaled#5);
    for l=0 upto m:
    draw cc shifted(l*(2*#1+1cm,0));
    for k=0 upto (#3-1):
    draw segment(A,B[k]) shifted(l*(2*#1+1cm,0));
    endfor;
    endfor;
  \end{mplibcode}
  \else
  \begin{mpost}
    pair A,B[];
    A=(0,0);
    path cc;
    cc=cercles(A,#1);
    for k=0 upto #3:
    B[k]=pointarc(cc,(360/#3)*k);
    endfor;
    m=#2 div #3;
    if (#2 mod #3)=0:m:=m-1; fi;
    if m>0:
    for l=0 upto (m-1):
    drawoptions(withpen pencircle scaled#5);
    draw hachurage(cc shifted(l*(2*#1+1cm,0)),1.5*360/#3,0.25,0) withcolor #4;
    endfor;
    fi;
    drawoptions(withpen pencircle scaled#5);
    draw hachurage((A--B0--arccercle(B[0],B[#2 mod #3],A)--cycle) shifted(m*(2*#1+1cm,0)),1.5*360/#3,0.25,0) withcolor #4;
    drawoptions(withpen pencircle scaled#5);
    for l=0 upto m:
    draw cc shifted(l*(2*#1+1cm,0));
    for k=0 upto (#3-1):
    draw segment(A,B[k]) shifted(l*(2*#1+1cm,0));
    endfor;
    endfor;
  \end{mpost}
  \fi
}

\def\MPFractionSegment#1#2#3#4{
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair A,C,B[];
    A=(0,0);
    C-A=(#1,0);
    for k=0 upto #3:
    B[k]=(k/#3)[A,C];
    endfor;
    m=#2 div #3;
    if m>0:
    for l=0 upto (m-1):
    draw (segment(B[0],B[#3]) shifted(l*(#1+1cm,0))) withpen pencircle scaled 2 withcolor #4;
    endfor;
    fi;
    if (#2 mod #3)<>0:
    draw (segment(B[0],B[#2 mod #3]) shifted(m*(#1+1cm,0))) withpen pencircle scaled 2 withcolor #4;
    draw segment(A,C) shifted(m*(#1+1cm,0));
    fi;
    marque_p:="tiretv";
    for l=0 upto m-1:
    for k=0 upto #3:
    pointe(B[k] shifted(l*(#1+1cm,0)));
    endfor;
    endfor;
    if (#2 mod #3)<>0:
    for k=0 upto #3:
    pointe(B[k] shifted(m*(#1+1cm,0)));
    endfor;
    fi;
  \end{mplibcode}
  \else
  \begin{mpost}
        pair A,C,B[];
    A=(0,0);
    C-A=(#1,0);
    for k=0 upto #3:
    B[k]=(k/#3)[A,C];
    endfor;
    m=#2 div #3;
    if m>0:
    for l=0 upto (m-1):
    draw (segment(B[0],B[#3]) shifted(l*(#1+1cm,0))) withpen pencircle scaled 2 withcolor #4;
    endfor;
    fi;
    if (#2 mod #3)<>0:
    draw (segment(B[0],B[#2 mod #3]) shifted(m*(#1+1cm,0))) withpen pencircle scaled 2 withcolor #4;
    draw segment(A,C) shifted(m*(#1+1cm,0));
    fi;
    marque_p:="tiretv";
    for l=0 upto m-1:
    for k=0 upto #3:
    pointe(B[k] shifted(l*(#1+1cm,0)));
    endfor;
    endfor;
    if (#2 mod #3)<>0:
    for k=0 upto #3:
    pointe(B[k] shifted(m*(#1+1cm,0)));
    endfor;
    fi;
  \end{mpost}
  \fi
}

\def\MPFractionSegmentH#1#2#3#4#5{%
  \ifluatex
   \mplibforcehmode
  \begin{mplibcode}
    pair A,C,B[];
    A=(0,0);
    C-A=(#1,0);
    for k=0 upto #3:
    B[k]=(k/#3)[A,C];
    endfor;
    m=#2 div #3;
    if m>0:
    for l=0 upto (m-1):
    drawoptions(withpen pencircle scaled#5);
    draw hachurage(polygone(B[0]+u*(0,-0.15),B[#3]+u*(0,-0.15),B[#3]+u*(0,0.15),B[0]+u*(0,0.15)) shifted(l*(#1+1cm,0)),120,0.2,0) withcolor #4;
    drawoptions(withpen pencircle scaled#5);
    draw segment(A,C) shifted(l*(#1+1cm,0));
    endfor;
    fi;
    if (#2 mod #3)<>0:
    drawoptions(withpen pencircle scaled#5);
    draw hachurage(polygone(B[0]+u*(0,-0.15),B[#2 mod #3]+u*(0,-0.15),B[#2 mod #3]+u*(0,0.15),B[0]+u*(0,0.15)) shifted(m*(#1+1cm,0)),120,0.2,0) withcolor #4;
            drawoptions(withpen pencircle scaled#5);
    draw segment(A,C) shifted(m*(#1+1cm,0));
    fi;
    marque_p:="tiretv";
    for l=0 upto m-1:
    for k=0 upto #3:
    pointe(B[k] shifted(l*(#1+1cm,0)));
    endfor;
    endfor;
    if (#2 mod #3)<>0:
    for k=0 upto #3:
    pointe(B[k] shifted(m*(#1+1cm,0)));
    endfor;
    fi;
  \end{mplibcode}
  \else
  \begin{mpost}
        pair A,C,B[];
    A=(0,0);
    C-A=(#1,0);
    for k=0 upto #3:
    B[k]=(k/#3)[A,C];
    endfor;
    m=#2 div #3;
    if m>0:
    for l=0 upto (m-1):
    drawoptions(withpen pencircle scaled#5);
    draw hachurage(polygone(B[0]+u*(0,-0.15),B[#3]+u*(0,-0.15),B[#3]+u*(0,0.15),B[0]+u*(0,0.15)) shifted(l*(#1+1cm,0)),120,0.2,0) withcolor #4;
    drawoptions(withpen pencircle scaled#5);
    draw segment(A,C) shifted(l*(#1+1cm,0));
    endfor;
    fi;
    if (#2 mod #3)<>0:
    drawoptions(withpen pencircle scaled#5);
    draw hachurage(polygone(B[0]+u*(0,-0.15),B[#2 mod #3]+u*(0,-0.15),B[#2 mod #3]+u*(0,0.15),B[0]+u*(0,0.15)) shifted(m*(#1+1cm,0)),120,0.2,0) withcolor #4;
            drawoptions(withpen pencircle scaled#5);
    draw segment(A,C) shifted(m*(#1+1cm,0));
    fi;
    marque_p:="tiretv";
    for l=0 upto m-1:
    for k=0 upto #3:
    pointe(B[k] shifted(l*(#1+1cm,0)));
    endfor;
    endfor;
    if (#2 mod #3)<>0:
    for k=0 upto #3:
    pointe(B[k] shifted(m*(#1+1cm,0)));
    endfor;
    fi;
  \end{mpost}
  \fi
}

\newcommand\Fraction[2][]{%
  \useKVdefault[ClesFraction]%
  \setKV[ClesFraction]{#1}%
  \setsepchar[*]{/}%
  \readlist*\ListeFraction{#2}%
  \ifboolKV[ClesFraction]{Triangle}{%
    \ifboolKV[ClesFraction]{Reponse}{}{\setKV[ClesFraction]{Couleur=white}}%
      \ifboolKV[ClesFraction]{Hachures}{%
        \MPFractionTriangleH{\useKV[ClesFraction]{Longueur}}{\useKV[ClesFraction]{Parts}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}{\useKV[ClesFraction]{Epaisseur}}%
      }{%
        \MPFractionTriangle{\useKV[ClesFraction]{Longueur}}{\useKV[ClesFraction]{Parts}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}%
      }%
  }{%
    \ifboolKV[ClesFraction]{Regulier}{%
      \ifboolKV[ClesFraction]{Reponse}{}{\setKV[ClesFraction]{Couleur=white}}%
      \ifboolKV[ClesFraction]{Hachures}{%
        \MPFractionRegulierH{\useKV[ClesFraction]{Rayon}}{\useKV[ClesFraction]{Cotes}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}{\useKV[ClesFraction]{Epaisseur}}%
      }{%
        \MPFractionRegulier{\useKV[ClesFraction]{Rayon}}{\useKV[ClesFraction]{Cotes}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}%
      }%
    }{%
      \ifboolKV[ClesFraction]{Segment}{%
        \ifboolKV[ClesFraction]{Reponse}{}{\setKV[ClesFraction]{Couleur=white}}%
        \ifboolKV[ClesFraction]{Hachures}{%
          \MPFractionSegmentH{\useKV[ClesFraction]{Longueur}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}{\useKV[ClesFraction]{Epaisseur}}%
        }{%
          \MPFractionSegment{\useKV[ClesFraction]{Longueur}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}%
        }%
      }{%
        \ifboolKV[ClesFraction]{Rectangle}{%rectangle
          \ifboolKV[ClesFraction]{Reponse}{}{\setKV[ClesFraction]{Couleur=white}}%
          \ifboolKV[ClesFraction]{Hachures}{%
            \MPFractionRectangleH{\useKV[ClesFraction]{Longueur}}{\useKV[ClesFraction]{Largeur}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}{\useKV[ClesFraction]{Multiple}}{\useKV[ClesFraction]{Epaisseur}}%
          }{%
            \MPFractionRectangle{\useKV[ClesFraction]{Longueur}}{\useKV[ClesFraction]{Largeur}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}{\useKV[ClesFraction]{Multiple}}%
          }%
        }{%disque
          \ifboolKV[ClesFraction]{Reponse}{}{\setKV[ClesFraction]{Couleur=white}}%
          \ifboolKV[ClesFraction]{Hachures}{%
            \MPFractionDisqueH{\useKV[ClesFraction]{Rayon}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}{\useKV[ClesFraction]{Epaisseur}}% 
          }{%
            \MPFractionDisque{\useKV[ClesFraction]{Rayon}}{\ListeFraction[1]}{\ListeFraction[2]}{\useKV[ClesFraction]{Couleur}}%
          }%
        }%
      }%
    }%
  }%
}%