2

Is it possible to write a more "clean" code to fill the sectors ?

      \documentclass[border=5mm]{standalone}
      \usepackage{luatex85}
      \usepackage{luamplib}
      \begin{document}
      \mplibtextextlabel{enable}
      \begin{mplibcode}
      beginfig(1);

      path C, xx, yy;
      C = fullcircle scaled 200;

      xx = (point 4 of C -- point 0 of C) scaled 1.1;
      yy = (point 6 of C -- point 2 of C) scaled 1.1;


     fill buildcycle(point 0 of C -- origin,origin -- point 1 of C,
     subpath (1,0) of C --cycle) withcolor0.6*(white+blue);
     fill buildcycle(point 1 of C -- origin,origin -- point 2 of C,
     subpath (2,1) of C --cycle) withcmykcolor(0,0.5,0.69,0);
     fill buildcycle(point 2 of C -- origin,origin -- point 3 of C,
     subpath (3,2) of C --cycle) withcolor 0.6*(white+blue);
     fill buildcycle(point 3 of C -- origin,origin -- point 4 of C,
     subpath (4,3) of C --cycle) withcmykcolor(0,0.5,0.69,0);
     fill buildcycle(point 4 of C -- origin,origin -- point 5 of C,
     subpath (5,4) of C --cycle) withcolor 0.6*(white+blue);
     fill buildcycle(point 5 of C-- origin,origin -- point 6 of C,
     subpath (6,5) of C --cycle)  withcmykcolor(0,0.5,0.69,0);;
     fill buildcycle(point 6 of C-- origin,origin -- point 7 of C,
     subpath (7,6) of C --cycle) withcolor 0.6*(white+blue);
     fill buildcycle(point 7 of C-- origin,origin -- point 0 of C,
     subpath (8,7) of C --cycle) withcmykcolor(0,0.5,0.69,0);

    for i=1 step 2 until 7:
    draw origin -- point i of C;
    endfor;

   draw xx;
   draw yy;

   draw C withpen pencircle scaled 3/4;

   label.llft("$O$", origin);
   label.lrt("$I$", point 0 of C);
   label.ulft("$J$", point 2 of C);
   label.llft("$I'$", point 4 of C);
   label.lrt("$J'$", point 6 of C);
   label.urt("$M$", point 1 of C);
   label.ulft("$N$", point 3 of C);
   label.llft("$P$", point 5 of C);
   label.lrt("$Q$",point 7 of C);
   endfig;
  \end{mplibcode}
  \end{document}

3

Here is a small improvement that uses steps within loops.

 \usepackage{luamplib}
 \begin{document}
 \mplibtextextlabel{enable}
 \begin{mplibcode}
 beginfig(1);

 path C, xx, yy;
 C = fullcircle scaled 200;

 xx = (point 4 of C -- point 0 of C) scaled 1.1;
 yy = (point 6 of C -- point 2 of C) scaled 1.1;

for i=0 step 2 until 6 :
   fill buildcycle(point i of C -- origin,origin -- point i+1 of C,
   subpath (i+1,i) of C --cycle) withcolor 0.6*(white+blue);
endfor
for i=1 step 2 until 7:
   fill buildcycle(point i of C -- origin,origin -- point i+1 of C,
   subpath (i+1,i) of C --cycle) withcmykcolor(0,0.5,0.69,0);
endfor

for i=1 step 2 until 7:
draw origin -- point i of C;
endfor;

  draw xx;
  draw yy;

  draw C withpen pencircle scaled 3/4;

  label.llft("$O$", origin);
  label.lrt("$I$", point 0 of C);
  label.ulft("$J$", point 2 of C);
  label.llft("$I'$", point 4 of C);
  label.lrt("$J'$", point 6 of C);
  label.urt("$M$", point 1 of C);
  label.ulft("$N$", point 3 of C);
  label.llft("$P$", point 5 of C);
  label.lrt("$Q$",point 7 of C);
  endfig;
 \end{mplibcode}
 \end{document}

output

You can also improve the second loop according to Schrödinger cat suggestion, but my trigonometry skills are far too bad to understand them.

PS I tested it under ConTeXt with \startMPpage\stopMPpage

2

Again just for fun a TikZy version.

\documentclass[tikz,border=3.14mm]{standalone}
\definecolor{witch}{cmyk}{0,0.5,0.69,0}
\begin{document}
\begin{tikzpicture}[declare function={R=pi;FabriceAnchor(\x)=180+\x-45*cos(2*\x);}]
 \foreach \X [count=\Y,evaluate=\Y as \Z using \Y*45] in {M,J,N,I',P,J',G,I}
 {\draw \ifodd\Y [fill=blue!60] \else [fill=witch] \fi
   (\Z:R) node[anchor={FabriceAnchor(\Z)}]{$\X$} 
  -- (0,0) -- (\Z-45:R) arc(\Z-45:\Z:R);}
 \draw (-4,0) -- (4,0)  (0,-4) -- (0,4) 
  (0,0) node[below left,inner xsep=0.5pt,inner ysep=7pt]{$O$};
\end{tikzpicture}
\end{document}

enter image description here

1
  • Thank you for this suggestion ; the code is simple to understand.
    – Fabrice
    Oct 6 '19 at 8:04
2

You can do the sectors without buildcycle. If C is your circle, then all you need is

center C -- subpath (a, b) of C -- cycle

to define a path for a sector. Recall that a fullcircle has 8 "points" along it, so

center C -- subpath (0, 1) of C -- cycle

defines a 45° sector starting on the x-axis. And you can switch colours using an "inline" if odd. Like this:

\documentclass[border=5mm]{standalone}
\usepackage{luatex85}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    path C, xx, yy;
    C = fullcircle scaled 140;

    xx = (point 4 of C -- point 0 of C) scaled 1.15;
    yy = (point 6 of C -- point 2 of C) scaled 1.15;

    color pink, royal;

    pink = (3/4, 1/3, 1/3);
    royal = (1/3, 1/3, 2/3);

    for i=1 upto length C:
        fill center C -- subpath (i-1, i) of C -- cycle 
             withcolor 1/2 [if odd i: royal else: pink fi, white];
    endfor

    draw point 1 of C -- point 5 of C;
    draw point 3 of C -- point 7 of C;

    draw C withpen pencircle scaled 3/4;

    draw xx;
    draw yy;

    label("$O$", 12 dir 247); % position this one specially

    label.lrt("$I$", point 0 of C);
    label.ulft("$J$", point 2 of C);
    label.ulft("$I'$", point 4 of C);
    % etc...

endfig;
\end{mplibcode}
\end{document}

Notice also how you can use dir to write polar coordinates directly.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.