[NTG-context] Clipping a path to a boundary

Hans Hagen pragma at wxs.nl
Mon Aug 8 13:30:30 CEST 2005


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 at ntg.nl
>> http://www.ntg.nl/mailman/listinfo/ntg-context
>>
>
> _______________________________________________
> ntg-context mailing list
> ntg-context at 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
-----------------------------------------------------------------



More information about the ntg-context mailing list