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

Hans Hagen pragma at wxs.nl
Mon Aug 8 15:20:38 CEST 2005


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



More information about the ntg-context mailing list