3

Suppose I have a array of path path pat[]; I try to define a function like this:

def drawpaths(expr plists)=
  for p in plists:
    pickup defaultpen;
    draw pat[p];
  endfor
enddef

so that I can draw the path 0,2,3 simply by call drawpaths(0,2,3).

Unfortunately, metapost don't support for loops of a list, is there any solution?

1 Answer 1

5

Of course Metapost supports loops over lists. Here in ConTeXt:

\startMPpage
path pat[];
pat[0] := (0,0) -- (10,10);
pat[1] := (0,0) -- (10,20);
pat[2] := (0,0) -- (10,30);

def drawpaths(text plists) =
    for p = plists:
        pickup defaultpen;
        draw pat[p];
    endfor
enddef;

drawpaths(0,1,2);
\stopMPpage
1
  • That's it! Thanks a lot! We can also move the definition to the very beginning of the code and set the variable p as a local one.
    – user19832
    Nov 22, 2017 at 4:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .