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

David Arnold darnold at northcoast.com
Wed Aug 10 18:15:31 CEST 2005


Hans et al,

I think I've found something that works. I know ahead of time that 
there will only be two crossings of the clipping path boundary (I make 
sure of the by the parameters I choose):

beginfig(0);

% initialize a, h, and h for f(x)=ax^2+bx+c=a(x-h)^2+k
numeric a, h, k;
a=-1;
h=-3;
k=2;

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

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

% define function f(x)=a|x-b|+c
vardef f(expr x)=
	a*(x-h)*(x-h)+k
enddef;

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

% the line
F:=F scaled 1u;

% clipping path
path cpath;
cpath:=(-5,-5)--(5,-5)--(5,5)--(-5,5)--cycle;
cpath:=cpath scaled 1u;

% clip the path F
pair A, B;
F:=F cutbefore cpath;
F:=reverse F;
F:=F cutbefore cpath;
drawdblarrow F;



endfig;

end.



More information about the ntg-context mailing list