# [NTG-context] Clipping a path to a boundary

```All,

```
I'm looking for a new idea for something I need to do. I typically create a path on a domain [-5,5] as follows, a arbitrary. Then I randomly reflect the path about and axis, a line, or maybe rotate it. The result is that parts of the path may lie outside a bounding window.
```
% define linear function f
vardef f(expr x)=
a*x*x
enddef;

% define paths for functions f, g, and h, respectively
path P;

P:=(-5,f(-5));
for x=-5 step .1 until 5:
P:=P--(x,f(x));
endfor;

% 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;

```
Then I do some stuff I need to do and bring back the path at a later time with:
```
draw pic;

```
This works fine. However, what I need to do is somehow clip the path P to the bounding box, but retain the resulting cutpath in a path variable Q. That is, I need to cut off the parts of the path P that lie outside the bounding box cpath:=(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle and store the clipped path in the path variable Q. The difficulty is I have no advance knowledge if the path P lies entirely inside the bounding box or if it escapes through one of the sides.
```
Any hints, tips, etc., would be very appreciated.

Thanks.

_______________________________________________
ntg-context mailing list
ntg-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/ntg-context
```