[NTG-context] Clipping again
David Arnold
dwarnold45 at cox.net
Sun Jun 25 21:19:41 CEST 2006
Hi,
I confess I got some earlier advice on this issue, but I've never
been able to apply it to find a resolution. In the buffer that
follows, I define a clipping box:
% clipping path
path cpath;
cpath:=(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle;
cpath:=cpath scaled 1u;
A function is created and scaled:
% scale and draw graph
P:=P scaled u;
draw P withcolor blue;
The picture is clipped and saved:
% clip and save current picture
picture pic;
clip currentpicture to cpath;
pic:=currentpicture;
Then it is redrawn later.
% redraw line
draw pic;
Unfortunately, even though this works, mathematicians really want
arrows at each end of the graph. Easy enough to do with drawdblarrow
P withcolor blue, but the arrows then get clipped. What I really need
is to adapt the code below so that my function is clipped to the
boundary box, but then redrawn with arrows at each end of it. If
anyone can adjust my code to do that, it would be much appreciated,
and it would break down a barrier I've faced for years with metapost
coding.
Note that I've tried some stuff with cutbefore and cutafter with some
success. But I should remark that the code below is generated by a
perl script and some special coding we've set up to generate these
graphics on the fly for student quizzes. This is not a situation
where I can tweak an individual plot or two. Rather, our script might
generate 100 sets of the code below, all with different parameters.
So this magnifies the problem.
Again, any ideas would be greatly appreciated.
\startbuffer
numeric w,h;
w=4cm;h=4cm;
% inititialize numerator and denominator of the slope of f
numeric a;
a=2;
% initialize choice of function
numeric choice;
choice=1;
% initialize type of reflection
numeric reflect;
reflect=2;
% define linear function f
vardef f(expr x)=
a*x*x
enddef;
% define linear function g
vardef g(expr x)=
a*abs(x)
enddef;
% define linear function h
vardef h(expr x)=
a*x*(x-2)*(x+2)/2
enddef;
% define paths for functions f, g, and h, respectively
path P, F, G, H;
F:=(-5,f(-5));
for x=-5 step .1 until 5:
F:=F--(x,f(x));
endfor;
G:=(-5,g(-5));
for x=-5 step .1 until 5:
G:=G--(x,g(x));
endfor;
H:=(-5,h(-5));
for x=-5 step .1 until 5:
H:=H--(x,h(x));
endfor;
% choose the function to use, f, g, or h
if (choice=1):
P:=F;
elseif (choice=2):
P:=G;
else:
P:=H;
fi;
% choose the type of reflection
if (reflect=1):
P:=P;
elseif (reflect=2):
P:=P reflectedabout((0,0),(1,1));
elseif (reflect=3):
P:=P reflectedabout((0,0),(1,-1));
else:
P:=P reflectedabout((-1,0),(1,0));
fi;
% initialize scale
numeric u; 10u=w;
% scale and draw graph
P:=P scaled u;
draw P withcolor blue;
% clipping path
path cpath;
cpath:=(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle;
cpath:=cpath scaled 1u;
% clip and save current picture
picture pic;
clip currentpicture to cpath;
pic:=currentpicture;
% erase currentpicture
currentpicture:=nullpicture;
% draw grid
for k=-5u step 1u until 5u:
draw (-5u,k)--(5u,k) withcolor mygridcolor;
draw (k,-5u)--(k,5u) withcolor mygridcolor;
endfor;
% draw axes
drawarrow (-5u,0)--(5u,0);
drawarrow (0,-5u)--(0,5u);
% label axes
label.rt(btex $x$ etex, (5.2u,0));
label.top(btex $y$ etex, (0,5.2.u));
label.bot(btex $5$ etex, (5u,0));
label.lft(btex $5$ etex, (0,5u));
% redraw line
draw pic;
% draw vertical line test
numeric xvert;
if (reflect=3):
xvert:=-1;
else:
xvert:=1;
fi;
% vertical line
path vert;
vert:=(xvert,-5)--(xvert,5);
vert:=vert scaled u;
pickup pencircle scaled 2pt;
drawdblarrow vert withcolor red;
pickup defaultpen;
\stopbuffer
More information about the ntg-context
mailing list