This appendix describes some of the macros supplied with TeXdraw which can be used to define additional commands for creating drawings. The macros described here work in the user specified coordinate system. Some of these toolbox macros are used by the TeXdraw commands themselves, others are supplied in an auxiliary file `txdtools.tex'.
The coordinate parsing macro \getpos
is useful for creating new
commands. This macro takes care of stripping leading and trailing
blanks from coordinates specified between parentheses. In addition,
symbolic coordinates are translated to the corresponding relative
coordinate using the segment offset and scaling in effect.
The macro \currentpos
returns the relative coordinates of the
current position. The returned values are relative to the current
segment and the current scaling. The macro \cossin
returns the
real-valued cosine and sine of the direction of the line joining two
points. The macro \vectlen
returns the length of a vector. The
results appear as the value of user supplied macro names.
\getpos (x y)\mx\my
(x
y)
are decoded. Symbolic coordinates are translated to the
corresponding relative coordinate using the current segment offset and
scaling. The resulting character strings representing the real-valued
coordinates are assigned to the macros specified by \mx
and
\my
.
\currentpos \mx\my
\mx
and \my
.
\cossin (x1 y1)(x2 y2)\cosa\sina
(x1 y1)
to (x2 y2)
. The character
strings representing these real-valued quantities are assigned to the
macros specified by \cosa
and \sina
.
\vectlen (x1 y1)(x2 y2)\len
\len
.
The TeXdraw toolbox supplies macros to perform real arithmetic on coordinate values. The result appears as the value of a user supplied macro name.
\realadd {value1} {value2} \sum
\sum
.
\realmult {value1} {value2} \prod
\prod
.
\realdiv {value1} {value2} \result
\result
.
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.
Go to the first, previous, next, last section, table of contents.