Smoothing over 'moveto' vs 'lineto'

Skip to first unread message

luser droog

Jan 24, 2022, 12:19:21 PMJan 24
In simple drawing programs I keep running into the very minor
problem that you have to call 'moveto' on the first point but
then 'lineto' on all the other points to put a line segment into
the path. It is very minor. But it keeps on happening.

This last time around I had my points all nicely packed into
an array of array. Lovely. And that led to this function which
I haven't found the right name for:

/fx,mapgxs { % a f g . [a0 f] [a1 g .. aN g]
3 1 roll exch dup first % g f a a0
3 -1 roll [ 3 1 roll exec ] % g a [a0 f]
3 1 roll rest exch map % [a0 f] [a1 g .. aN g]
} def
/first{ 0 get } def
/rest{ 1 1 index length 1 sub getinterval } def
/map { 1 index xcheck 3 1 roll [ 3 1 roll forall ] exch {cvx} if } def

Which you call like this:

/draw { % [[x0 y0]..[xN yN]] . -
{aload pop moveto} {aload pop lineto} fx,mapgxs pop pop
} def

Looking over some old code just now I found a completely
different approach to the same problem, by redefining the
thing to call. The `currentpoint stroke moveto` is just to "animate"
the drawing.

/newline { /line {moveto /line {lineto currentpoint stroke moveto} store} store } def

Which you could use like this (remove the "currentpoint stroke moveto"
above for closepath to work):

/draw {
newline {aload pop line} forall
} def

Are there other solutions? Is this a problem for anyone else?

luser droog

Jan 24, 2022, 12:58:58 PMJan 24
Oh. I forgot about this other way. Perhaps this should be called the "Oopsie!"

/line { {currentpoint pop pop}stopped{moveto}{lineto} ifelse } def

or just:

/line { {lineto}stopped{moveto}if } def

Reply all
Reply to author
0 new messages