# How to use previous points of a path to specify next?

Consider this `test.mf`:

``````draw (10,20)--(10+100,20)--(10+100,20+50)--
(10+100+30,20+50)--(10+100+30,20+50-40);
showit;
end
``````

Is there a way in metafont/metapost to draw a line of given length in a given direction without the need to specify additional variables and repeat coordinates? Something like this pseudocode for above example:

``````draw (10,20)--<100right>--<50up>--<30right>--<40down>;
``````

EDIT

I came up with this but I can't make it work:

``````pair wire[][];
z0=(10,20);
xpart(wire1\1)=x0+100; ypart(wire1\1)=y0;
xpart(wire1\2)=xpart(wire1\1); ypart(wire1\2)=ypart(wire1\1)+50;
xpart(wire1\3)=xpart(wire1\2)+30; ypart(wire1\3)=ypart(wire1\2);
xpart(wire1\4)=xpart(wire1\3); ypart(wire1\4)=ypart(wire1\3)-40;
numeric n; n:=0;
draw z0 forever :
n:=n+1;
exitif unknown wire1[n];
-- wire1[n]
endfor;
end
``````
• Just for comparison purposes: In TikZ you could write `\draw (10, 20) -- ++(100,0) -- ++(0, 50) -- ++(30, 0) -- (0, -40);` – TeXnician Dec 12 '18 at 9:27
• we've done this several times before. I will try to find the relevant answers – Thruston Dec 12 '18 at 13:58

There's no built-in feature to do this, but it is not hard to write your own macro to do it. If you are short of time you can use or borrow the `turtle` function provided by the `mfpic` macros.

Like this:

``````prologues := 3;
outputtemplate := "%j%c.eps";
input grafbase
beginfig(1);
draw turtle((10,20), 100 right, 50 up, 30 right, 40 down);
endfig;
end.
``````

which produces: You can use the mfpic macros with Metafont as well as Metapost. See `texdoc mfpic` for details.

If you want to write your own macro, you could try something like this:

``````vardef turtle (text t) =
save n, nose, track;
pair nose, track[];
numeric n;
nose = origin;
n = 0;
for @ = t:
nose := nose + @;
track[n] := nose;
n := n+1;
endfor
track for i=1 upto n-1: -- track[i] endfor
enddef;
``````

although you might prefer to make it more robust with some error checking.

Here is in the spirit of your attempt (the 2nd fig)

``````outputtemplate := "%j-%c.mps";

beginfig(1);
z1 = (10,20);
z2 = z1 + (100,20);
z3 = z2 + (0,50);
z4 = z3 + (30,0);
z5 = z4 + (0,-40);

draw z1 -- z2 -- z3 -- z4 --z5;
endfig;

beginfig(2);
pair myz[];
myz = (10,20);
myz = myz + (100,20);
myz = myz + (0,50);
myz = myz + (30,0);
myz = myz + (0,-40);

numeric n;  n:=1;
path p;     p:=myz1;

forever :
n:=n+1;
exitif unknown myz[n];
p := p -- myz[n];
endfor;

draw p;

endfig;

end
``````