cutafter are not perfectly reliable in where they cut, if the path to be cut crosses the path where it must cut multiple times. To solve that' I've created two
primarydefs (maxcutbefore and maxcutafter) that can be used just as cutbefore and cutafter, but that remove as much as possible. This works. This minimal example shows it:
primarydef inPath mycutafter cutPath = begingroup save resultPath; path resultPath; save tmpPath, checkPath; path tmpPath, checkPath; save aT; resultPath := inPath; % Algo: we return resultPath unless checkPath is an improvement forever: aT := arctime ((arclength resultPath)-1) of resultPath; % t where length is length minus 1bp checkPath := subpath( 0, aT) of resultPath; tmpPath := checkPath cutafter cutPath; if (arclength tmpPath) < (arclength checkPath): % we have actually cut the path further resultPath := tmpPath; % set current result value and try again else: % we did not cut the path further, we have our result exitif true; fi endfor; resultPath endgroup enddef; vardef TEST( expr inputConn, fromPicOutline, toPicOutline) = save resultConn, workingConn; path resultConn, workingConn; workingConn := inputConn; % if true: resultConn := (inputConn cutbefore fromPicOutline) mycutafter toPicOutline; % else: % show "HELLO"; % fi resultConn enddef; path line, ca, cb; ca := fullcircle scaled 2 shifted (-5,0); cb := fullcircle scaled 2 shifted (5,0); line := (-10,0)--(10,0); line := TEST( line, ca, cb); draw ca; draw cb; draw line;
As you can see, I'm using
mycutafter. If you change that to
cutafter, it also works, but the line is cut to the right of the right circle, not to the left.
But when I uncomment the if:--else:--fi lines, the vardef
TEST fails with:
metapost log > ! Extra 'else'. metapost log > TEST->...2)cutbefore(EXPR3))mycutafter(EXPR4);else metapost log > :show"HELLO";fi.resultConn... metapost log > <*> ...-10,0)--(10,0); line := TEST( line, ca, cb) metapost log > ; draw ca; draw cb; draw l...
But if I uncomment the lines and use the original
cutafter, it also works. There must be something I do not understand regarding the grammar of METAFONT that makes my primarydef interfere with my vardef code. But what? And why?