[metapost] (newbie) macro with a sequence of pairs

Nicola nvitacolonna at gmail.com
Thu Dec 17 22:49:56 CET 2009


In article <4B2742EA.7060001 at elvenkind.com>,
 Taco Hoekwater <taco at elvenkind.com> wrote:

> luigi scarso wrote:
> > I'm pretty new about metapost programming
> > and I need to write a "polyline" macro
> > that take a with a "sequence of pairs"  like
> > (0,0) (10,10) (30,10)
> > and draw  a line
> > ie
> > polyline (0,0) (10,10) (30,10)
> > is
> > draw (0,0)--(10,10)--(30,30)
> > 
> >  Is it possible ?
> 
> Not without a lot of trickery, I think. Why do you
> not want to use
> 	
>    polyline (0,0)--(10,10)--(30,30);

Here is some trickery:

def next text p =
   -- begingroup [p]
enddef;

vardef polyline(expr x,y) text points =
   save (,),[,];
   delimiters [ ];
   let ( = next;
   let ) = endgroup;
   draw [x,y] points
enddef;

beginfig(0);
   polyline (0,0) (10,10) (30,10) ;
   polyline (5,7); % Boundary case
endfig;

Don't expect this to work if you deviate, even slightly, from the given 
syntax (e.g., if you want to use variables to denote pairs).

Regards,
Nicola



More information about the metapost mailing list