[metapost] Extending a path

Dan Luecking luecking at uark.edu
Thu Nov 11 19:10:33 CET 2010


At 11:05 AM 11/11/2010, Yasir Malik wrote:
>Hello,
>
>How can I extend a path at the edges (t=0 or t=infinity)?  The path is a line.

That depends on what you mean by "extend".

Assuming you have a path pp and wish to make a longer path by
appending path qq to it, then
   (pp & qq)
is all you need, provided the first point of qq equals the last
point of pp. Similarly, you can prepend with
   (qq & pp)
with a similar provision.

If you have other requirements on the extension, they amount to
defining qq appropriately. For example, if pp is a line and you
just want to produce a line that is twice as long, you can
define qq to be a shifted version of pp.
   n := length pp; % Of course, if pp is A--B, then n=1.
   pp & (pp shifted (point n of pp - point 0 of pp))

Or if you want to extend pp with a straight line for
a distance d:
   path qq;
   n := length pp;
   qq := (0,0)--(d*unitvector(direction n of pp));
   pp & (qq shifted point n of pp)

Note: sometimes the start of qq is mathematically equal to the
final point of pp, but not when limited accuracy calculations
are involve. In such cases "--" is safer than "&". MetaPost has
exact arithmetic for addition, subtraction and shifting, but
not for multiplication, division or rotations.

Finally, I have played around with cubic extrapolation:
taking the last cubic Bezier segment in a path, extracting
its mathematical formula, and extending the time variable
past 1. If that segment is a straight line, this will
produce a straight line extension. This is a little involved
(and seldom what anyone but a mathematician wants), but I can
provide some code if needed.

Caveat: I have not tested the above code snippets, but have
used similar things many times.

Regards,
Dan


Daniel H. Luecking
Department of Mathematical Sciences
Fayetteville, Arkansas
http://www-cs-faculty.stanford.edu/~knuth/iaq.html 



More information about the metapost mailing list