Re: [NTG-context] Clipping a path to a boundary --- Being more specific
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. ___ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context
Re: [NTG-context] Clipping a path to a boundary --- Being more specific
Hans et al, I tried what follows. But as far as I can see, I am not getting back a path q that is clipped to the cpath. vardef lastpath (expr p) = save _p_ ; path _p_ ; _p_ := origin ; for i within p : if stroked i : _p_ := pathpart i ; fi ; endfor ; _p_ enddef ; vardef firstpath (expr p) = save _p_, _b_ ; path _p_ ; _p_ := origin ; boolean _b_ ; _b_ := false ; for i within p : if not _b_ : if stroked i : _b_ := true ; _p_ := pathpart i ; fi ; fi ; endfor ; _p_ enddef ; beginfig(0); %initialize a of f(x)=|x-a| numeric a; a=-2; % initialize scale numeric u; 10u=3in; %function definition vardef f(expr x)= abs(x-a) enddef; %path path F; F:=(-5,f(-5)); for x=-5 step 1 until 5: F:=F--(x,f(x)); endfor; %scale and draw F F:=F scaled u; draw F withcolor blue; %clip path path cpath; cpath:=(5,-5)--(5,5)--(-5,5)--(-5,-5)--cycle; cpath:=cpath scaled u; clip currentpicture to cpath; %save and null currentpicture picture pic; pic:=currentpicture; currentpicture:=nullpicture; %retrieve first path path q; q:=firstpath(pic); %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)); drawdblarrow q withcolor blue; endfig; end. On Aug 8, 2005, at 6:20 AM, Hans Hagen wrote: David Arnold wrote: 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. hm, this need some more brainpower than the previous solution, in mp-tool there is a repathed macro that does most of the job so let's extend that one a bit: \starttext \setupcolors[state=start] \startMPpage def restroke suffix p = p := repathed (21,p) enddef ; % keep attributes def reprocess suffix p = p := repathed (22,p) enddef ; % no attributes vardef repathed (expr mode, p) text t = begingroup ; if mode=0 : save withcolor ; remapcolors ; fi ; save _p_, _pp_, _ppp_, _f_, _b_, _t_ ; picture _p_, _pp_, _ppp_ ; color _f_ ; path _b_ ; transform _t_ ; _b_ := boundingbox p ; _p_ := nullpicture ; for i within p : _f_ := (redpart i, greenpart i, bluepart i) ; if bounded i : _pp_ := repathed(mode,i) t ; setbounds _pp_ to pathpart i ; addto _p_ also _pp_ ; elseif clipped i : _pp_ := repathed(mode,i) t ; clip _pp_ to pathpart i ; addto _p_ also _pp_ ; elseif stroked i : if mode=21 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_") dashed dashpart i withpen penpart i withcolor _f_ ; ) ; elseif mode=22 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_")) ; else : addto _p_ doublepath pathpart i dashed dashpart i withpen penpart i withcolor _f_ % (redpart i, greenpart i, bluepart i) if mode=2 : t fi ; fi ; elseif filled i : if mode=11 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_") withcolor _f_ ; ) ; elseif mode=12 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_")) ; else : addto _p_ contour pathpart i withcolor _f_ if (mode=1) and (_f_<>refillbackground) : t fi ; fi ; elseif textual i : % textpart i <> "" : if mode <> 4 : addto _p_ also i if mode=3 : t fi ; fi ; else : addto _p_ also i ; fi ; endfor ; setbounds _p_ to _b_ ; _p_ endgroup enddef ; picture pic; pic := image( draw fullcircle scaled 5cm withpen pencircle scaled 5mm ; draw fullsquare scaled 2cm withpen pencircle scaled 5mm ; fill fullcircle scaled 1cm withpen pencircle scaled 5mm ; ) ; redraw pic withpen pencircle scaled 1mm withcolor red ; % restroke pic "drawdblarrow" ; pic := repathed(21,pic) "drawdblarrow" ; pic := repathed(11,pic) "drawdblarrow" ; draw pic ; \stopMPpage \stoptext the new restroke variant takes a macro name that itself takes o
Re: [NTG-context] Clipping a path to a boundary --- Being more specific
David Arnold wrote: 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. hm, this need some more brainpower than the previous solution, in mp-tool there is a repathed macro that does most of the job so let's extend that one a bit: \starttext \setupcolors[state=start] \startMPpage def restroke suffix p = p := repathed (21,p) enddef ; % keep attributes def reprocess suffix p = p := repathed (22,p) enddef ; % no attributes vardef repathed (expr mode, p) text t = begingroup ; if mode=0 : save withcolor ; remapcolors ; fi ; save _p_, _pp_, _ppp_, _f_, _b_, _t_ ; picture _p_, _pp_, _ppp_ ; color _f_ ; path _b_ ; transform _t_ ; _b_ := boundingbox p ; _p_ := nullpicture ; for i within p : _f_ := (redpart i, greenpart i, bluepart i) ; if bounded i : _pp_ := repathed(mode,i) t ; setbounds _pp_ to pathpart i ; addto _p_ also _pp_ ; elseif clipped i : _pp_ := repathed(mode,i) t ; clip _pp_ to pathpart i ; addto _p_ also _pp_ ; elseif stroked i : if mode=21 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_") dashed dashpart i withpen penpart i withcolor _f_ ; ) ; elseif mode=22 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_")) ; else : addto _p_ doublepath pathpart i dashed dashpart i withpen penpart i withcolor _f_ % (redpart i, greenpart i, bluepart i) if mode=2 : t fi ; fi ; elseif filled i : if mode=11 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_") withcolor _f_ ; ) ; elseif mode=12 : _ppp_ := i ; % indirectness is needed addto _p_ also image(scantokens(t & " pathpart _ppp_")) ; else : addto _p_ contour pathpart i withcolor _f_ if (mode=1) and (_f_<>refillbackground) : t fi ; fi ; elseif textual i : % textpart i <> "" : if mode <> 4 : addto _p_ also i if mode=3 : t fi ; fi ; else : addto _p_ also i ; fi ; endfor ; setbounds _p_ to _b_ ; _p_ endgroup enddef ; picture pic; pic := image( draw fullcircle scaled 5cm withpen pencircle scaled 5mm ; draw fullsquare scaled 2cm withpen pencircle scaled 5mm ; fill fullcircle scaled 1cm withpen pencircle scaled 5mm ; ) ; redraw pic withpen pencircle scaled 1mm withcolor red ; % restroke pic "drawdblarrow" ; pic := repathed(21,pic) "drawdblarrow" ; pic := repathed(11,pic) "drawdblarrow" ; draw pic ; \stopMPpage \stoptext the new restroke variant takes a macro name that itself takes one argument, i.e. a path, so in order to do more complex things, you need to define those Hans - Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl - ___ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context
Re: [NTG-context] Clipping a path to a boundary
David Arnold wrote: Hans, I tried: % redraw line path q; q:=pathpart pic; drawdblarrow q withcolor blue; But it drew the clipping path. I wanted the part of the path inside the clipping path. Am I missing something in your directions? ok, here's the full solution ... \starttext \setupcolors[state=start] \startMPpage vardef lastpath (expr p) = save _p_ ; path _p_ ; _p_ := origin ; for i within p : if stroked i : _p_ := pathpart i ; fi ; endfor ; _p_ enddef ; vardef firstpath (expr p) = save _p_, _b_ ; path _p_ ; _p_ := origin ; boolean _b_ ; _b_ := false ; for i within p : if not _b_ : if stroked i : _b_ := true ; _p_ := pathpart i ; fi ; fi ; endfor ; _p_ enddef ; picture pic; pic := image( draw fullcircle scaled 5cm withpen pencircle scaled 5mm ; draw fullsquare scaled 2cm withpen pencircle scaled 5mm ; fill fullcircle scaled 1cm withpen pencircle scaled 5mm ; ) ; draw pic ; drawdblarrow lastpath (pic) withcolor blue ; drawdblarrow firstpath(pic) withcolor red ; \stopMPpage \stoptext ... suitable for submission to the wiki On Aug 7, 2005, at 2:56 PM, Hans Hagen wrote: David Arnold wrote: 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. it all depends on what you want to do further on; - you can do some cutbefore and cutafter trickery, i.e. determine intersectionpoints with the edges of the bbox and do some cutting - if you just want the path back .. just ask for "pathpart pic" Hans - Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl - ___ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context ___ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context -- - Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl - ___ 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 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
Re: [NTG-context] Clipping a path to a boundary
Hans, I tried: % redraw line path q; q:=pathpart pic; drawdblarrow q withcolor blue; But it drew the clipping path. I wanted the part of the path inside the clipping path. Am I missing something in your directions? On Aug 7, 2005, at 2:56 PM, Hans Hagen wrote: David Arnold wrote: 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. it all depends on what you want to do further on; - you can do some cutbefore and cutafter trickery, i.e. determine intersectionpoints with the edges of the bbox and do some cutting - if you just want the path back .. just ask for "pathpart pic" Hans - Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl - ___ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context ___ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context
Re: [NTG-context] Clipping a path to a boundary
David Arnold wrote: 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. it all depends on what you want to do further on; - you can do some cutbefore and cutafter trickery, i.e. determine intersectionpoints with the edges of the bbox and do some cutting - if you just want the path back .. just ask for "pathpart pic" Hans - Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl - ___ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context
[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