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

```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.

_______________________________________________
ntg-context mailing list
ntg-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/ntg-context
```
• [NTG-context] Clipping a path to a boundary --- Being more sp... David Arnold