[NTG-context] Clipping a path to a boundary --- Being more specific

David Arnold darnold at northcoast.com
Mon Aug 8 03:17:18 CEST 2005


Hans et al,

Let me try to be more specific. Here is a file which is very typical of 
the way I draw graphs for my mathematics classes. When you compile, you 
will see the graph of the absolute value of x-1. That is, it draws the 
graph of y=|x-1|, but only after clipping it to the cpath.  What I 
would like to do is draw the graph with the drawdblarrow command so 
that the graph of the absolute value ends in arrowheads. So, I want to 
clip the graph to the bounding box, save the picture, null the picture, 
then somehow extract the path into the variable F in its clipped form 
(not the original path F that extends outside the clip path) so that I 
can replace the command draw pic with drawdblarrow F.

beginfig(0);

numeric a, c;
a=1;

% define function f(x)=|x-a|
vardef f(expr x)=
	abs(x-a)
enddef;

% draw line with given point and slope
path F;
F:=(-5,f(-5));
for x=-5 step 1 until 5:
	F:=F--(x,f(x));
endfor;

% initialize scale
numeric u; 10u=3in;

% the line
F:=F scaled 1u;
draw F 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;
currentpicture:=nullpicture;

% erase currentpicture
currentpicture:=nullpicture;

% draw grid
for k=-5u step 1u until 5u:
	draw (-5u,k)--(5u,k) withcolor 0.85white;
	draw (k,-5u)--(k,5u) withcolor 0.85white;
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;

endfig;

end.



More information about the ntg-context mailing list