(texdraw)Arrow curve


Prev: Real arithmetic Up: TeXdraw Toolbox
Enter node , (file) or (file)node

B.3 Arrow curve
===============

This example illustrates the use of the TeXdraw toolbox routines to do
computations with the coordinates.  The problem will be tackled in two
parts.  First, we will produce a macro to place an arrowhead on a Bezier
curve.  Then given this macro, we will produce a macro which can draw a
"wiggly" line from the current position to a given coordinate.

   The first macro, '\cavec', uses the '\cossin' command to determine
the the cosine and sine of the angle of the line joining the second
control point to the end point of the Bezier curve.  Recall that the
Bezier curve is tangent to this line at the end point.  After drawing
the Bezier curve, the scaling is set locally to absolute units of 0.05
inches.  We go back down the line from the end point by 0.05 inches and
draw an arrow vector to the end point from there.  This arrow vector is
mostly arrowhead, with little or no tail.

     \def\cavec (#1 #2)(#3 #4)(#5 #6){
       \clvec (#1 #2)(#3 #4)(#5 #6)
       \cossin (#3 #4)(#5 #6)\cosa\sina
       \rmove (0 0)
       \bsegment
         \drawdim in \setsegscale 0.05
         \move ({-\cosa} -\sina)  \avec (0 0)
       \esegment}

   Note the use of macros as arguments to a '\move' command.  Minus
signs are put in front of the macros.  However, the value of the macro
'\cosa' or '\sina' could be negative.  Fortunately, TeX accepts two
minus signs in a row and interprets the result as positive.  Note that
the '\rmove (0 0)' command before the beginning of the segment ensures
that the Bezier curve is stroked before the arrowhead is drawn.

   The second macro '\caw' builds on '\cavec'.  The goal is to produce a
wiggly vector that can be used as a pointer in a drawing.  Consider the
following symmetrical normalized Bezier curve.
     \centertexdraw{ \move (0 0) \cavec (1.4 0.1)(-0.4 -0.1)(1 0) }

   This curve has the appropriate wiggle.  Now we want to be able to
draw this curve, appropriately scaled and rotated.  The macro '\caw'
needs to do computations on the coordinates.  First, '\caw' uses the
macros '\getpos' and '\currentpos' to get the positions of the end and
start of the curve.  Next, the length of the vector is calculated using
the macro '\vectlen'.  A local macro '\rotatecoord' is used to rotate a
coordinate pair about the origin, using the cosine and sine of the
rotation angle.  The vector length is used to scale the normalized
curve.  The remaining code draws the rotated, normalized curve.

     \def\caw (#1 #2){
       \currentpos \xa\ya
       \cossin ({\xa} \ya)(#1 #2)\cosa\sina

     % The nominal wiggly curve is (0 0) (1+dx dy) (-dx -dy) (1 0)
     % Find the rotated offset (dx dy) -> (du dv)
       \rotatecoord (0.4 0.1)\cosa\sina \du\dv

     % calculate the length of the vector
       \vectlen ({\xa} \ya)(#1 #2)\len

     % draw the curve in normalized units
       \bsegment
         \setsegscale {\len}
         \realadd \cosa \du \tmpa  \realadd \sina \dv \tmpb
         \cavec ({\tmpa} \tmpb)({-\du} -\dv)({\cosa} \sina)
       \esegment
       \move (#1 #2)}

     % rotate a coordinate (x y)
     % arguments: (x y) cosa sina x' y'
     %  x' = cosa * x - sina * y;  y' = sina * x + cosa * y
     \def\rotatecoord (#1 #2)#3#4#5#6{
       \getpos (#1 #2)\xarg\yarg
       \realmult \xarg {#3} \tmpa  \realmult \yarg {#4} \tmpb
       \realadd \tmpa {-\tmpb} #5
       \realmult \xarg {#4} \tmpa  \realmult \yarg {#3} \tmpb
       \realadd \tmpa \tmpb #6}

   Finally, the new macro can be used as follows.
     \centertexdraw{
       \arrowheadtype t:W
       \move (0 0)
       \cavec (1.4 0.1)(-0.4 -0.1)(1 0)
       \move (1 0) \caw (1 1) \htext{tip at \tt (1 1)}
       \move (1 0) \caw (2 1) \htext{tip at \tt (2 1)}
       \move (1 0) \caw (2 0) \htext{tip at \tt (2 0)}

     }

   Note that the Bezier curve in the macro '\cavec' lies below the
arrowhead.  The example then draws an arrowhead of type 'W' to erase the
part of the line below the arrowhead.


automatically generated by info2www version 1.2.2.9