Download Learning mEtapost by Doing

Transcript
8.5
Precedence Rules of Binary Operators
METAPOST provides the classifiers primarydef, secondarydef, and tertiarydef (for def
macros, not for vardef macros) to set the level of priority of a binary operator. In the
example below, the orthogonal projection of a vector v along another vector w is defined as a
secondary binary operator.
u1
u4
2u2
u3
8.6
u2
beginfig(1);
secondarydef v projectedalong w =
if pair(v) and pair(w):
(v dotprod w) / (w dotprod w) * w
else:
errmessage "arguments must be vectors"
fi
enddef;
pair u[]; u1 = (20,80); u2 = (60,15);
drawarrow origin--u1;
drawarrow origin--u2;
drawarrow origin--2*u2;
u3 = u1 projectedalong u2;
u4 = 2*u2 projectedalong u1;
drawarrow origin--u3 withcolor blue;
draw u1--u3 dashed withdots;
draw ((1,0)--(1,1)--(0,1))
zscaled (6pt*unitvector(u2)) shifted u3;
drawarrow origin--u4 withcolor blue;
draw 2*u2--u4 dashed withdots;
draw ((1,0)--(1,1)--(0,1))
zscaled (6pt*unitvector(-u1)) shifted u4;
labeloffset := 4pt;
label.rt(btex $u_1$ etex, u1);
label.bot(btex $u_2$ etex, u2);
label.bot(btex $2u_2$ etex, 2*u2);
label.bot(btex $u_3$ etex, u3);
label.lft(btex $u_4$ etex, u4);
endfig;
end;
Recursion
A macro is defined recursively if in its definition, it makes a call to itself. Recursive definition
of a macro is possible in METAPOST. We shall illustrate this with the computation of a
Pythagorean tree.
51