[metapost] Re: tangent

Boguslaw Jackowski bop at bop.com.pl
Tue Jan 18 11:06:54 CET 2005

BJ> Could you present your solution?

HP> z is in origin, curve is concave (see picture)
HP>    |
HP> -------------------------
HP>    |     * * *
HP>    |   *        *
HP>    |  *           *
HP>    | *             *
HP>    |                *

Much better assumptions than just ``general'' ;-)

1. I'd rather use geometrical length as `delta' (this would
   involve arctime and arclength operators). Time is very
   imprecise and non-uniform.

2. Still better would be using a binary search, since you know
   in advance that the time you're looking for is somewhere between
   the time where the path goes rigthward and the length of the curve:

   path curve; numeric intersect, t, tl, tr;
   curve:=((0,0)..(100,100)..(300,-100)) shifted (100,-200); % exempli gratia
    tl:=directiontime right of curve; tr:=length curve;
     t:=1/2[tl,tr]; exitif (tr-tl)<2epsilon;
     numeric intersect;
     (intersect,0) = point t of curve + whatever * (direction t of curve);
     if (intersect<0): tl:=t else: tr:=t fi;
    show intersect; draw (point t of curve)--(intersect,0); draw curve;

3. My favorite technique, however, would be the following algorithm:

   path curve; numeric t, tprev;
   curve:=((0,0)..(100,100)..(1000,-100)) shifted (100,-200); % exempli gratia
   def cdir expr t = directiontime point t of curve of curve; enddef;
    t:=length curve;
    forever: tprev:=t; t:=cdir t; exitif abs(t-tprev)<eps; endfor;
    show t; draw origin--point t of curve; draw curve;

   The only thing we have to know in advance is a time t such that
   the line
     origin--point t of curve
   crosses the curve in two points (here t=length curve). The algorithm
   then replaces the time by the time where the curve direction is parallel
   to the secant. And again. And again. After a few steps the iteration
   should converge.

Needless to say, there are infinitely many cases where each of
the algorithms would fail ;-) But it is not difficult to ``robustify''
the algorithm (3) so that if will work in most of reasonable cases.

Cheers -- Jacko

BOP s. c.
ul. Bora-Komorowskiego 24, 80-377 Gdansk, Poland
tel. (+48 58) 553 46 59,  fax (+48 58) 511 03 81
bop at bop.com.pl, http://www.bop.com.pl

More information about the metapost mailing list